File tree Expand file tree Collapse file tree 2 files changed +114
-1
lines changed
Data Structures/Linked Lists/Circular Doubly Linked Lists Expand file tree Collapse file tree 2 files changed +114
-1
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ This is a Circular Doubly Linked List program which recursively reverses 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
+ class dcll_node {
22
+ public:
23
+ dcll_node* prev;
24
+ int data;
25
+ dcll_node* next;
26
+ };
27
+
28
+ void createDCLL (dcll_node* &head) {
29
+
30
+ dcll_node* temp = head;
31
+
32
+ int choice;
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
+ temp->next = newNode;
53
+ newNode->prev = temp;
54
+ temp = newNode;
55
+ newNode->next = head;
56
+ head->prev = newNode;
57
+ }
58
+
59
+ cout << " Do you want to continue? (1/0) : " ;
60
+ cin >> choice;
61
+
62
+ } while (choice == 1 );
63
+
64
+ }
65
+
66
+ dcll_node* recursive_reverse (dcll_node *head) {
67
+
68
+ if (head == NULL || head->next == head)
69
+ return head;
70
+
71
+ head->prev ->next = head->next ;
72
+ head->next ->prev = head->prev ;
73
+ dcll_node* tHead = recursive_reverse (head->next );
74
+ tHead->prev ->next = head;
75
+ head->prev = tHead->prev ;
76
+ head->next = tHead;
77
+ tHead->prev = head;
78
+ return tHead;
79
+ }
80
+
81
+ void display (dcll_node* head) {
82
+
83
+ if (head == NULL )
84
+ return ;
85
+
86
+ cout << " The elements are : " ;
87
+ dcll_node* temp = head;
88
+
89
+ do {
90
+
91
+ cout << temp->data << " " ;
92
+ temp = temp->next ;
93
+
94
+ } while (temp != head);
95
+ cout << endl;
96
+ }
97
+
98
+ int main () {
99
+
100
+ dcll_node* head = NULL ;
101
+
102
+ createDCLL (head);
103
+
104
+ cout << " Before reversing : " << endl;
105
+ display (head);
106
+
107
+ head = recursive_reverse (head);
108
+
109
+ cout << " After reversing : " << endl;
110
+ display (head);
111
+
112
+ return 0 ;
113
+ }
Original file line number Diff line number Diff line change 1
1
/*
2
2
3
- This is a Circular Doubly Linked List program which deletes a Node at the end of the Linked Lists.
3
+ This is a Circular Doubly Linked List program which reverses the Linked Lists.
4
4
Since, it is a Ciruclar Doubly Linked List, both the forward and backward traversal is also possible.
5
5
6
6
*/
You can’t perform that action at this time.
0 commit comments