-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack_using_LinkedList.cpp
More file actions
274 lines (251 loc) · 4.87 KB
/
Stack_using_LinkedList.cpp
File metadata and controls
274 lines (251 loc) · 4.87 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define FAILURE -9999999
#define SUCCESS 9999999
using namespace std;
int cnt=0;
typedef struct Node
{
int element;
struct Node * next;
}node;
node* list;
void init()
{
list = 0; //initially set to NULL
}
int insertFirst(int element) //insert at the beginning
{
node * newNode ; // create a new pointer to node
newNode = (node*) malloc (sizeof(node)) ; // allocate memory for a new node instance and assign pointer to it to newNode
newNode->element = element ;// update value of the newNode
if(list==NULL)
{
list=newNode;
newNode->next=NULL;
cnt++;
return SUCCESS;
}
newNode->next = list ; //point to previous first node
list = newNode ;
cnt++; //set list to point to newnode as this is now the first node
return SUCCESS ;
}
int deleteElement(int element)
{
node *temp, *prev ;
temp = list ; //start at the beginning
while (temp != 0)
{
if (temp->element == element) break ;
prev = temp;
temp = temp->next ; //move to next node
}
if (temp == 0) return FAILURE ; //item not found to delete
if (temp == list) //delete the first node
{
list = list->next ;
free(temp) ;
}
else
{
prev->next = temp->next ;
free(temp);
}
return SUCCESS;
}
node* searchItem(int element)
{
node * temp ;
temp = list ; //start at the beginning
while (temp != 0)
{
if (temp->element == element) return temp ;
temp = temp->next ; //move to next node
}
return 0 ; //0 means invalid pointer in C, also called NULL value in C
}
void printList()
{
node * temp;
temp = list;
while(temp!=0)
{
printf("%d\n", temp->element);
temp = temp->next;
}
printf("\n");
}
/**
Insert element at the end of the list
Retrun SUCCESS for successful insertion
**/
int insertLast(int element1)
{
node* newItem;
newItem = (node*) malloc (sizeof(node)) ;
node* temp;
if(list==NULL)
{
list=newItem;
newItem->element=element1;
newItem->next=NULL;
}
else
{
temp=list;
while(temp->next!=NULL)
{
temp=temp->next;
}
newItem->element=element1;
temp->next=newItem;
newItem->next=NULL;
}
//free(temp);
return SUCCESS;
}
/**
Insert newElement after the first occurrence of oldElement
Return SUCCESS for successful insertion
Return FAILURE if oldElement not exist in the list
**/
int insertAfter(int oldElement, int newElement)
{
int flag=0;
node* prev;
node* newItem;
newItem = (node*) malloc (sizeof(node)) ;
newItem->element=newElement;
newItem->next=NULL;
prev=list;
while(prev!=NULL)
{
if(prev->element==oldElement)
{
flag=1;
break;
}
prev=prev->next;
}
if(flag==1)
{
newItem->next=prev->next;
prev->next=newItem;
}
if(flag==0) return FAILURE;
else
return SUCCESS;
}
/**
Delete the last element of the list
Return SUCESS for successful deletion
Return Failure for unsuccessful deletion
**/
int deleteLast()
{
node* prev;
node* cur;
cur=list;
if(list==NULL) return FAILURE;
if(list->next==NULL)
{
list=list->next;
free(cur);
cnt--;
return SUCCESS;
}
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
if(prev!=NULL)
{
prev->next=NULL;
}
free(cur);
cnt--;
return SUCCESS;
}
/**
Delete the first element of the list
Return SUCESS for successful deletion
Return Failure for unsuccessful deletion
**/
int deleteFirst()
{
node* temp;
temp=list;
int ans=temp->element;
if(list!=NULL)
{
list=list->next;
free(temp);
}
cnt--;
return ans;
}
int stack_size()
{
return cnt;
}
void push(int x)
{
insertFirst(x);
}
int pop()
{
int a=deleteFirst();
return a;
}
void print()
{
printList();
}
int main()
{
init();
while(1)
{
printf("1. Push 2. Pop 3. Size 4. Print 5. Top 6. Exit\n");
int choice;
cout<<"enter choice: ";
cin>>choice;
if(choice==1)
{
int x;
cout<<"enter element: ";
cin>>x;
push(x);
print();
}
else if(choice==2)
{
if(stack_size()>0)
{
int a=pop();
cout<<a<<" has been popped"<<endl;
}
else cout<<"empty stack"<<endl;
print();
}
else if(choice==3)
{
cout<<"size is: "<<stack_size()<<endl;
}
else if(choice==4)
{
print();
}
else if(choice==5)
{
if(cnt==0) cout<<"no element in stack"<<endl;
else cout<<list->element<<endl;
}
else if(choice==6)
return 0;
}
return 0;
}