-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
155 lines (153 loc) · 2.54 KB
/
Copy pathstack.cpp
File metadata and controls
155 lines (153 loc) · 2.54 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<stdio.h>
#define SIZE 10
struct stack
{
int item[SIZE];
int top;
}s;
void init(struct stack*s);
void push(struct stack*s,int n);
int pop(struct stack*s);
int peep(struct stack*s,int i);
void change(struct stack *s,int i,int n);
void init(struct stack*p)
{
p->top=-1;
}
void push(struct stack*p,int n)
{
if(p->top==SIZE-1)
{
printf("stack is overflow");
}
else
{
p->top=p->top+1;
p->item[p->top]=n;
}
}
int pop(struct stack*p)
{
int value;
if(p->top==-1)
{
printf("stack is underflow");
}
else
{
p->top=p->top-1;
value=p->item[p->top+1];
}
return value;
}
int peep(struct stack*p,int i)
{
int value;
if(p->top-i+1<0)
{
printf("invalid");
}
else
{
value=p->item[p->top-i+1];
}
return value;
}
void change(struct stack*p,int i,int n)
{
if(p->top-i+1<0)
{
printf("invalid");
}
else
{
p->item[p->top-i+1]=n;
}
}
int main()
{
int i,c;
init(&s);
label:printf("\nmenu\n1.push\n2.pop\n3.peep\n4.change\n5.display\n6.exit");
printf("\nenter your choice:");
scanf("%d",&c);
if(c==1)
{
int v;
char j;
printf("\nenter the index of the variable you want to push:");
scanf("%d",&v);
push(&s,v);
printf("\npress y to continue and any other key to exit\n");
scanf("%s",&j);
if(j=='y'||j=='Y')
{
goto label;
}
}
else if(c==2)
{
int p;
char j;
p=pop(&s);
printf("\npoped variable is %d",p);
printf("\npress y for continue and any other key to exit\n");
scanf("%s",&j);
if(j=='y'||j=='Y')
{
goto label;
}
}
else if(c==3)
{
int i,p;
char j;
printf("\nenter the index of the variable you want to peep");
scanf("%d",&i);
p=peep(&s,i);
printf("\n the peeped variable is %d",p);
printf("\npress y to continue and any other key to exit\n");
scanf("%s",&j);
if(j=='y'||j=='Y')
{
goto label;
}
}
else if(c==4)
{
int i,v;
char j;
printf("enter the index and the value of variable you want to change:");
scanf("%d %d",&i,&v);
change(&s,i,v);
printf("\npress y to continue and any other key to exit\n");
scanf("%s",&j);
if(j=='y'||j=='Y')
{
goto label;
}
}
else if(c==5)
{
char j;
for(i=s.top;i>-1;i--)
{
printf("%d \n",s.item[i]);
}
printf("\npress y to continue and any other key to exit\n");
scanf("%s",&j);
if(j=='y'||j=='Y')
{
goto label;
}
}
else if(c==6)
{
printf("\ngoodbye\n");
}
else
{
printf("\ninvalid");
}
return 0;
}