-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSinglyLL.cpp
More file actions
273 lines (235 loc) · 5.71 KB
/
SinglyLL.cpp
File metadata and controls
273 lines (235 loc) · 5.71 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
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class SNode {
/*
objective: Create a class for a Node for Single Linked list
input parameters: none
output value: none
description: SNode class defines the node structure
approach: Class defines data item is names element with datatype string
and link is named next pf snode type
*/
private:
string elem;
SNode* next;
friend class SLinkedList; // provides SLinkedList access to SNode
};
class SLinkedList {
/*
objective: Create a Single LInked List class
input parameters: none
output value: none
side effects: Class SlinkedList defines Single Linked LIst class
approach: Class definition
*/
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
SNode* getNode(const string& e); // creates a new SNode and returns its pointer
bool empty() const; // is list empty?
const string& front() const; // get front element
void addFront(const string& e); // add to front of list
void addBack(const string& e); // add to back of list
void removeFront(); // remove from front
void removeEnd(); // remove from end
void print(); // prints the SLL
private:
SNode* head; // pointer to the head of list
};
SLinkedList::SLinkedList(){
/*
Objective: To construct an object of class SLinkedList and initialize head with NULL.
Input: None
Output: None
Return value: None
*/
head = NULL;
}
SLinkedList::~SLinkedList(){
/*
Objective: To destruct the object of class SLinkedList.
Input: None
Output: None
Return value: None
*/
SNode* curr = head;
while(curr != NULL){
SNode* next = curr->next;
delete curr;
curr = next;
}
head = NULL;
}
SNode* SLinkedList::getNode(const string& e){
/*
Objective: To create a new object of class SNode, initialize its members and return its pointer.
Input: A string
Output: None
Return value: A Pointer to the object of class SNode.
*/
SNode* temp = new SNode();
temp->elem = e;
temp->next = NULL;
return temp;
}
bool SLinkedList::empty() const{
/*
Objective: To test whether the list is empty.
Input: None
Output: None
Return value: true, if list is empty
false, if list is not empty.
*/
if(head==NULL)
return true;
else
return false;
}
const string& SLinkedList::front() const{
/*
Objective: To return the front element of the list.
Input: None
Output: None
Return value: element at the front, if list is not empty
NULL, if list is empty
*/
if(!empty())
return head->elem;
}
void SLinkedList::addFront(const string& e){
/*
Objective: To insert a node at the front of list.
Input: e -> The element to be inserted.
Output: None
Return value: None
*/
SNode* temp = getNode(e);
if(head == NULL)
head = temp;
else{
temp->next = head;
head = temp;
}
}
void SLinkedList::addBack(const string& e){
/*
Objective: To insert a node at the end of list.
Input: e -> The element to be inserted.
Output: None
Return value: None
*/
SNode* temp = getNode(e);
if(head == NULL)
head = temp;
else{
SNode* t = head;
while(t->next != NULL)
t = t->next;
t->next = temp;
}
}
void SLinkedList::removeFront(){
/*
Objective: To remove a node from the front of list.
Input: None
Output: None
Return value: None
*/
if(!empty()){
SNode* temp = head;
head = head->next;
delete temp;
}
}
void SLinkedList::removeEnd(){
/*
Objective: To remove a node from the end of list.
Input: None
Output: None
Return value: None
*/
if(!empty()){
SNode* curr = head;
if(head->next == NULL)
head = NULL;
else{
SNode* prev = curr;
while(curr->next != NULL){
prev = curr;
curr = curr->next;
}
prev->next = NULL;
}
delete curr;
}
}
void SLinkedList::print(){
/*
Objective: To display the queue
Input: None
Output: Queue data
Return value: None
*/
cout<<"\n\n---------------------------------LIST-------------------------------------------\n";
SNode* curr = head;
while(curr != NULL){
cout<<curr->elem<<"\t";
curr = curr->next;
}
cout<<"\n----------------------------------------------------------------------------------";
}
int main(){
/*
Objective: To perform various singly linked list operations.
*/
SLinkedList sll;
int choice;
string ele;
while(1){
cout<<"\n\n\t\t\tLINKED LIST OPERATIONS\n\n1.INSERT AT FRONT\n2.INSERT AT END\n3.REMOVE FRONT\n4.REMOVE END\n5.SHOW FRONT\n6.PRINT LIST\n7.EXIT\n\n";
cout<<"\nEnter your choice : ";
cin>>choice;
cin.ignore();
switch(choice){
case 1: cout<<"\nEnter string to be inserted: ";
getline(cin,ele);
sll.addFront(ele);
sll.print();
break;
case 2: cout<<"\nEnter string to be inserted: ";
cin>>ele;
sll.addBack(ele);
sll.print();
break;
case 3: if(!sll.empty()){
sll.removeFront();
cout<<"\nElement removed!";
}
else
cout<<"\nList empty!!Cannot remove node!!";
sll.print();
break;
case 4: if(!sll.empty()){
sll.removeEnd();
cout<<"\nElement removed!";
}
else
cout<<"\nList empty!!Cannot remove node!!";
sll.print();
break;
case 5: if(!sll.empty())
cout<<"\nFront element: "<<sll.front();
else
cout<<"\nList is empty!!";
break;
case 6: sll.print();
break;
case 7: exit(0);
default: cout<<"Invalid choice!!";
break;
}
}
return 0;
}