Code CrazeMar 18, 20231 min readStack Using Array#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 100int stack[size];int top=-1;void push(void);void pop(void);void peek_top(void);void isEmpty(void);void isFull(void);void display(void);void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void push(){ int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nStack is full!!"); } else { top++; stack[top]=v; }}void display(){ int i; if(top==-1) { printf("\nStack is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",stack[i]); } }}void pop(){ if(top==-1) { printf("\nStack is empty!!"); } else { top--; }}void peek_top(){ printf("\nTop : %d",top); printf("\nTop Value: %d",stack[top]);}void isEmpty(){ if(top==-1) { printf("\nStack is empty!!"); } else { printf("\nStack is not empty!!"); }}void isFull(){ if(top==size-1) { printf("\nStack is full!!"); } else { printf("\nStack is not full!!"); }}
#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 100int stack[size];int top=-1;void push(void);void pop(void);void peek_top(void);void isEmpty(void);void isFull(void);void display(void);void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void push(){ int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nStack is full!!"); } else { top++; stack[top]=v; }}void display(){ int i; if(top==-1) { printf("\nStack is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",stack[i]); } }}void pop(){ if(top==-1) { printf("\nStack is empty!!"); } else { top--; }}void peek_top(){ printf("\nTop : %d",top); printf("\nTop Value: %d",stack[top]);}void isEmpty(){ if(top==-1) { printf("\nStack is empty!!"); } else { printf("\nStack is not empty!!"); }}void isFull(){ if(top==size-1) { printf("\nStack is full!!"); } else { printf("\nStack is not full!!"); }}
Stack Using QueueA stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue operations. When an element is pushed onto the stack, it is enqueued