-
Notifications
You must be signed in to change notification settings - Fork 37
/
stackarray.c
157 lines (116 loc) Β· 2.44 KB
/
stackarray.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdlib.h>
#include<stdio.h>
struct stack{
int total_size;
int top;
int *arr;
};
/* array memory made in stack memory
struct stack{
int total_size;
int top;
int arr[100];
}; */
void createstack(struct stack *st,int total_size){
st->total_size=total_size;
st->top=-1;
st->arr=(int *)malloc(st->total_size * sizeof(int));
}
void push(struct stack *st){
int num;
printf("enter number to push in a stack");
scanf("%d",&num);
if(st->top==st->total_size-1){
printf("stack is full\n");
}else{
st->top=st->top+1;
st->arr[st->top]=num;
}
}
void pop(struct stack *st){
if(st->top==-1){
printf("stack is empty\n");
}else{
int x=st->arr[st->top];
st->top=st->top-1;
printf("deleted element is %d",x);
}
}
void peek(struct stack *st){
if(st->top==-1){
printf("stack is empty\n");
}else{
printf("top element in a stack is %d",st->arr[st->top]);
}
}
void isempty(struct stack *st){
if(st->top==-1){
printf("stack is empty\n");
}else{
printf("stack is not empty\n");
}
}
void isfull(struct stack *st){
if(st->top==st->total_size-1){
printf("stack is full\n");
}else{
printf("stack is not full\n");
}
}
void display(struct stack *st){
if(st->top==-1){
printf("there is no element in stack to display\n");
}
int i;
for(i=st->top;i>=0;i--)
printf("%d\n",st->arr[i]);
}
int menu(){
int choice;
printf("\n1.create stack using array\n");
printf("2.push into stack\n");
printf("3.pop into a stack\n");
printf("4.top most element in stack\n");
printf("5. check if stack is empty or not\n");
printf("6. check if stack is full or not\n");
printf("7.display element in stack\n");
printf("8.exit\n");
printf("enter your choice");
scanf("%d",&choice);
return choice;
}
int main(){
struct stack st;
while(1)
{
switch(menu())
{
case 1:
createstack(&st,10);
break;
case 2:
push(&st);
break;
case 3:
pop(&st);
break;
case 4:
peek(&st);
break;
case 5:
isempty(&st);
break;
case 6:
isfull(&st);
break;
case 7:
display(&st);
break;
case 8:
exit(0);
default:
printf("enter wrong choice\n");
}
}
return 0;
}