-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_Stack_Using_LL.c
More file actions
175 lines (163 loc) · 2.74 KB
/
19_Stack_Using_LL.c
File metadata and controls
175 lines (163 loc) · 2.74 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
19
Stack using Linked List
Name: Sayooj K
Roll no: 45
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*TOP;
int deleteAll() {
struct node *ptr = TOP, *prevPtr;
while (ptr != NULL) {
prevPtr = ptr;
ptr = ptr->next;
free(prevPtr);
}
TOP->next = NULL;
return (0);
}
struct node *newNode() {
struct node *newptr = malloc(sizeof(struct node));
if (newptr == NULL) {
printf("Memory overflow");
deleteAll();
exit(0);
}
return (newptr);
}
int deleteFirstNode() {
struct node *currentNode = TOP->next;
if (currentNode == NULL) {
printf("Empty stack.\nDeletion failed.\n");
} else {
TOP->next = currentNode->next;
free(currentNode);
}
return (0);
}
int insertNodeFront(int data) {
struct node *newptr = newNode();
newptr->next = TOP->next;
newptr->data = data;
TOP->next = newptr;
return (0);
}
int printLinkedList() {
struct node *currentNode = TOP->next;
printf("\nLinked List\nTOP->");
while (currentNode != NULL) {
printf("%d\n", currentNode->data);
currentNode = currentNode->next;
}
printf("\n");
return (0);
}
int showMenu() {
char choice='1';
int data, previousData, nextData;
do {
printf("\n\tLINKED LIST\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Print\n");
printf("4. Exit\n");
printf("Enter your choice : ");
scanf(" %c", &choice);
printf("\n");
switch (choice) {
case '1':
printf("Enter new data : ");
scanf("%d", &data);
insertNodeFront(data);
break;
case '2':
deleteFirstNode();
break;
case '3':
printLinkedList();
break;
case '4':
deleteAll();
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
} while(choice != '4');
return (0);
}
int main() {
// initialize Linked List
TOP = malloc(sizeof(struct node));
TOP->data = 0;
TOP->next = NULL;
// finished initializing Linked List
showMenu();
deleteAll();
return (0);
}
/*
OUTPUT:
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice :1
Enter new data : 8
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice :1
Enter new data : 9
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice :1
Enter new data : 2
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice :3
Linked List
TOP->2
9
8
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice : 2
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice : 2
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice : 2
LINKED LIST
1. Push
2. Pop
3. Print
4. Exit
Enter your choice : 2
Empty stack.
Deletion failed.
*/