Skip to content

Commit 95a181f

Browse files
authored
Update Stack.c
1 parent ba475d0 commit 95a181f

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

Stack.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,129 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#define Size 10
14

5+
void Push();
6+
void Pop();
7+
void Peek();
8+
void isEmpty();
9+
void isFull();
10+
void Display();
11+
12+
int Top = -1, array[Size];
13+
int main()
14+
{
15+
int option;
16+
while(1)
17+
{
18+
printf("\nOperation to be performed by Stack");
19+
        printf("\n1.Push\n2.Pop\n3.Peek or Top\n4.isEmpty\n5.isFull\n6.Display\n7.Exit");
20+
        printf("\n\nEnter the choice (1, 2, 3, 4, 5, 6, 7):");
21+
22+
        scanf("%d",&option);
23+
24+
switch(option)
25+
{
26+
case 1: Push();
27+
break;
28+
case 2: Pop();
29+
break;
30+
case 3: Peek();
31+
break;
32+
case 4: isEmpty();
33+
break;
34+
case 5: isFull();
35+
break;
36+
case 6: Display();
37+
break;
38+
case 7: exit(0);
39+
40+
default:
41+
printf("\nInvalid Option");
42+
43+
}
44+
}
45+
}
46+
47+
void Push()
48+
{
49+
int x;
50+
if (Top == Size - 1)
51+
{
52+
printf("\n Stack Overflow!");
53+
}
54+
else
55+
{
56+
printf("\nEnter number to be added to array: ");
57+
scanf("%d",&x);
58+
printf("%d is added to array", x);
59+
Top = Top + 1;
60+
array[Top] = x;
61+
}
62+
}
63+
64+
void Pop()
65+
{
66+
if (Top == -1)
67+
{
68+
printf("\n Stack Underflow!");
69+
}
70+
else
71+
{
72+
printf("Element %d is popped", array[Top]);
73+
Top = Top -1;
74+
}
75+
}
76+
77+
void Peek()
78+
{
79+
if (Top == -1)
80+
{
81+
printf("Stack Underflow!");
82+
}
83+
else
84+
{
85+
printf("\nLast Element of array is %d", array[Top]);
86+
}
87+
88+
}
89+
90+
void isEmpty()
91+
{
92+
if (Top == -1)
93+
{
94+
printf("\nArray is Empty!");
95+
}
96+
else
97+
{
98+
printf("\nArray contains element");
99+
}
100+
}
101+
102+
void isFull()
103+
{
104+
if (Top == Size -1)
105+
{
106+
printf("\nArray is Full!");
107+
}
108+
else
109+
{
110+
printf("\nArray is not full elements can be added to array.");
111+
}
112+
}
113+
114+
void Display()
115+
{
116+
if (Top == -1)
117+
{
118+
printf("\nStack Underflow");
119+
}
120+
else
121+
{
122+
int i;
123+
printf("\nElements of array array: ");
124+
for(i = Top; i >= 0; --i)
125+
{
126+
printf("%d ",array[i]);
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)