Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Stack
- #include<conio.h>
- #include<stdio.h>
- #define MAX 3
- int st[MAX], top=-1;
- //push (add)
- void push(int st[], int val);
- //pop (remove)
- int pop(int st[]);
- //peep (search)
- int peep(int st[], int idx);
- void display(int st[]);
- void update(int st[], int val, int idx);
- void main()
- {
- int val, op, idx;
- clrscr();
- do
- {
- getch();
- clrscr();
- printf("\n Main Menu");
- printf("\n 1. Push");
- printf("\n 2. Pop");
- printf("\n 3. Peep");
- printf("\n 4. Display");
- printf("\n 5. Update");
- switch(op)
- {
- case 1:
- printf("Enter Number : ");
- scanf("%d", &val);
- push(st,val);
- break;
- case 2:
- val=pop(st);
- if(val==-1)
- {
- printf("\n The Value Deleted From The Stack");
- }
- break;
- case 3:
- printf("\n Enter The Index To Search : ");
- scanf("%d", &idx);
- if(idx<1)
- {
- printf("\n Invalid Index");
- }
- else
- {
- val=peep(st,idx);
- if(val != 1)
- {
- printf("\n The Value Stored at Top Of The Stack is %d", val);
- }
- }
- break;
- case 4:
- printf("\n Enter The Number to Update on to The Stack : ");
- scanf("%d", &val);
- printf("\n Enter The Index To Be Update on The Stack : ");
- scanf("%d", &idx);
- if(idx<1)
- {
- printf("\n Invalid Index");
- }
- else
- {
- update(st, val, idx);
- }
- break;
- case 5:
- display(st);
- break;
- }
- }while (op!=6);
- getch();
- }
- void push(int st[], int val)
- {
- if(top==MAX-1)
- {
- printf("\n Stack is Overflow");
- }
- else
- {
- top++;
- st[top]=val;
- }
- }
- int pop(int st[])
- {
- int val;
- if(top==1)
- {
- printf("Stack is Underflow");
- return -1;
- }
- else
- {
- val=st[top];
- top--;
- return val;
- }
- }
- void display(int st[])
- {
- int i;
- if(top == -1)
- {
- printf("\n Stack is Empty");
- }
- else
- {
- for(i=top;i>=0;i++)
- {
- printf("\n %d", st[i]);
- }
- }
- }
- int peep(int st[], int idx)
- {
- if(top-idx+1<=-1)
- {
- printf("\n Stack is Underflow");
- return -1;
- }
- else
- {
- return(st[top-idx+1]);
- }
- }
- void update(int st[], int val, int idx)
- {
- if(top-idx+1<=-1)
- {
- printf("\n Stack is Underflow");
- }
- else
- {
- st[top-idx+1]=val;
- }
- }
Add Comment
Please, Sign In to add comment