Skip to content

Commit 3b351fd

Browse files
authored
Merge pull request #26 from ashleymays/add-circular-singly-linked-list
added circular singly linked list implementation
2 parents e3a5402 + cb709c6 commit 3b351fd

File tree

1 file changed

+317
-0
lines changed
  • LinkedList-dataStructure/circularSinglyLL-implementation

1 file changed

+317
-0
lines changed
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
/*
2+
This is a circular singly linked list implementation.
3+
4+
A circular singly linked list is a lot like a regular singly linked list,
5+
except the last node links back to the first node, AKA the head.
6+
*/
7+
8+
#include <iostream>
9+
10+
struct CLLNode
11+
{
12+
CLLNode() : next(nullptr), data(0) {}
13+
CLLNode(int d) : next(nullptr), data(d) {}
14+
~CLLNode() {}
15+
16+
int data;
17+
CLLNode* next; // create a pointer to the next node in the list
18+
};
19+
20+
class CLList
21+
{
22+
private:
23+
int length;
24+
CLLNode* head; // create a pointer to the first node in the list
25+
public:
26+
CLList() : head(nullptr), length(0) {} // create new empty list
27+
28+
~CLList()
29+
{
30+
CLLNode* cur = head; // create temporary node to read through the list
31+
32+
// since a circular list links back to the head at the end, you need to
33+
// count how many elements are in the list using the length
34+
for (int i = 0; i < length; ++i)
35+
{
36+
CLLNode* node = cur->next;
37+
delete cur;
38+
cur = node;
39+
}
40+
}
41+
42+
43+
44+
45+
int getLength()
46+
{
47+
return length;
48+
}
49+
50+
51+
52+
53+
bool search(int val)
54+
{
55+
CLLNode* cur = head; // temporary node to read through the list
56+
57+
for (int i = 0; i < length; ++i)
58+
{
59+
if (val == cur->data)
60+
{
61+
return true;
62+
}
63+
cur = cur->next; // move on to the next node in the list
64+
}
65+
return false;
66+
}
67+
68+
69+
70+
71+
void insertAtBeg(int val)
72+
{
73+
CLLNode* newNode = new CLLNode(val); // store new value in a new node
74+
75+
// empty list
76+
if (!head)
77+
{
78+
head = newNode;
79+
newNode->next = newNode; // last node links with the head
80+
}
81+
82+
else
83+
{
84+
CLLNode* cur = head; // create temporary node to read through the list
85+
86+
for (int i = 0; i < length - 1; ++i)
87+
{
88+
cur = cur->next;
89+
}
90+
91+
// cur is now the last node in the list
92+
cur->next = newNode;
93+
newNode->next = head;
94+
head = newNode;
95+
}
96+
++length;
97+
}
98+
99+
100+
101+
102+
void insertAtIndex(int val, int i)
103+
{
104+
// list starts at index 0
105+
106+
// invalid index to insert at
107+
if (i < 0 || i > length)
108+
{
109+
std::cout << "Invalid index." << std::endl;
110+
return;
111+
}
112+
113+
// inserting at index 0 is the same as inserting at the beginning
114+
else if (i == 0)
115+
{
116+
insertAtBeg(val);
117+
}
118+
119+
// inserting at this index is the same as inserting at the end
120+
else if (i == length)
121+
{
122+
insertAtEnd(val);
123+
}
124+
125+
else
126+
{
127+
CLLNode* newNode = new CLLNode(val); // store new value in a new node
128+
CLLNode* cur = head; // create temporary node to read through the list
129+
130+
for (int j = 0; j < i - 1; ++j)
131+
{
132+
cur = cur->next;
133+
}
134+
135+
// cur is now the node before the insertion point
136+
newNode->next = cur->next;
137+
cur->next = newNode;
138+
++length;
139+
}
140+
}
141+
142+
143+
144+
145+
void insertAtEnd(int val)
146+
{
147+
CLLNode* newNode = new CLLNode(val); // store new value in a new node
148+
149+
// empty list
150+
if (!head)
151+
{
152+
head = newNode;
153+
newNode->next = newNode;
154+
}
155+
156+
else
157+
{
158+
CLLNode* cur = head; // create temporary node to read through the list
159+
for (int i = 0; i < length - 1; ++i)
160+
{
161+
cur = cur->next;
162+
}
163+
164+
// cur is now the last element in the list
165+
cur->next = newNode;
166+
newNode->next = head; // last node links with the head
167+
}
168+
++length;
169+
}
170+
171+
172+
173+
174+
void deleteAtBeg()
175+
{
176+
// empty list
177+
if (!head)
178+
{
179+
return;
180+
}
181+
182+
// only one element in list
183+
else if (!head->next)
184+
{
185+
delete head;
186+
head = nullptr;
187+
}
188+
189+
else
190+
{
191+
CLLNode* tmp = head;
192+
head = head->next;
193+
delete tmp;
194+
}
195+
--length;
196+
}
197+
198+
199+
200+
201+
void deleteAtIndex(int i)
202+
{
203+
// list starts at index 0
204+
205+
// invalid index to delete at
206+
if (i < 0 || i >= length)
207+
{
208+
std::cout << "Invalid index." << std::endl;
209+
return;
210+
}
211+
212+
// deleting at index 0 is the same as delete the first node
213+
else if (i == 0)
214+
{
215+
deleteAtBeg();
216+
}
217+
218+
// deleting at this index is the same as deleting at the last node
219+
else if (i == length - 1)
220+
{
221+
deleteAtEnd();
222+
}
223+
224+
else
225+
{
226+
CLLNode* cur = head; // temporary node to read through the list
227+
228+
for (int j = 0; j < i - 1; ++j)
229+
{
230+
cur = cur->next;
231+
}
232+
233+
// cur is now the node before the deletion point
234+
235+
CLLNode* tmp;
236+
tmp = cur->next->next; // store the node after the deletion point
237+
delete cur->next; // free memory at the deletion point
238+
cur->next = tmp;
239+
--length;
240+
}
241+
}
242+
243+
244+
245+
246+
void deleteAtEnd()
247+
{
248+
// empty list
249+
if (!head)
250+
{
251+
return;
252+
}
253+
254+
// only one element in list
255+
else if (!head->next)
256+
{
257+
delete head;
258+
head = nullptr;
259+
}
260+
261+
else
262+
{
263+
CLLNode* cur = head; // temporary node to read through the list
264+
265+
for (int i = 0; i < length - 2; ++i)
266+
{
267+
cur = cur->next;
268+
}
269+
270+
// cur->next is now the last node in the list
271+
delete cur->next;
272+
cur->next = head;
273+
}
274+
--length;
275+
}
276+
277+
278+
279+
280+
void display() {
281+
CLLNode* cur = head; // temporary node to read through the list
282+
283+
// since a circular list links back to the head at the end, you need to
284+
// count how many elements are in the list using the length
285+
std::cout << "Data: ";
286+
287+
for (int i = 0; i < length; ++i)
288+
{
289+
std::cout << cur->data << ' ';
290+
cur = cur->next;
291+
}
292+
293+
std::cout << std::endl;
294+
}
295+
};
296+
297+
298+
int main() {
299+
CLList list = CLList();
300+
301+
list.insertAtEnd(1);
302+
list.insertAtEnd(2);
303+
list.insertAtEnd(4);
304+
list.insertAtEnd(5);
305+
306+
list.display();
307+
308+
list.insertAtBeg(0);
309+
310+
list.display();
311+
312+
list.deleteAtEnd();
313+
314+
list.display();
315+
316+
return 0;
317+
}

0 commit comments

Comments
 (0)