forked from codeitbijay/Hacktoberfest2k20
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stack using array code in C
111 lines (92 loc) · 1.35 KB
/
Stack using array code in 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
#include<stdio.h>
#include<stdlib.h>
#define size 10void Push();
void Pop();
void Display();
int CheckStackFull();
int CheckStackEmpty();
struct StackArray
{
int array[size],top;
}
ptr;
int main()
{
int choice,contin;
ptr.top=-1;
do
{
printf("Enter\n1 - Push\n2 - Pop\n3 - Display\n4 - Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Display();
break;
case 4: exit(0);
break;
default: printf("Enter Valid Choice\n");
break;
}
printf("Enter 1 to continue otherwise any other number\n");
scanf("%d",&contin);
}
while(contin==1);
return(0);
}
void Push()
{
int flag=CheckStackFull();
if(flag==1)
{
int value;
printf("Enter value to push into stack\n");
scanf("%d",&value);
ptr.top+=1;
ptr.array[ptr.top]=value;
}
else
{
printf("Stack is Full\n");
}
}
void Pop()
{
int flag=CheckStackEmpty();
if(flag==1)
{
printf("Popped Element is %d\n",ptr.array[ptr.top]);
ptr.top-=1;
}
else
{
printf("Stack is Empty\n");
}
}
void Display()
{
int i,flag=CheckStackEmpty();
if(flag==1)
{
printf("Elements present in stack\n");
for(i=ptr.top;i>-1;i--) printf("%d\n",ptr.array[i]);
printf("\n");
}
else
{
printf("Stack is Empty\n");
}
}
int CheckStackFull()
{
if(ptr.top==size-1) return 0;
else return 1;
}
int CheckStackEmpty()
{
if(ptr.top==-1) return 0;
else return 1;
}