stacks
/* stack implementation using static array */ #include<stdio.h> #define CAPACITY 5 //define the stack size: pre-processor Macro, cant be change int stack[CAPACITY], top=-1; // here you can not pass ordinary var but u can pass macros cuz its a global variable void push(int); // in declaring the prototype, no need to include the element int pop(void); int isFull(void); int isEmpty(void); void traverse(void); void peek(void); void main(void) { int ch, item ; while(1) { printf("1.Push \n"); printf("2.Pop \n"); printf("3.Peek \n"); printf("4.Traverse \n"); printf("5.Quit \n"); printf("Enter your choice : "); scanf("%d", &ch); switch(ch) { case 1 : printf("Enter element : "); scanf("%d", &item); push(item); break; case 2 : item = pop(); if (item==0) { printf("stack is underflow \n"); } else { printf("popped item : %d\n", item); } break; case 3 : peek(); // display the first element break; case 4 : traverse(); //display all the element break; default: printf("Invalid input \n\n"); } } void push(int ele) //the prototype function must be declared, how to insert { if(isFull()) { printf("stack is overflow \n"); } else { top++ ; stack[top] = ele ; printf("%d pushed \n", ele ); } } isFull() { if(top == CAPACITY-1) { return 1 ; } else { return 0 ; } } int pop() // how to delete ele instacks { if (isEmpty()) { printf("stack is underflow \n"); } else { return stack[top--]; } } int isEmpty(top == -1) // how to delete ele instacks { return 1 ; } else { return 0 ; } void peek() { if (isEmpty()) { printf("stack is empty \n"); } else { printf("peek element : %d \n", stack[top] ); } } void traverse() { if (isEmpty()) { printf("stack is empty