Skip to content

Commit 6da21ef

Browse files
authored
Merge pull request #27 from ADlv19/patch-1
Create Stack data structure in C
2 parents 09055ad + 78249ec commit 6da21ef

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

c/Stack_Implementation_Using_Array.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include<stdio.h>
2+
3+
void push(int n);
4+
void pop();
5+
void peek();
6+
void display();
7+
int isFull();
8+
int isEmpty();
9+
void printMenu();
10+
11+
const int max=10;
12+
int stack[max];
13+
int top=-1;
14+
15+
int main(){
16+
int ch,num;
17+
do{
18+
printMenu();
19+
scanf("%d",&ch);
20+
switch(ch){
21+
case 1:
22+
if(isFull()==1){
23+
break;
24+
}
25+
printf("Enter number to add in stack: ");
26+
scanf("%d",&num);
27+
push(num);
28+
break;
29+
30+
case 2:
31+
if(isEmpty()==1){
32+
printf("Stack is Empty\n");
33+
}
34+
else
35+
display();
36+
break;
37+
38+
case 3:
39+
peek();
40+
break;
41+
42+
case 4:
43+
pop();
44+
continue;
45+
}
46+
}while(ch!=5);
47+
48+
return 0;
49+
}
50+
51+
52+
void push(int n){
53+
top++;
54+
stack[top]=n;
55+
}
56+
57+
58+
void pop(){
59+
if(top==-1){
60+
printf("Stack is Empty");
61+
}
62+
else{
63+
printf("Element deleted from stack is :%d\n",stack[top--]);
64+
display();
65+
}
66+
}
67+
68+
void peek(){
69+
if(isEmpty()==1){
70+
printf("Stack is Empty\n");
71+
}
72+
else
73+
printf("Top element of stack is: %d\n",stack[top]);
74+
}
75+
76+
void display(){
77+
78+
for(int i=top;i>=0;i--){
79+
printf("\t%d",stack[i]);
80+
}
81+
82+
}
83+
84+
int isFull(){
85+
if(top>=max-1){
86+
printf("Stack Overflow\n\n");
87+
return 1;
88+
}
89+
else{
90+
return 0;
91+
}
92+
return 0;
93+
}
94+
95+
int isEmpty(){
96+
if(top==-1){
97+
return 1;
98+
}
99+
else{
100+
return 0;
101+
}
102+
}
103+
104+
void printMenu(){
105+
printf("\nEnter 1 to Push\n");
106+
printf("Enter 2 to Display\n");
107+
printf("Enter 3 to Peek\n");
108+
printf("Enter 4 to Pop\n");
109+
printf("Enter 5 to Exit\n\n");
110+
printf("Enter your Choice:");
111+
}

0 commit comments

Comments
 (0)