-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstack_implementation_using_array.c
More file actions
97 lines (97 loc) · 1.83 KB
/
stack_implementation_using_array.c
File metadata and controls
97 lines (97 loc) · 1.83 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
#include<stdio.h>
#include<stdlib.h>
//Stack implementation using array
#define N 5
int top=-1,stack[N];
void push()
{
int a;
if(top==(N-1))
printf("\nStack overflow\n");
else
{
int b;
printf("\nEnter data to push into the stack :- ");
scanf("%d",&b);
top++;
stack[top]=b;
}
}
void pop()
{
int c;
if(top==-1)
{
printf("\nStack underflow\n");
}
else
{
c=stack[top];
top--;
printf("\n%d popped out from the stack",c);
}
}
void topp()
{
if(top==-1)
{
printf("\nStack is Empty\n");
}
else
printf("\nThe top element of the stack is :- %d",stack[top]);
}
void isfull()
{
if(top==(N-1))
printf("\nYes stack is full\n");
else
printf("\nNo stack is not full\n");
}
void isempty()
{
if(top==-1)
printf("\nYes stack is empty\n");
else
printf("\nNo stack is not empty\n");
}
void display()
{
int i;
if(top==-1)
printf("\nStack is empty\n");
printf("\nThe elements in the stack are -:\n");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
int main()
{
int ch;
while(1)
{
printf("\n1. To push into the stack\n2. To pop an element from the stack\n3.To see the top element\n4.To check if the stack is full\n5. To check if the stack is empty\n6. To display the stack\n7.Exit\n");
printf("Enter your choice from the following -: ");
scanf("%d",&ch);
switch (ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : topp();
break;
case 4 : isfull();
break;
case 5 : isempty();
break;
case 6 : display();
break;
case 7 : exit(0);
break;
default: printf("\nInvalid Choice!!\nPlease make a valid choice\n");
break;
}
}
return 0;
}