forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
99 lines (90 loc) · 1.65 KB
/
Copy pathmain.c
File metadata and controls
99 lines (90 loc) · 1.65 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
//program for stack using array
#include <stdio.h>
void push();
void pop();
void peek();
void update();
int a[100], top = -1;
int main()
{
int x;
while (1)
{
printf("\n0.exit");
printf("\n1.push");
printf("\n2.pop");
printf("\n3.peek");
printf("\n4.update");
printf("\nenter your choice? ");
scanf("%d", &x);
switch (x)
{
case 0:
return 0;
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
update();
break;
default:
printf("\ninvalid choice");
}
}
return (0);
}
//function for pushing the element
void push()
{
int n = 0;
printf("\nenter the value to insert? ");
scanf("%d", &n);
top += 1;
a[top] = n;
}
//function for poping the element out
void pop()
{
if (top == -1)
{
printf("\nstack is empty");
}
else
{
int item;
item = a[top];
top -= 1;
printf("\npoped item is %d ", item);
}
}
//function for peeping the element from top of the stack
void peek()
{
if (top >= 0)
printf("\n the top element is %d", a[top]);
else
printf("\nstack is empty");
}
//function to update the element of stack
void update()
{
int i, n;
printf("\nenter the position to update? ");
scanf("%d", &i);
printf("\nenter the item to insert? ");
scanf("%d", &n);
if (top - i + 1 < 0)
{
printf("\nunderflow condition");
}
else
{
a[top - i + 1] = n;
}
}