- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_using_array.c
More file actions
Latest commit
102 lines (92 loc) · 2.15 KB
/
Copy pathstack_using_array.c
File metadata and controls
102 lines (92 loc) · 2.15 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
#include<stdio.h>
#include<stdlib.h>
//structure of a stack.
structStack{
int*array;
intsize;
inttop;
};
typedefstructStackStack;
//function-definitions;
intmainMenu();
voidpush(Stack*s);
intpop(Stack*s);
voiddisplay(constStack*s);
intmain(){
Stack*s= (Stack*)malloc(sizeof(Stack));
s->top=-1;
printf("\nEnter size of the stack: ");
scanf("%d", &(s->size));
s->array= (int*)calloc(s->size, sizeof(int));
if (s->array==NULL) {
printf("\n:( Memory not allocated.\n");
exit(0);
}
printf("\n:) Stack of size '%d' is successfully Created.\n", s->size);
intchoice;
do {
choice=mainMenu();
switch (choice) {
case1:
push(s);
break;
case2:
pop(s);
break;
case3:
display(s);
break;
case4:
free(s->array);
free(s);
break;
default:
printf("\n:( Enter valid operation number.\n");
}
}
while(choice!=4);
return0;
}
intmainMenu(){
intchoice;
printf("\nSelect a choice\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("\nEnter Choice: ");
scanf("%d", &choice);
returnchoice;
}
voidpush(Stack*s){
if(s->top==s->size-1){
printf("\n:( Stack Overflow.\n");
return;
}
intn;
printf("\nEnter Element to push: ");
scanf("%d", &n);
s->array[++(s->top)] =n;
printf("\n:) PUSH SUCCESSFUL.\n");
}
intpop(Stack*s){
if(s->top==-1){
printf("\n:( Stack Underflow.\n");
return-1;
}
printf("\n:) POP SUCCESSFUL.\nDATA VALUE: '%d'\n", s->array[s->top]);
returns->array[(s->top)--];
}
voiddisplay(constStack*s){
if(s->top==-1){
printf("\n:( Stack is empty.\n");
return;
}
printf("\n");
for(inti=0 ; i<=s->top ; i++){
printf("%d,", s->array[s->top-i]);
}
printf("\n^");
printf("\nTOP\n");
}
//By - Shubham 219301072 B.Tech Sec-3C (CSE);