Destructor for circular linked list C++

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.

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.

Implementation

The three basic operations supported by a linked list are searching, insertion and deletion.

Searching

In 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.

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 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; }


Deletion

In 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 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 List

In 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 #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 & operatordata == value] { return node; } node = node->next; } if[node->data == value] { return node; } std::cerr next != head] { std::cout 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::circular_doubly_linked_list[circular_doubly_linked_list&& cdll] noexcept { head = tail = nullptr; swap[*this, cdll]; } template void circular_doubly_linked_list::append_node[T value] { 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::delete_node[T value] { Node *node = search[value]; if[node == nullptr] { std::cerr 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::~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; 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

Chủ Đề