-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack Data structure.c
122 lines (101 loc) · 2.08 KB
/
Stack Data structure.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
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
/* A simple Stack (dynamic) implementation
Date created: Friday; April 01, 2022
Content
01. Header files
1.1 stdio.h
02. Data structure
03. Functions
3.1 push() - Function to insert a data into the Stack
3.2 pop() - Function to throw a data from the Stack
3.3 display() - Function to print data values in the Stack
04. main
*/
// 01. Header files
#include<stdio.h>
// 02. Data structure
int stack[10],choice,n,top,x,i;
// 03. Functions
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is full");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t No elements to pop");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK are, \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
// 04. main
int main()
{
top=-1;
printf("\n Enter the size of STACK (maximum 10):");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS ");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice");
}
}
}
while(choice!=4);
return 0;
}