-
stack problem
here i know that the array 's' used for stack is providing garbage
also it is better to implement stack using linklists because of no size limit
but suppose that i want to implement a fixed size stack
how do i do it?
Code:
#include<stdio.h>
#include<conio.h>
int s[50];
int max;
int top = -1,n = 20;
int i;
int size(void) {
return n;
}
void push(int x) {
if (top==max)
printf("Stack overflow\t");
else
if (top==max)
{}
else
{
n++;
top++;
s[top] = x;
}
}
int pop(void) {
int popped = 0;
if (top==-1)
{
printf("Stack underflow\t");
return -1;
}
else
{
popped = s[top];
n--;
top--;
}
return popped;
}
int search(int x) {
for (i=0;i<=top;i++)
if (s[i] == x)
return i;
return -1;
}
int prints(void)
{
if (top==-1)
printf("Stack is empty ! ");
// {}
else
{
for (i=0;i<=top;i++)
printf("%d",s[i] + " ");
}
printf("\n");
return 0;
}
int main(void)
{
//just to get a fair idea of what's happening
printf("STACK ");
printf("\npush 1\t"); push(1); prints();
printf("push 2\t"); push(2); prints();
printf("push 3\t"); push(3); prints();
printf("push 4\t"); push(4); prints();
printf("push 5\t"); push(5); prints();
printf("push 6\t"); push(6); prints();
printf("push 7\t"); push(7); prints();
printf("push 8\t"); push(8); prints();
printf("push 9\t"); push(9); prints();
printf("pop\t"); pop(); prints();
printf("pop\t"); pop(); prints();
printf("push 9\t"); push(9); prints();
printf("push 8\t"); push(8); prints();
printf("pop\t"); pop(); prints();
printf("pop\t"); pop(); prints();
printf("pop\t"); pop(); prints();
printf("pop\t"); pop(); prints();
getch();
return 0;
}
OUTPUT :
STACK
push 1 4199355
push 2 Stack overflow 4199355
push 3 Stack overflow 4199355
push 4 Stack overflow 4199355
push 5 Stack overflow 4199355
push 6 Stack overflow 4199355
push 7 Stack overflow 4199355
push 8 Stack overflow 4199355
push 9 Stack overflow 4199355
pop Stack is empty !
pop Stackunderflow Stack is empty !
push 9 4199363
push 8 Stack overflow 4199363
pop Stack is empty !
pop Stackunderflow Stack is empty !
pop Stackunderflow Stack is empty !
pop Stackunderflow Stack is empty !
-
Code:
if (top==max)
printf("Stack overflow\t");
else
if (top==max)
{}
...
-
and this:
Code:
printf("%d",s[ i ] + " ");
I think what you meant to do was:
Code:
printf("%d ",s[ i ] );
-
i tried those corrections but only a minor diffrence in output
OUTPUT :
STACK
push 1 1-------------------------------------->here pushes 1
push 2 Stack overflow 1-------------------> why not here?
push 3 Stack overflow 1
push 4 Stack overflow 1
push 5 Stack overflow 1
push 6 Stack overflow 1
push 7 Stack overflow 1
push 8 Stack overflow 1
push 9 Stack overflow 1
pop Stack is empty !------------------------->here it pops it out
pop Stackunderflow Stack is empty !-----> ok
push 9 9---------------------------------------->pushes 9
push 8 Stack overflow 9
pop Stack is empty !
pop Stackunderflow Stack is empty !
pop Stackunderflow Stack is empty !
pop Stackunderflow Stack is empty !
it seems that it is considering the stacksize to be 1
why is it not taking the second element ??
-
well what is max... max is never defined...
this should solve it
Code:
#include<stdio.h>
#include<conio.h>
int s[50];
int max=49;
max should be defined as 49 instead of 50 because of the way you are validating your code...
and whats with this
Code:
if (top==max)
printf("Stack overflow\t");
else
if (top==max)
{}
else
{
n++;
top++;
s[top] = x;
}
Why is there an extra if condition... if(top==max) and else if(top==max) the 2nd condition will never be validated.. i think it should be
Code:
if (top==max)
printf("Stack overflow\t");
else
{
n++;
top++;
s[top] = x;
}
and what is the variable n doing....
another suggection.. instead of
make max=50 and use
because this is a better way of checking it.. what happens due to some logic error if top is made equal to something above 50 your program will never identify it because its always trying to detect overflow by checking if it is only equal to 50.. actually 49
-
thanks anyway
actually i understood that problem after i last posted my message and have rectified it . such a silly mistake!
well thanks a lot still for providing help
the output now is
OUTPUT :
STACK
push 1 1
push 2 1 2
push 3 1 2 3
push 4 1 2 3 4
push 5 1 2 3 4 5
push 6 1 2 3 4 5 6
push 7 1 2 3 4 5 6 7
push 8 1 2 3 4 5 6 7 8
push 9 1 2 3 4 5 6 7 8 9
pop 1 2 3 4 5 6 7 8
pop 1 2 3 4 5 6 7
push 9 1 2 3 4 5 6 7 9
push 8 1 2 3 4 5 6 7 9 8
pop 1 2 3 4 5 6 7 9
pop 1 2 3 4 5 6 7
pop 1 2 3 4 5 6
pop 1 2 3 4 5
and it's right .
Now i can use it in my problem.
thanks vasanth!!!
u r gr8 & i am ..........