-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
269 lines (192 loc) · 5.21 KB
/
linkedlist.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
259
260
261
262
263
264
265
266
267
268
269
// implementation of simple singly linked list in C++
//THERE IS A NEWER version of singly linked list in this repo
#include<iostream>
#include<string>
class LinkedList
{
private:
//basis structure for a node
struct Node;
//node count
int count;
//pointer pointing to first node
Node* first;
//pointer pointing to last node
Node* last;
public:
//defalt constructor creates a list with no nodes
LinkedList ();
//this constructor creates list with one node, which data value equals to the argumet
explicit LinkedList (int);
//explicitly disable copy constructor
LinkedList (const LinkedList&) = delete;
//explicitly disable copy assignment operator
LinkedList& operator = (const LinkedList&) = delete;
//explicitly disable move constructor
LinkedList (LinkedList&&) = delete;
//explicitly disable move assignment operator
LinkedList& operator = (LinkedList&&) = delete;
virtual ~LinkedList();
int getFirstItem () const;
int getLastItem () const;
void insertHead (int val);
void insertTail (int val);
int itemsCount () const;
//this method returns a string of node data values
std::string getValues () const;
//this method searchers for a particular value in the list
//returns true if the value is found and false if not
bool search (int val) const;
//this method inserts a node with data value equaled to the second argument
//after a node with data value equled to the first argumet
//if no node with data value equled to the first argumet is found, there is no insertion
void insertAfter (int val1, int val2);
void removeHead ();
};
int main ()
{
LinkedList myLinkedList(5);
myLinkedList.insertHead(10);
myLinkedList.insertTail(200);
myLinkedList.insertTail(247);
std::cout << myLinkedList.getFirstItem() << std::endl;
std::cout << myLinkedList.getLastItem() << std::endl;
std::cout << myLinkedList.itemsCount() << std::endl;
std::cout << "Items: " << myLinkedList.getValues() << std::endl;
int variable = 25;
if(myLinkedList.search(variable))
{
std::cout << "Item " << variable << " is in the linked list" << std::endl;
}
else
{
std::cout << "Item " << variable << " is NOT in the linked list" << std::endl;
}
int variable2 = 247;
if(myLinkedList.search(variable2))
{
std::cout << "Item " << variable2 << " is in the linked list" << std::endl;
}
else
{
std::cout << "Item " << variable2 << " is NOT in the linked list" << std::endl;
}
myLinkedList.insertAfter(200, 1000);
myLinkedList.insertHead(8888);
std::cout << "Items: " << myLinkedList.getValues() << std::endl;
myLinkedList.insertTail(22222);
std::cout << "Items: " << myLinkedList.getValues() << std::endl;
myLinkedList.removeHead();
std::cout << "Items: " << myLinkedList.getValues() << std::endl;
LinkedList mll2(12333);
std::cout << mll2.getValues() << std::endl;
mll2.insertAfter(12333, 55555);
std::cout << mll2.getValues() << std::endl;
mll2.insertAfter(55555, 2222222);
std::cout << mll2.getValues() << std::endl;
mll2.removeHead();
std::cout << mll2.getValues() << std::endl;
return 0;
}
struct LinkedList::Node
{
int item;
Node* next;
};
LinkedList::LinkedList (): first{nullptr}, last{nullptr}, count{0} {}
LinkedList::LinkedList (int val)
{
Node* ptr = new Node;
ptr->item = val;
ptr->next = nullptr;
first = ptr;
last = ptr;
count = 1;
}
LinkedList::~LinkedList()
{
Node* current = nullptr;
Node* temp = first;
while (temp)
{
current = temp;
temp = temp->next;
delete current;
}
}
int LinkedList::getFirstItem () const
{
return first->item;
}
int LinkedList::getLastItem () const
{
return last->item;
}
void LinkedList::insertHead (int val)
{
Node* ptr = new Node;
ptr->next = first;
first = ptr;
ptr->item = val;
count++;
}
void LinkedList::insertTail (int val)
{
Node* ptr = new Node;
ptr->next = nullptr;
last->next = ptr;
last = ptr;
count++;
ptr->item = val;
}
int LinkedList::itemsCount () const
{
return count;
}
std::string LinkedList::getValues () const
{
Node* temp = first;
std::string str = "";
while(temp)
{
str += std::to_string(temp->item) + " ";
temp = temp->next;
}
return str;
}
bool LinkedList::search (int val) const
{
if (count == 0) return false;
Node* temp = first;
while(temp)
{
if(temp->item==val) return true;
temp = temp->next;
}
return false;
}
void LinkedList::insertAfter (int val1, int val2)
{
if (count == 0) return;
Node* temp = first;
while(temp)
{
if(temp->item==val1)
{
Node* ptr = new Node;
ptr->item = val2;
ptr->next = temp->next;
temp->next = ptr;
count++;
}
temp = temp->next;
}
}
void LinkedList::removeHead ()
{
if (count == 0) return;
Node* temp = first;
first = temp->next;
delete temp;
count--;
}