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?
OUTPUT :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; }
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 !