Created
March 10, 2024 20:00
-
-
Save mudassaralichouhan/bcc7229b3d7dbb5e2b32ff1717b632b9 to your computer and use it in GitHub Desktop.
It is a linked list whose nodes are connected in such a way that it forms a circle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "iostream" | |
using namespace std; | |
struct Node { | |
int id; | |
string name; | |
Node *next; | |
Node(unsigned int id, string name, Node *next = nullptr) : | |
id(id), name(name), next(next) {} | |
}; | |
class CLL { | |
private: | |
Node *head = nullptr; | |
Node *tail = nullptr; | |
public: | |
void insert_at_start(unsigned int id, string name) { | |
Node *n = new Node(id, name); | |
if (!head) { | |
head = n; | |
tail = n; | |
n->next = head; | |
return; | |
} | |
n->next = head; | |
head = n; | |
tail->next = head; | |
} | |
bool insert_after(unsigned int needle, unsigned int id, string name) { | |
Node *tmp = head; | |
while (tmp) { | |
if (tmp->id == needle) | |
break; | |
tmp = tmp->next; | |
if (tmp == tail) { | |
break; | |
} | |
} | |
if (head == tmp) { | |
Node *n = new Node(id, name, head->next); | |
head->next = n; | |
return true; | |
} else if (tail == tmp) { | |
insert_at_end(id, name); | |
return true; | |
} | |
if (tmp) { | |
Node *n = new Node(id, name, tmp->next); | |
tmp->next = n; | |
return true; | |
} | |
return false; | |
} | |
bool insert_before(unsigned int needle, unsigned int id, string name) { | |
Node *tmp = head; | |
Node *prev = nullptr; | |
while (tmp) { | |
if (tmp->id == needle) | |
break; | |
prev = tmp; | |
tmp = tmp->next; | |
if (tmp == tail) { | |
break; | |
} | |
} | |
if (head == tmp) { | |
insert_at_start(id, name); | |
return true; | |
} else if (tail == tmp) { | |
Node *n = new Node(id, name, prev->next); | |
prev->next = n; | |
tail = n->next; | |
return true; | |
} | |
if (tmp) { | |
Node *n = new Node(id, name, prev->next); | |
prev->next = n; | |
return true; | |
} | |
return false; | |
} | |
void insert_at_end(unsigned int id, string name) { | |
if (head == nullptr) | |
return insert_at_start(id, name); | |
Node *n = new Node(id, name); | |
Node *tmp = head; | |
while (tmp != tail) | |
tmp = tmp->next; | |
tmp->next = n; | |
n->next = head; | |
tail = n; | |
} | |
bool delete_node(unsigned int needle) { | |
if (head == nullptr) | |
return false; | |
Node *tmp = head; | |
Node *prv = nullptr; | |
while (tmp) { | |
if (tmp->id == needle) | |
break; | |
if (tmp->next == head) { | |
tmp = nullptr; | |
break; | |
} | |
prv = tmp; | |
tmp = tmp->next; | |
} | |
if (tmp == head) { | |
Node *t = head; | |
head = head->next; | |
delete t; | |
return true; | |
} else if (tmp == tail) { | |
tail = prv; | |
delete tmp; | |
return true; | |
} else if (tmp) { | |
delete prv->next; | |
prv->next = tmp->next; | |
return true; | |
} | |
return false; | |
} | |
Node *search(unsigned int id) { | |
Node *tmp = head; | |
while (tmp) { | |
if (id == tmp->id) | |
return tmp; | |
tmp = tmp->next; | |
if (tmp->next == head) | |
break; | |
} | |
return nullptr; | |
} | |
bool update(unsigned int needle, unsigned int id, string name) { | |
if (head == nullptr) | |
return false; | |
Node *tmp = head; | |
while (tmp && tmp->next != head) { | |
if (tmp->id == needle) { | |
tmp->id = id; | |
tmp->name = name; | |
return true; | |
} | |
tmp = tmp->next; | |
} | |
return false; | |
} | |
void sort() { | |
if (head == nullptr) | |
return; | |
Node *tmp = nullptr; | |
bool sort = false; | |
do { | |
Node *prev = nullptr; | |
tmp = head; | |
sort = false; | |
while (tmp->next != head) { | |
if (tmp->id > tmp->next->id) { | |
if (tmp == head) { | |
head = head->next; | |
} else { | |
Node *t = prev->next; | |
prev->next = tmp->next; | |
Node *t1 = prev->next->next; | |
prev->next->next = t; | |
t->next = t1; | |
} | |
sort = true; | |
} | |
prev = tmp; | |
tmp = tmp->next; | |
} | |
} while (sort); | |
tail = tmp; | |
} | |
void print() { | |
if (!head) | |
return; | |
Node *tmp = head; | |
do { | |
cout << tmp->id << "\t" << tmp->name << endl; | |
tmp = tmp->next; | |
} while (tmp != head); | |
} | |
~CLL() { | |
Node *temp = head; | |
while (temp != nullptr) { | |
Node *next = temp->next; | |
delete temp; | |
temp = next; | |
if (temp == head) | |
break; | |
} | |
head = nullptr; | |
} | |
}; | |
int main() { | |
CLL cll; | |
cll.insert_at_end(3, "three"); | |
cll.insert_at_end(10, "one"); | |
cll.insert_at_end(9, "one"); | |
cll.insert_at_end(1, "one"); | |
cll.insert_at_end(2, "one"); | |
cll.insert_at_end(3, "one"); | |
cll.insert_at_end(4, "one"); | |
cll.insert_at_end(5, "one"); | |
cll.insert_at_end(8, "one"); | |
cll.insert_at_start(2, "two"); | |
cll.insert_at_end(4, "four"); | |
cll.insert_before(4, 0, "Zero"); | |
cll.insert_before(2, -1, "native one"); | |
cll.insert_before(1, 6, "six"); | |
cll.insert_after(-1, 10, "ten"); | |
cll.insert_after(4, 7, "seven"); | |
cll.insert_after(1, 8, "eight"); | |
cll.insert_before(1, 9, "nine"); | |
cll.delete_node(1); | |
cll.delete_node(4); | |
cll.update(3, 11, "Eleven"); | |
cll.print(); | |
Node *search = cll.search(2); | |
if (search) | |
cout << "searched: " << search->id << endl; | |
else | |
cout << "search not found\n"; | |
cll.sort(); | |
cout << endl; | |
cll.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why linked list data structure needed? Single Linked List
Here are a few advantages of a linked list that is listed below, it will help you understand why it is necessary to know.
Why Circular linked list?
In a singly linked list, for accessing any node of the linked list, we start traversing from the first node. If we are at any node in the middle of the list, then it is not possible to access nodes that precede the given node. This problem can be solved by slightly altering the structure of a singly linked list. In a singly linked list, the next part (pointer to the next node) of the last node is NULL. If we utilize this link to point to the first node, then we can reach the preceding nodes.