0

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

4th May 2019, 5:22 PM
Joseph Ndive
Joseph Ndive - avatar
1 Antwort
+ 2
Joseph Ndive, Please edit your question: 1. Put C in the Relevant Tags. 2. Save the code in your profile and share the link instead, as you can see, the code is truncated, it doesn't fit Description character limits. See the following post for a how-to on sharing links, in case you didn't know already. https://www.sololearn.com/post/74857/?ref=app * Do not reply as I will remove this note as soon as you updated the thread. Good luck with solution! 👍
4th May 2019, 5:34 PM
Ipang