-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_implementation_Practice.c
More file actions
116 lines (77 loc) · 1.67 KB
/
Copy pathstack_implementation_Practice.c
File metadata and controls
116 lines (77 loc) · 1.67 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
struct array_stack{
int top,capacity;
int *arr;
};
struct array_stack* create_stack(int cap){
struct array_stack *stack;
stack=(struct array_stack*)malloc(sizeof(struct array_stack));
stack->capacity=cap;
stack->top=-1;
stack->arr=(int*)malloc(sizeof(int)*cap);
return stack;
}
int isfull(struct array_stack *stack){
if(stack->top==stack->capacity-1)
return 1;
else
{
return 0;
}
}
int isempty(struct array_stack *stack){
if(stack->top==-1)
return 1;
else
return 0;
}
void PUSH(struct array_stack *stack,int item){
if(!isfull(stack)){
stack->top++;
stack->arr[stack->top]=item;
}
}
int POP(struct array_stack *stack){
int item;
if(!isempty(stack)){
item=stack->arr[stack->top];
stack->top--;
return item;
}
else
return -1;
}
int main(void){
int item,choice,size;
struct array_stack *stack;
printf("INPUT THE SIZE OF ARRAY YOU WANT :\n");
scanf("%d",&size);
stack=create_stack(size);
while(1){
printf("INPUT YOUR CHOICE :\n");
printf("INPUT 1 FOR PUSH.\n");
printf("INPUT 2 FOR POP.\n");
printf("INPUT 3 FOR EXIT\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Input the value to be pushed !\n");
scanf("%d",&item);
PUSH(stack,item);
break;
case 2:
item=POP(stack);
if(item==-1)
printf("The stack is already empty!\n");
else
printf("The popped item is : %d\n",item);
break;
case 3:
free(stack);
exit(0);
break;
}
}
return 0;
}