File tree Expand file tree Collapse file tree 1 file changed +88
-0
lines changed
Data Structures/Linked Lists/Circular Doubly Linked Lists Expand file tree Collapse file tree 1 file changed +88
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ This is a Circular Doubly Linked List program which add a Node at the end of the Linked Lists.
4
+ Since, it is a Ciruclar Doubly Linked List, both the forward and backward traversal is also possible.
5
+
6
+ */
7
+
8
+ #include < iostream>
9
+
10
+ using namespace std ;
11
+
12
+ /*
13
+
14
+ Node definition:
15
+ 1. Pointer to previous node.
16
+ 2. Integer Data
17
+ 3. Pointer to next node.
18
+
19
+ */
20
+
21
+
22
+ class dcll_node {
23
+ public:
24
+ dcll_node* prev;
25
+ int data;
26
+ dcll_node* next;
27
+ };
28
+
29
+ void createDCLL (dcll_node* &head) {
30
+
31
+ int choice;
32
+ dcll_node* temp = head;
33
+
34
+ do {
35
+
36
+ int data;
37
+
38
+ cout << " Enter Data : " ;
39
+ cin >> data;
40
+
41
+ dcll_node* newNode = new dcll_node ();
42
+ newNode->data = data;
43
+ newNode->prev = NULL ;
44
+ newNode->next = NULL ;
45
+
46
+ if (head == NULL ) {
47
+ head = newNode;
48
+ temp = head;
49
+ head->next = head;
50
+ head->prev = head;
51
+ } else {
52
+ newNode->prev = temp;
53
+ temp->next = newNode;
54
+ temp = newNode;
55
+ temp->next = head;
56
+ head->prev = temp;
57
+ }
58
+
59
+
60
+ cout << " Do you want to continue? (1/0) : " ;
61
+ cin >> choice;
62
+
63
+ } while (choice == 1 );
64
+
65
+ }
66
+
67
+ void display (dcll_node* head) {
68
+ cout << " The elements are : " ;
69
+
70
+ dcll_node* temp = head;
71
+ do {
72
+
73
+ cout << temp->data << " " ;
74
+ temp = temp->next ;
75
+
76
+ } while (temp != head);
77
+ cout << endl;
78
+ }
79
+
80
+ int main () {
81
+
82
+ dcll_node* head = NULL ;
83
+
84
+ createDCLL (head);
85
+ display (head);
86
+
87
+ return 0 ;
88
+ }
You can’t perform that action at this time.
0 commit comments