Stack: Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out).
Mainly the following three basic operations are performed in the stack:
run these codes on your machine and insert choices to stack operations.
Thank you guys!!
Mainly the following three basic operations are performed in the stack:
- Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
- Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
- Peek or Top: Returns top element of stack.
- isEmpty: Returns true if stack is empty, else fals.
#include<stdio.h> #define N 10 int stack[N],top=-1;over=0;under=0; int push(int n) { if(top>=N-1) { printf("Overflow.\n"); return over=1; } else stack[++top]=n; } int pop() { if(top==-1) { printf("Underflow.\n"); return under=1; } return stack[top--]; } int main() { int choice,n,i,item; for(;;) { printf("Select an operation\n1.Push\n2.Pop\n3.Show\n4.Exit\n"); scanf("%d",&choice); if(choice==1) { printf("How many number you want to insert:\n"); scanf("%d",&n); printf("Please enter number:\n"); for(i=0;i<n;i++) { scanf("%d",&item); if(over==1) break; push(item); } } else if(choice==2) { printf("How many number you want to delete:\n"); scanf("%d",&n); for(i=0;i<n;i++) { pop(); if(under==1) break; printf("Deleted: %d\n",stack[top+1]); } } else if(choice==3) { if(top==-1) printf("Stack is empty.\n"); else { printf("Stack is:\n"); for(i=0;i<=top;i++) { printf("%d\n",stack[i]); } } } else if(choice==4) break; else printf("Wrong choice!!"); } }
run these codes on your machine and insert choices to stack operations.
Thank you guys!!
No comments:
Post a Comment