Circular Doubly linked list Program in cpp
Doubly Circular Linked List |C++ ImplementationAbhishek Singh Thakur Follow Feb 2, 2018 · 6 min read The nodes in a linked list are connected through pointers. Pointers represent the address of a location in a memory. The order in a linked list is determined by a pointer in each node. A node in a doubly circular linked list contains a data item and two node pointers, one to the previous node and one to the next node. In doubly linked list we can traverse in both direction. Show Related: Doubly Linked List Here is a meme to understand Circular Linked List. The first node of the linked list is the head and the last node is the tail. If head is NULL then the list is empty. In C++, a node can be defined using struct, which contain a data item and node pointers. struct Node{ T data; Node * next; Node * prev; Node(T value) : data(std::move(value)), next(nullptr), prev(nullptr) {} }; Node(T val): data(val), next(nullptr), prev(nullptr) {} is the constructor for the struct Node which is used to initialise data, next and prev. T means it is a generic struct and data can store values of all data types. To declare head and tail: Node *head, *tail; In the above fig. Node containing 5 is head and node containing 15 is tail. prev pointer in head points to the last node and next pointer in tail points to the head. The three basic operations supported by a linked list are searching, insertion and deletion. SearchingIn the search function a value is passed as an argument and its node is returned if found, otherwise a message says "No such element in the list" and nullptr is returned. The function starts searching from the head to the last node and passed value is matched with every nodes data item. Here is the code for iterative search. { Node *node = head; while(node->next != head) { if(node->data == value) { return node; } node = node->next; } if(node->data == value) { return node; } std::cerr << "No such element in the list \n"; return nullptr; } Insertioninsert function insert a node with the value at the end of the linked list. If the linked list does not contain any node then the new node becomes head and tail otherwise new node is added after tail and its prev pointer points to tail and next pointer points to head. Then new node becomes tail. void insert(T value){ Node *node = new Node(value); if(head == nullptr) { node->next = node; node->prev = node; head = node; tail = node; } tail = head->prev; tail->next = node; node->prev = tail; node->next = head; head->prev = node; tail = node; } DeletionIn deleteNode function the value is entered which is to be deleted. The function search the node containing the value using search function and then the node is deleted. If the searched node is head then node next to head is made head and then the searched node is deleted. The node is deleted only if the value exists means if (node != nullptr). After deletion next and prev pointer of previous and next nodes are updated. void deleteNode(T value){ Node *node = search(value); if(node == nullptr) { std::cerr << "No such value in the list\n"; return; } else { Node *tmp = head; Node *tail = head->prev; if(tmp == node) { tail->next = tmp->next; tmp->prev->next->prev = tail; head = tail->prev; delete tmp; return; } else if(tail == node) { Node *curr = tail; tmp = tail->prev; tmp->next = curr->next; head->prev = tmp; tail = tmp; delete curr; return; } else { while(tmp->next != head) { if(tmp == node) { tmp->prev->next = tmp->next; tmp->prev->next->prev = tmp->prev; delete tmp; return; } tmp = tmp->next; } } } } C++ Implementation of Circular Doubly Linked ListIn C++ implementation of this program we have copy constructor, move constructor, copy assignment operator, move assignment operator and destructor. Because the presence of a user-defined destructor, copy-constructor, or copy-assignment operator prevents implicit definition of the move constructor and the move assignment operator, any class for which move semantics are desirable, has to declare all five special member functions. This is called Rule of Five. #include template class circular_doubly_linked_list { struct Node { T data; Node * next; Node * prev; Node(T value) : data(std::move(value)), next(nullptr), prev(nullptr) {} }; Node *head, *tail; public: circular_doubly_linked_list() : head(nullptr), tail(nullptr) {} //copy constructor circular_doubly_linked_list(const circular_doubly_linked_list &); //copy assignment circular_doubly_linked_list& operator=(const circular_doubly_linked_list& cdll) { circular_doubly_linked_list temp(cdll); temp.swap(*this); return *this; } //move constructor circular_doubly_linked_list(circular_doubly_linked_list&&) noexcept; //move assignment circular_doubly_linked_list& operator=(circular_doubly_linked_list&& cdll) noexcept { cdll.swap(*this); return *this; } ~circular_doubly_linked_list(); void append_node(T); void delete_node(T); friend void swap(circular_doubly_linked_list& lhs, circular_doubly_linked_list& rhs) { std::swap(lhs.head, rhs.head); } template friend std::ostream & operator<<(std::ostream & os, const circular_doubly_linked_list & cdll) { cdll.print_list(os); return os; } private: struct Node *search(T value) { Node *node = head; while(node->next != head) { if(node->data == value) { return node; } node = node->next; } if(node->data == value) { return node; } std::cerr << "No such element in the list \n"; return nullptr; } void print_list(std::ostream& os = std::cout) const { Node *tmp = head; while(tmp->next != head) { std::cout << tmp->data << ' '; tmp = tmp->next; } std::cout << tmp->data << '\n'; } }; template circular_doubly_linked_list { if(cdll.head == nullptr) { head = tail = nullptr; } else { head = new Node(cdll.head->data); Node *curr = head; Node *tmp = head; Node *obj_curr = cdll.head; while(obj_curr->next != cdll.head) { curr->next = new Node(obj_curr->next->data); obj_curr = obj_curr->next; curr = curr->next; curr->prev = tmp; tmp = tmp->next; } tail = curr; curr->next = head; head->prev = curr; } } template circular_doubly_linked_list { head = tail = nullptr; swap(*this, cdll); } template void circular_doubly_linked_list { Node *node = new Node(std::move(value)); if(head == nullptr) { node->next = node; node->prev = node; head = node; tail = node; } tail = head->prev; tail->next = node; node->prev = tail; node->next = head; head->prev = node; tail = node; } template void circular_doubly_linked_list { Node *node = search(value); if(node == nullptr) { std::cerr << "No such value in the list\n"; return; } else { Node *tmp = head; Node *tail = head->prev; if(tmp == node) { tail->next = tmp->next; tmp->prev->next->prev = tail; head = tail->prev; delete tmp; return; } else if(tail == node) { Node *curr = tail; tmp = tail->prev; tmp->next = curr->next; head->prev = tmp; tail = tmp; delete curr; return; } else { while(tmp->next != head) { if(tmp == node) { tmp->prev->next = tmp->next; tmp->prev->next->prev = tmp->prev; delete tmp; return; } tmp = tmp->next; } } } } template circular_doubly_linked_list { if(head) { Node *tmp = head; while(tmp->next != head) { Node *t = tmp; tmp = tmp->next; delete t; } delete tmp; head = nullptr; } } int main() { circular_doubly_linked_list cdll1.append_node(3); cdll1.append_node(4); cdll1.append_node(5); cdll1.append_node(6); cdll1.append_node(7); cdll1.append_node(8); std::cout << cdll1; cdll1.delete_node(6); std::cout << cdll1; circular_doubly_linked_list std::cout << "Linked List 2: " << cdll2; circular_doubly_linked_list std::cout << "Linked List 3: " << cdll3; circular_doubly_linked_list std::cout << "Linked list 4: " << cdll4; } View this code on Github. Reference: You may also likeMove all Odd numbers after Even numbers in Singly Linked List Follow me on Facebook and Twitter Originally published at https://programmercave0.github.io on February 2, 2018. |