-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
79 lines (70 loc) · 1.62 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
int max=5,min=0,stack[5],top=-1;
int main(){
int i=1,m,j,n;
while(i!=0){
printf("Enter Your Choice: \n(1)POP (2)PEEP (3)PUSH (4)CHANGE (5)VIEW STACK (0)EXIT : ");
scanf("%d",&i);
switch(i){
case 0:
break;
case 1:
printf("POP:: %d\n",pop(stack));
break;
case 2:
printf("\nEnter index from top: ");
scanf("%d",&m);
printf("PEEP:: %d\n",peep(stack,m));
break;
case 3:
printf("\nEnter Number: ");
scanf("%d",&n);
printf("PUSH:: %d\n",n);
push(stack,n);
break;
case 4:
printf("\nEnter Number: ");
scanf("%d",&n);
printf("\nEnter index from top: ");
scanf("%d",&m);
printf("CHANGE:: %d @ %d\n",n,i);
change(stack,n,i);
case 5:
printf("\nStack is:\n[");
for(j=0;j<5;j++){
printf("%d ",stack[j]);
}
printf("]\n");
}
}
}
void change(int stack[],int val, int i){
if(top-i+1<min){
printf("Stack Underflow!!\n");
}else{
stack[top-i+1]=val;
}
}
void push(int stack[],int val){
if(top<max){
top=top+1;
stack[top]=val;
}else{
printf("Stack Overflow!!\n");
}
}
int pop(int stack[]){
if(top<min){
printf("Stack underflow!!\n");
}else{
top=top-1;
return stack[top+1];
}
}
int peep(int stack[],int i){
if((top-i+1)<min){
printf("Stack underflow!!\n");
}else{
return stack[top-i+1];
}
}