2017-03-08 40 views
1

我是新来的c编程,而使用c程序实现堆栈时,它给我一个错误。在堆栈中获取错误(推送和弹出)

尽管在我的堆栈数组中有5个元素空间,但它在for循环的第二个迭代(同时推送元素)中给了我“堆栈溢出”(消息)。

&而飞出数

似乎是错误的,只有第一个号码弹出(5次)?

#include<stdio.h> 
#include<conio.h> 
#include <stdlib.h> 
# define MAX 5 
int stack1[MAX]; 
int top=-1; 
void push(int); 
int pop(); 
void display(); 
main() 
{ 
int choice,num; 
while(1) 
{ 
    printf("\nEnter Your Choice: \n"); 
    printf("Enter 1. to Push \n"); 
    printf("Enter 2. to pop \n"); 
    printf("Enter 3. to Display \n"); 
    printf("Enter 4. To Exit\n"); 
    scanf("%d",&choice); 
switch(choice) 
{ 

case 1: 
    printf("Enter Elements to be pushed\n"); 
    int z; 
    for(z=1;z<=5;z++) 
    { 
    printf("Enter a the code In Room %d: ",z); 
    scanf("%d",&num); 
    push(num);//Function call, Calling push 
    } 
    break; 
case 2: 
    printf("Numbers to be poped\n"); 
    int b; 
    for(b=1;b<=5;b++) 
    { 
    num=pop(); 
    }//Return a integer Value; 
    break; 

case 3: 
    display(); 
    break; 
case 4: 
    exit(1); 
default: 
    printf("Invalid Choice\n"); 
} 
} 
} 
void push(int element)//Push Function 
{ 
int x; 
for(x=1;x<=5;x++) 
{ 
if(top== MAX-1)//Check if Stack is Full 
{ 
    printf("Stack Overflow \n"); 
    return;//Terminate the function 
} 
top=top+1;//start from -1 and gets incremented 
stack1[top]=element;//insert elements at each step 
} 
} 
int pop() 
{ 
int a; 
for(a=1;a<=5;a++) 
{ 

int element; 
if(top==-1) 
{ 
    printf("Stack EMPTY can't delete anything"); 
    return; 
} 
element=stack1[top];//In stack Elements are Always delete from TOP 
top=top-1;//To shift the pointer 
printf("%d has been deleted \n", element); 
return element; 
} 
} 
void display() 
{ 
int i; 
if(top==-1) 
{ 
    printf("Stack Is empty can't display"); 
    return;//Terminates Display function 
} 
printf("\n\n"); 
for(i=top;i>=0;i--)//Displays top elements and decrements on each step 
    printf("%d\n",stack1[i]); 
} 
+0

该stackoverflow消息与您的堆栈数据结构无关:) – StoryTeller

回答

0

你不应该使用“”循环用于弹出,推搡。

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
# define MAX 5 
int stack1[MAX]; 
int top=-1; 
void push(int); 
int pop(); 
void display(); 
main() 
{ 
int choice,num; 
while(1) 
{ 
    printf("\nEnter Your Choice: \n"); 
    printf("Enter 1. to Push \n"); 
    printf("Enter 2. to pop \n"); 
    printf("Enter 3. to Display \n"); 
    printf("Enter 4. To Exit\n"); 
    scanf("%d",&choice); 
switch(choice) 
{ 

case 1: 
    printf("Enter Elements to be pushed\n"); 
    int z; 
    for(z=1;z<=5;z++) 
    { 
    printf("Enter a the code In Room %d: ",z); 
    scanf("%d",&num); 
    push(num);//Function call, Calling push 
    } 
    break; 
case 2: 
    printf("Numbers to be poped\n"); 
    int b; 
    for(b=1;b<=5;b++) 
    { 
    num=pop(); 
    }//Return a integer Value; 
    break; 

case 3: 
    display(); 
    break; 
case 4: 
    exit(1); 
default: 
    printf("Invalid Choice\n"); 
} 
} 
} 
void push(int element)//Push Function 
{ 
int x; 

if(top== MAX-1)//Check if Stack is Full 
{ 
    printf("Stack Overflow \n"); 
    return;//Terminate the function 
} 
top=top+1;//start from -1 and gets incremented 
stack1[top]=element;//insert elements at each step 

} 
int pop() 
{ 
int a; 


int element; 
if(top==-1) 
{ 
    printf("Stack EMPTY can't delete anything"); 
    return; 
} 
element=stack1[top];//In stack Elements are Always delete from TOP 
top=top-1;//To shift the pointer 
printf("%d has been deleted \n", element); 
return element; 

} 
void display() 
{ 
int i; 
if(top==-1) 
{ 
    printf("Stack Is empty can't display"); 
    return;//Terminates Display function 
} 
printf("\n\n"); 
for(i=top;i>=0;i--)//Displays top elements and decrements on each step 
    printf("%d\n",stack1[i]); 
} 
+0

为什么?是不允许的? – harsh97

+0

为第一个元素弹出5次,我编辑你的程序的方式是有道理的。您可以一次推送一个元素,并一次弹出一个元素。立即推送所有元素并弹出不适合堆叠。 – Saran

+0

是的,我知道,但为了解决这个问题陈述,我需要在ONE GO中采用5个元素,并一次性弹出它们,所以我认为循环应该是最好的方式。你能否提出一些其他实现方法? – harsh97

0

你有2 for循环。在主功能中有一个循环,在开关内部,在pushpop功能中也有一个循环。

pushpop功能将pushpop只有一个元素,并不会需要一个循环里面。

如果您在编写代码时使用了缩进,这将更加明显。这是正确缩进代码的原因之一。

+0

是的,我知道,但为了解决这个问题的陈述,我需要在ONE GO中取(推)5个元素,并且一次性弹出它们,所以我认为循环应该是最好的方法。你可以建议一些其他的方式来实现它 – harsh97

+0

你已经有一个循环的主要功能,将推动5个元素。如果在推送功能中有另一个循环,则试图推送总共25个元素。实际上,由于数组大小为5,会发生什么情况是第一个元素被推入5次,其余4个元素尝试推入(每次5次),但发现堆栈已满。所以,当你尝试弹出时,你会弹出第一个元素5次。 –

0

让我们一步一个脚印吧。

1)。您启动了一个for循环,以便用户可以为您的堆栈输入5个元素。一旦他输入了号码,你的推送功能就会被调用。在推,再次你有一个循环运行5次。因此,相同的元素被推入堆栈5次。所以在你的下一个选择中,堆栈已经满了。因此堆栈溢出消息。 2)。现在你的堆栈就像 - [5,5,5,5,5],假设5是输入的5号码,在你的弹出功能中,你再次运行一个循环。理想情况下,您只应该为用户每次弹出一个元素。因此,由于您正在运行一个循环,所有5个元素都会弹出,因为只有第一个元素位于堆栈中,这就是您在输出中看到的内容。

+0

那么,我在程序中做了什么改变,以便在每次迭代时移动指针并采取(推送)不同的元素? – harsh97

+0

好的。既然你已经有循环运行,它正在接受来自用户的元素,你可以简单地将该元素传递给你的推送函数。在你的push函数中,你现在有什么,因为我上面解释过的原因,你将不得不移除for循环。你会简单地拿那个元素,递增最高值并且做栈[top] = yourNewValue。简单。另外,我想提一下,当你定义一个5的数组时,索引星将从0开始。因此,对于(i = 0; i <= 4; i ++)而言,你的循环将会是类似的,而不是从1到5 [5]未定义。 –