-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_1.cpp
258 lines (246 loc) · 6.55 KB
/
code_1.cpp
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
//
// code_1.cpp
// Problems
//
// Created by Mohd Shoaib Rayeen on 22/03/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
class SNode {
/*
objective: 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 of snode type
*/
private:
string elem;
SNode* next;
friend class SLinkedList; // provides SLinkedList access to SNode
};
class SLinkedList {
/*
objective: 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
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
void mainloop();
private:
SNode* head; // pointer to the head of list
};
SLinkedList::SLinkedList() {
/*
objective: constructor of class
input parameters: none
output value: none
approach: initializing head as NULL
*/
head = NULL;
}
SLinkedList:: ~SLinkedList() {
/*
objective: destructor of class
input parameters: none
output value: none
approach: deleting each element of class
*/
if( head == NULL ) {
return;
}
while ( head->next != NULL ) {
SNode * temp = head;
head = head->next;
delete temp;
}
delete head;
}
bool SLinkedList:: empty() const {
/*
objective: empty()
input parameters: none
output value: none
approach: checking List is empty or not by comparing with head
*/
if ( head == NULL ) {
return true;
}
return false;
}
const string& SLinkedList::front() const {
/*
objective: accessing front element of linked list
input parameters: none
output value: data of head
approach: returning data of head
*/
return head->elem;
}
void SLinkedList::addFront(const string & e){
/*
objective: adding element from front
input parameters: new value which has to inserted
output value: none
approach: updating head after adding node
*/
SNode* newNode = new SNode;
newNode->next = NULL;
newNode->elem = e;
if( empty()) {
head = newNode;
print();
return;
}
newNode->next = head;
head = newNode;
print();
}
void SLinkedList::addBack(const string &e ) {
/*
objective: adding element at the back of list
input parameters: new value which has to inserted
output value: none
approach: Linked new node at the end ( using iteration )
*/
SNode* newNode = new SNode;
newNode->next = NULL;
newNode->elem = e;
if(empty()) {
head = newNode;
print();
return;
}
SNode* temp = head;
while(temp->next != NULL ) {
temp = temp->next;
}
temp->next = newNode;
print();
}
void SLinkedList::removeFront() {
/*
objective: removing element from front
input parameters: none
output value: none
approach: updating head after deletion
*/
if( empty() ) {
cout << "\nList is Empty.\n";
return;
}
SNode* temp = head;
head = head->next;
cout << "\nDeleted Element\t:\t" << temp->elem;
delete temp;
print();
}
void SLinkedList::removeEnd() {
/*
objective: deleting at the end
input parameters: none
output value: none
approach: using iteration
*/
if( empty() ) {
cout << "\nList is Empty.\n";
return;
}
SNode* temp = head;
if(head->next == NULL ) {
removeFront();
return;
}
while(temp->next->next != NULL ) {
temp = temp->next;
}
SNode* p = temp->next;
temp->next = NULL;
cout << "\nDeleted Element\t:\t" << p->elem;
delete p;
print();
}
void SLinkedList::print() {
/*
objective: printing Linked List
input parameters: none
output value: none
approach: using iteration
*/
if(empty()) {
cout << "\nList is Empty.\n";
return;
}
SNode* temp = head;
cout << "\nList\n";
while(temp->next != NULL ) {
cout << temp->elem << "\n";
temp = temp->next;
}
cout << temp->elem << "\n";
}
void SLinkedList::mainloop() {
/*
objective: used for less interaction with main
input parameters: none
output value: none
approach: accessing each functions in switch case
*/
string str;
char choice;
bool done = true;
while ( done ) {
cout << "\n\t\tSingle Linked List Menu";
cout << "\n1. Insertion at the beginning";
cout << "\n2. Insertion at the end";
cout << "\n3. Deletion from the beginning";
cout << "\n4. Deletion from the end";
cout << "\n5. Front data of the list";
cout << "\n6. Exit";
cout << "\nEnter Your Choice\t:\t";
cin >> choice;
switch (choice) {
case '1': cout << "\nEnter String\t:\t";
cin >> str;
addFront(str);
break;
case '2': cout << "\nEnter String\t:\t";
cin >> str;
addBack(str);
break;
case '3': removeFront();
break;
case '4': removeEnd();
break;
case '5': if(!empty()) {
cout << "\nFront of List\t:\t" << front();
break;
}
cout << "\nList is Empty\n";
break;
case '6': done = false;
break;
default: cout << "\nWrong Input\n";
break;
}
}
}
int main() {
SLinkedList obj;
obj.mainloop();
return 0;
}