Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Stack Operations with Array
- Afif Al Mamun*/
- #include <stdio.h>
- #define MAX 5
- int top = -1;
- int arr[MAX];
- void push(int val)
- {
- if(top==MAX-1)
- {
- printf("Stack Overflow!\n");
- return;
- }
- top++;
- arr[top]=val;
- }
- void pop()
- {
- if(top==-1)
- {
- printf("Stack Underflow\n");
- return;
- }
- printf("%d has been popped!\n",arr[top--]);
- }
- int peek()
- {
- return arr[top];
- }
- int main()
- {
- int opt=1, v, i;
- while(opt!=0)
- {
- printf("1. Push\n2. Pop\n3. Top\n4. Display\n0. Exit\n\n");
- printf("Your choice: ");
- scanf("%d",&opt);
- printf("\n");
- switch(opt)
- {
- case 1:
- printf("--Push--\n");
- printf("Enter the value: ");
- scanf("%d",&v);
- push(v);
- break;
- case 2:
- printf("--Pop--\n");
- pop();
- break;
- case 3:
- printf("--Top--\n");
- printf("%d\n",peek());
- break;
- case 4:
- printf("--Display--\n");
- for(i=top;i>=0;i--)
- {
- printf("---\n");
- printf(" %d\n",arr[i]);
- }
- printf("---\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement