Skip to content

Commit 9321e42

Browse files
authored
Added Stack Program using arrays.
1 parent be6e106 commit 9321e42

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

Stack/StackUsingArrays.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Program : Stack implementation using array in C.
3+
Author : Aman Kumar
4+
*/
5+
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
#define SIZE 5
9+
10+
//Global Variables
11+
int top = -1;
12+
int arr[SIZE],i;
13+
14+
// Pushing element into the stack.
15+
void push(int item)
16+
{
17+
if(top >= (SIZE - 1))
18+
{
19+
printf("Stack is overflow.\n");
20+
}
21+
else{
22+
top++;
23+
arr[top] = item;
24+
printf("Pushed %d in stack at %dth position.\n",item,top);
25+
}
26+
}
27+
28+
//Popping element from stack.
29+
void pop()
30+
{
31+
if(top < 0)
32+
{
33+
printf("Stack is underflow. \n");
34+
}
35+
else{
36+
printf("Popped %d from the stack.\n",arr[top]);
37+
arr[top] = NULL;
38+
top--;
39+
}
40+
}
41+
42+
//Peeking element of stack.
43+
void peek()
44+
{
45+
printf("Peeking top element.\n Top element is : %d\n",arr[top]);
46+
}
47+
48+
// Find element in stack
49+
void search(int item)
50+
{
51+
int flag = 0;
52+
for(i=0;i<SIZE;i++)
53+
{
54+
if(arr[i] == item)
55+
{
56+
flag = 1;
57+
break;
58+
}
59+
}
60+
(flag == 1)?printf("Element(%d) found at %dth position in stack.\n",item,i+1):printf("Did not find element (%d) in the stack.\n",item);
61+
}
62+
63+
//Finding minimum element of stack.
64+
void min()
65+
{
66+
int x = arr[0];
67+
for(i=0;i<SIZE;i++)
68+
{
69+
if(x > arr[i])
70+
x = arr[i];
71+
}
72+
printf("Minimum element in stack is : \t%d\n",x);
73+
}
74+
75+
//Finding maximum element of stack.
76+
void max()
77+
{
78+
int x = arr[0];
79+
for(i=0;i<SIZE;i++)
80+
{
81+
if(x < arr[i])
82+
x = arr[i];
83+
}
84+
printf("Maximum element in stack is : \t%d\n",x);
85+
}
86+
87+
//Displaying elements of stack.
88+
void display()
89+
{
90+
int i;
91+
for(i=0;i<SIZE;i++)
92+
printf("%d\n",arr[i]);
93+
}
94+
95+
96+
//Main function as user for operations input.
97+
int main()
98+
{
99+
int ch,x;
100+
while (1)
101+
{
102+
printf("Please select an option: \n 1. Push \t\t\t 2. Pop \n 3. Peek \t\t\t 4. Search \n 5. Minimum \t\t\t 6. Maximum \n 7. Display \t\t\t 8. Exit\n\n");
103+
scanf("%d",&ch);
104+
105+
switch(ch)
106+
{
107+
case 1:
108+
printf("Please enter the element which you want to push into the stack: \n");
109+
scanf("%d",&x);
110+
push(x);
111+
break;
112+
case 2:
113+
pop();
114+
break;
115+
case 3:
116+
peek();
117+
break;
118+
case 4:
119+
printf("Please enter the element which you want to search into the stack: \n");
120+
scanf("%d",&x);
121+
search(x);
122+
break;
123+
case 5:
124+
min();
125+
break;
126+
case 6:
127+
max();
128+
break;
129+
case 7:
130+
display();
131+
break;
132+
case 8:
133+
exit(0);
134+
break;
135+
default:
136+
printf("Invalid option requested.\nPlease retry.\n");
137+
break;
138+
}
139+
140+
}
141+
return 0;
142+
}

0 commit comments

Comments
 (0)