A one doubly attached list is a linear data structure, in i beg your pardon the aspects are stored in the type of a node. Every node includes three sub-elements. A data component that shop the worth of the element, the previous part that shop the guideline to the previous node, and also the next component that stores the pointer to the next node as shown in the listed below image:




You are watching: Circular doubly linked list c++

*

The very first node additionally known as HEAD is always used together a recommendation to traverse the list. Last element contains attach to the very first element as next and the first element contains link of the last aspect as previous. A circular doubly linked can be visualized together a chain of nodes, where every node points to previous and next node.


*

Implementation of one Doubly connected List

Representation:

In C, a node can be created using structure. In C++, one doubly linked list deserve to be created using a class and a Node making use of structures. The LinkedList course contains Node as course member.

In Java, Python, C# and PHP, circular doubly linked list deserve to be stood for as a class and also a Node together a different class. The LinkedList class contains a reference of Node course type.


//node structurestruct Node int data; Node* next; Node* prev;;class LinkedList public: Node* head; public: //constructor to create an empty LinkedList LinkedList() head = NULL; ;
# node structureclass Node: #constructor to produce a new node def __init__(self, data): self.data = data self.next = no one self.prev = None#class linked Listclass LinkedList: #constructor to produce an north LinkedList def __init__(self): self.head = None
//node structureclass Node int data; Node next; Node prev;;class LinkedList Node head; //constructor to produce an empty LinkedList LinkedList() head = null; ;
//node structureclass Node public int data; public Node next; publicly Node prev;;class LinkedList windy Node head; //constructor to develop an empty LinkedList windy LinkedList() head = null; ;
//node structureclass Node windy $data; public $next; public $prev;class LinkedList public $head; //constructor to produce an north LinkedList public duty __construct() $this->head = null; ;

Create a circular Doubly linked List

Let us create a simple circular doubly attached list which consists of three data nodes.


#include making use of namespace std;//node structurestruct Node int data; Node* next; Node* prev;;class LinkedList public: Node* head; public: //constructor to develop an north LinkedList LinkedList() head = NULL; ;//test the code int main() //create an north LinkedList LinkedList MyList; //Add very first node. Node* first = brand-new Node(); first->data = 10; first->next = NULL; first->prev = NULL; //linking through head node MyList.head = first; //linking next of the node v head first->next = MyList.head; //linking prev that the head MyList.head->prev = first; //Add 2nd node. Node* 2nd = brand-new Node(); second->data = 20; second->next = NULL; //linking with very first node second->prev = first; first->next = second; //linking next of the node through head second->next = MyList.head; //linking prev the the head MyList.head->prev = second; //Add third node. Node* third = new Node(); third->data = 30; third->next = NULL; //linking with second node third->prev = second; second->next = third; //linking following of the node with head third->next = MyList.head; //linking prev of the head MyList.head->prev = third; return 0;
#include #include //node structurestruct Node int data; struct Node* next; struct Node* prev;;// test the password int main() //create the head node with name MyList struct Node* MyList = NULL; //Add very first node. Struct Node* first; //allocate second node in the heap very first = (struct Node*)malloc(sizeof(struct Node)); first->data = 10; first->next = NULL; first->prev = NULL; //linking v head node MyList = first; //linking next of the node with head first->next = MyList; //linking prev the the head MyList->prev = first; //Add 2nd node. Struct Node* second; //allocate second node in the heap 2nd = (struct Node*)malloc(sizeof(struct Node)); second->data = 20; second->next = NULL; //linking with first node second->prev = first; first->next = second; //linking following of the node with head second->next = MyList; //linking prev of the head MyList->prev = second; //Add 3rd node. Struct Node* third; //allocate 3rd node in the heap third = (struct Node*)malloc(sizeof(struct Node)); third->data = 30; third->next = NULL; //linking with second node third->prev = second; second->next = third; //linking following of the node through head third->next = MyList; //linking prev that the head MyList->prev = third; return 0;
# node structureclass Node: #constructor to create a brand-new node def __init__(self, data): self.data = data self.next = no one self.prev = None#class attached Listclass LinkedList: #constructor to create an empty LinkedList def __init__(self): self.head = none # test the code # create an north LinkedList MyList = LinkedList()#Add first node.first = Node(10)#linking through head nodeMyList.head = first#linking following of the node with headfirst.next = MyList.head #linking prev the the head MyList.head.prev = first#Add second node.second = Node(20)#linking with an initial nodesecond.prev = firstfirst.next = second#linking next of the node through headsecond.next = MyList.head #linking prev the the head MyList.head.prev = second#Add third node.third = Node(30)#linking with 2nd nodethird.prev = secondsecond.next = third#linking following of the node v headthird.next = MyList.head #linking prev the the head MyList.head.prev = third
//node structureclass Node int data; Node next; Node prev;;class LinkedList Node head; //constructor to develop an empty LinkedList LinkedList() head = null; ;// test the password public class Implementation public revolution void main(String<> args) //create an empty LinkedList LinkedList MyList = new LinkedList(); //Add first node. Node an initial = brand-new Node(); first.data = 10; first.next = null; first.prev = null; //linking with head node MyList.head = first; //linking following of the node v head first.next = MyList.head; //linking prev of the head MyList.head.prev = first; //Add 2nd node. Node 2nd = brand-new Node(); second.data = 20; second.next = null; //linking with very first node second.prev = first; first.next = second; //linking following of the node with head second.next = MyList.head; //linking prev the the head MyList.head.prev = second; //Add 3rd node. Node 3rd = new Node(); third.data = 30; third.next = null; //linking with 2nd node third.prev = second; second.next = third; //linking next of the node v head third.next = MyList.head; //linking prev of the head MyList.head.prev = third;
using System;//node structureclass Node windy int data; windy Node next; public Node prev;;class LinkedList windy Node head; //constructor to develop an north LinkedList public LinkedList() head = null; ;// check the code course Implementation static void Main(string<> args) //create an empty LinkedList LinkedList MyList = new LinkedList(); //Add an initial node. Node an initial = new Node(); first.data = 10; first.next = null; first.prev = null; //linking v head node MyList.head = first; //linking next of the node through head first.next = MyList.head; //linking prev of the head MyList.head.prev = first; //Add 2nd node. Node second = brand-new Node(); second.data = 20; second.next = null; //linking with an initial node second.prev = first; first.next = second; //linking next of the node with head second.next = MyList.head; //linking prev the the head MyList.head.prev = second; //Add third node. Node third = new Node(); third.data = 30; third.next = null; //linking with second node third.prev = second; second.next = third; //linking following of the node v head third.next = MyList.head; //linking prev of the head MyList.head.prev = third;
head = null; } };// test the code //create an north LinkedList $MyList = new LinkedList();//Add an initial node.$first = brand-new Node();$first->data = 10;$first->next = null;$first->prev = null;//linking v head node$MyList->head = $first;//linking next of the node through head$first->next = $MyList->head;//linking prev the the head $MyList->head->prev = $first;//Add second node.$second = brand-new Node();$second->data = 20;$second->next = null;//linking with first node$second->prev = $first;$first->next = $second;//linking following of the node v head$second->next = $MyList->head;//linking prev that the head $MyList->head->prev = $second;//Add third node.$third = brand-new Node();$third->data = 30;$third->next = null;//linking with 2nd node$third->prev = $second;$second->next = $third;//linking following of the node through head$third->next = $MyList->head;//linking prev that the head $MyList->head->prev = $third;?>

Traverse a circular Doubly connected List

A circular doubly connected list have the right to be traversed from any node that the list making use of a temp node. Store on moving the temp node come the following one and also displaying its content. Avoid the traversal, after reaching the starting node.


#include utilizing namespace std;//node structurestruct Node int data; Node* next; Node* prev;;class LinkedList public: Node* head; public: //constructor to produce an empty LinkedList LinkedList() head = NULL; //display the content of the perform void PrintList() Node* temp = head; if(temp != NULL) coutdatanext; if(temp == head) break; coutdata = 10; first->next = NULL; first->prev = NULL; //linking with head node MyList.head = first; //linking following of the node v head first->next = MyList.head; //linking prev of the head MyList.head->prev = first; //Add 2nd node. Node* 2nd = brand-new Node(); second->data = 20; second->next = NULL; //linking with an initial node second->prev = first; first->next = second; //linking following of the node through head second->next = MyList.head; //linking prev that the head MyList.head->prev = second; //Add 3rd node. Node* third = brand-new Node(); third->data = 30; third->next = NULL; //linking with 2nd node third->prev = second; second->next = third; //linking next of the node with head third->next = MyList.head; //linking prev that the head MyList.head->prev = third; //print the content of perform MyList.PrintList(); return 0;
#include #include //node structurestruct Node int data; struct Node* next; struct Node* prev;;//display the contents of the listvoid PrintList(struct Node* head_ref) struct Node* temp = head_ref; if(head_ref != NULL) printf("The list contains: "); when (1) printf("%i ",temp->data); temp = temp->next; if(temp == head_ref) break; printf(" "); rather printf("The perform is empty. "); // check the code int main() //create the head node through name MyList struct Node* MyList = NULL; //Add an initial node. Struct Node* first; //allocate second node in the heap very first = (struct Node*)malloc(sizeof(struct Node)); first->data = 10; first->next = NULL; first->prev = NULL; //linking v head node MyList = first; //linking next of the node with head first->next = MyList; //linking prev that the head MyList->prev = first; //Add second node. Struct Node* second; //allocate 2nd node in the heap 2nd = (struct Node*)malloc(sizeof(struct Node)); second->data = 20; second->next = NULL; //linking with first node second->prev = first; first->next = second; //linking following of the node v head second->next = MyList; //linking prev of the head MyList->prev = second; //Add third node. Struct Node* third; //allocate third node in the heap third = (struct Node*)malloc(sizeof(struct Node)); third->data = 30; third->next = NULL; //linking with 2nd node third->prev = second; second->next = third; //linking next of the node through head third->next = MyList; //linking prev that the head MyList->prev = third; //print the contents of perform PrintList(MyList); return 0;
# node structureclass Node: #constructor to develop a brand-new node def __init__(self, data): self.data = data self.next = no one self.prev = None#class attached Listclass LinkedList: #constructor to create an empty LinkedList def __init__(self): self.head = no one #display the content of the perform def PrintList(self): temp = self.head if(temp != None): print("The perform contains:", end=" ") if (True): print(temp.data, end=" ") temp = temp.next if(temp == self.head): break print() else: print("The perform is empty.")# check the password # develop an north LinkedList MyList = LinkedList()#Add very first node.first = Node(10)#linking v head nodeMyList.head = first#linking next of the node v headfirst.next = MyList.head #linking prev that the head MyList.head.prev = first#Add second node.second = Node(20)#linking with very first nodesecond.prev = firstfirst.next = second#linking following of the node with headsecond.next = MyList.head #linking prev of the head MyList.head.prev = second#Add 3rd node.third = Node(30)#linking with second nodethird.prev = secondsecond.next = third#linking next of the node v headthird.next = MyList.head #linking prev the the head MyList.head.prev = third#print the contents of list MyList.PrintList()
//node structureclass Node int data; Node next; Node prev;;class LinkedList Node head; //constructor to produce an north LinkedList LinkedList() head = null; //display the contents of the perform void PrintList() Node temp = brand-new Node(); temp = this.head; if(temp != null) System.out.print("The perform contains: "); while(true) System.out.print(temp.data + " "); temp = temp.next; if(temp == this.head) break; System.out.println(); rather System.out.println("The list is empty."); ;// check the code public course Implementation public revolution void main(String<> args) //create an north LinkedList LinkedList MyList = brand-new LinkedList(); //Add very first node. Node very first = brand-new Node(); first.data = 10; first.next = null; first.prev = null; //linking v head node MyList.head = first; //linking following of the node with head first.next = MyList.head; //linking prev that the head MyList.head.prev = first; //Add 2nd node. Node 2nd = brand-new Node(); second.data = 20; second.next = null; //linking with an initial node second.prev = first; first.next = second; //linking following of the node v head second.next = MyList.head; //linking prev that the head MyList.head.prev = second; //Add third node. Node 3rd = new Node(); third.data = 30; third.next = null; //linking with second node third.prev = second; second.next = third; //linking next of the node through head third.next = MyList.head; //linking prev of the head MyList.head.prev = third; //print the content of list MyList.PrintList();
using System;//node structureclass Node public int data; windy Node next; windy Node prev;;class LinkedList publicly Node head; //constructor to produce an empty LinkedList publicly LinkedList() head = null; //display the content of the list public void PrintList() Node temp = new Node(); temp = this.head; if(temp != null) Console.Write("The list contains: "); while(true) Console.Write(temp.data + " "); temp = temp.next; if(temp == this.head) break; Console.WriteLine(); else Console.WriteLine("The list is empty."); ;// check the code course Implementation static void Main(string<> args) //create an empty LinkedList LinkedList MyList = brand-new LinkedList(); //Add an initial node. Node very first = brand-new Node(); first.data = 10; first.next = null; first.prev = null; //linking v head node MyList.head = first; //linking next of the node through head first.next = MyList.head; //linking prev of the head MyList.head.prev = first; //Add 2nd node. Node second = brand-new Node(); second.data = 20; second.next = null; //linking with first node second.prev = first; first.next = second; //linking next of the node with head second.next = MyList.head; //linking prev that the head MyList.head.prev = second; //Add 3rd node. Node 3rd = brand-new Node(); third.data = 30; third.next = null; //linking with second node third.prev = second; second.next = third; //linking next of the node v head third.next = MyList.head; //linking prev of the head MyList.head.prev = third; //print the contents of list MyList.PrintList();


See more: Dark Souls Legends Of The Flame #2, Dark Souls: Legends Of The Flame #2

head = null; //display the content of the perform public duty PrintList() $temp = new Node(); $temp = $this->head; if($temp != null) echo "The perform contains: "; while(true) echo $temp->data." "; $temp = $temp->next; if($temp == $this->head) break; echo " "; else echo "The list is empty. "; };// test the code //create an empty LinkedList $MyList = brand-new LinkedList();//Add first node.$first = new Node();$first->data = 10;$first->next = null;$first->prev = null;//linking v head node$MyList->head = $first;//linking following of the node v head$first->next = $MyList->head;//linking prev of the head $MyList->head->prev = $first;//Add 2nd node.$second = brand-new Node();$second->data = 20;$second->next = null;//linking with an initial node$second->prev = $first;$first->next = $second;//linking next of the node through head$second->next = $MyList->head;//linking prev the the head $MyList->head->prev = $second;//Add 3rd node.$third = new Node();$third->data = 30;$third->next = null;//linking with second node$third->prev = $second;$second->next = $third;//linking next of the node v head$third->next = $MyList->head;//linking prev the the head $MyList->head->prev = $third;//print the content of list$MyList->PrintList(); ?>