-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_using_array.c
122 lines (112 loc) · 2.51 KB
/
stack_using_array.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
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void push(int[],int);
void pop(int []);
void delStack();
void displayStack(int []);
int stack[MAX],TOP;
int main(){
int choice,data;
TOP = -1;
while(1){
printf("\n\nEnter your choice:\n\n");
printf("1.Display Stack.\n\n2.PUSH an element\n\n3.POP an element\n\n4.DELETE THE STACK\n\n0.EXIT\n\n");
scanf("%d",&choice);
switch(choice){
case 1:
displayStack(stack);
break;
case 2:
printf("\nEnter the data: ");
scanf("%d",&data);
push(stack,data);
break;
case 3:
pop(stack);
break;
case 4:
delStack();
break;
case 0:
exit(0);
break;
default:
printf("Invalid choice\n");
};
}
}
void push(int stack[],int data){
if(TOP == MAX-1){
printf("ERROR: Stack Full");
return; //failsafe
}
else{
++TOP;
stack[TOP]=data;
printf("Insertion successful...\n");
}
//printf("\n\n\nTOP= %d\n\n\n\n",TOP); //debug note
}
void pop(int stack[]){
int itemDeleted, confirm=0;
//printf("\n\n\nTOP= %d\n\n\n\n",TOP); //debug note
if(TOP == -1){
printf("ERROR: Stack Empty");
return; //failsafe
}
else{
itemDeleted = stack[TOP];
printf("Item to be deleted : %d press 1 to confirm ", itemDeleted);
scanf("%d",&confirm);
if(confirm == 1){
--TOP;
printf("DELETION SUCCESSFUL...\n");
}
else{
printf("OPERATION CANCELLED...\n");
}
}
}
void displayStack(int stack[]){
int temp = TOP;
//printf("\n\n\nTOP= %d\n\n\n\n",TOP); //debug note
int i=0;
if(TOP == -1){
printf("\n\nERROR: Stack Empty\n\n");
return; //failsafe
}
else{
while(temp > -1){
if(temp == TOP){
printf("%d<--TOP\n", stack[temp]);
}
else{
printf("%d\n", stack[temp]);
}
--temp;
}
printf("END OF STACK");
}
}
void delStack(){
int confirm=0;
if(TOP == -1){
printf("ERROR: Stack Empty");
return //failsafe
}
else{
printf("YOUR ARE ABOUT TO DELETE THE WHOLE STACK ONCE DELETED\nTHE DATA CANNOT BE RETRIEVED. PRESS 1 TO CONFIRM. \n");
scanf("%d",&confirm);
if(confirm == 1){
printf("DELETING STACK..\n");
while(TOP > -1){
--TOP;
}
printf("Stack deleted... Value of TOP = %d\n",TOP);
}
else{
printf("\nOPERATION CANCELLED");
}
}
}