-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathStack implementation.c
More file actions
119 lines (110 loc) · 1.71 KB
/
Stack implementation.c
File metadata and controls
119 lines (110 loc) · 1.71 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
#include <stdio.h>
#define max 3
int arr[max],top=-1;
int push(int arr[],int val);
int pop(int arr[]);
int peek(int arr[]);
int display(int arr[]);
int main()
{
int val,choice;
do
{
printf("\n,,,,,,,,,,,,,,,MAIN MENU,,,,,,,,,,,,,,\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. PEEK\n");
printf("4. DISPLAY\n");
printf("5. EXIT\n");
printf("\nENTER YOUT CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element you want to add : ");
scanf("%d",&val);
push(arr,val);
break;
case 2:
val=pop(arr);
if (val!=-1)
{
printf("The value deleted is : %d\n",val);
}
break;
case 3:
val=peek(arr);
if (top!=-1)
{
printf("The element at stack's top : %d\n",val);
break;
}
case 4:
display(arr);
break;
}
}
while (choice !=5);
}
int push(int arr[],int val)
{
if (top==(max-1))
{
printf("\nSTACK IS OVERFLOW\n");
}
else
{
top++;
arr[top]=val;
printf("\n%d\n",top);
}
return 0;
}
int pop(int arr[])
{
int val;
if (top==-1)
{
printf("\nSTACK IS UNDERFLOW\n");
return -1;
}
else
{
val=arr[top];
top--;
return val;
}
return 0;
}
int peek(int arr[])
{
if (top==-1)
{
printf("\nSTACK IS EMPTY\n");
return -1;
}
else
{
return arr[top];
}
return 0;
}
int display(int arr[])
{
if (top==-1)
{
printf("\nSTACK IS EMPTY\n");
return -1;
}
else
{
printf("\nFROM TOP TO BOTTOM\n");
int x;
for(x=top;x>=0;x--)
{
printf("%d",arr[x]);
printf("\n");
}
}
return 0;
}