-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlattenADoublyLinkedList.cpp
134 lines (115 loc) · 2.58 KB
/
FlattenADoublyLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
using namespace std;
// using recursion
class Node // class to create a node
{
public:
int data;
Node *next;
Node *prev;
Node *child;
Node(int val)
{
data = val;
next = prev = child = NULL;
}
};
class DoublyList // class to add, delete, print and search nodes
{
public:
Node *head;
Node *tail;
DoublyList()
{
head = tail = NULL;
}
Node *flatten(Node *head)
{
if (head == NULL)
{
return head;
}
Node *curr = head;
while (curr != NULL)
{
if (curr->child != NULL) // if node has a valid child
{
Node *next = curr->next;
curr->next = flatten(curr->child); // recursive call
curr->next->prev = curr;
curr->child = NULL;
// find tail
while (curr->next != NULL)
{
curr = curr->next;
}
// attach tail with next ptr
if (next != NULL)
{
curr->next = next;
next->prev = curr;
}
}
curr = curr->next;
}
return head;
}
void printList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main()
{
DoublyList dll;
// Creating nodes
Node *n1 = new Node(1);
Node *n2 = new Node(2);
Node *n3 = new Node(3);
Node *n4 = new Node(4);
Node *n5 = new Node(5);
Node *n6 = new Node(6);
Node *n7 = new Node(7);
Node *n8 = new Node(8);
Node *n9 = new Node(9);
Node *n10 = new Node(10);
Node *n11 = new Node(11);
Node *n12 = new Node(12);
// Connecting main doubly linked list
n1->next = n2;
n2->prev = n1;
n2->next = n3;
n3->prev = n2;
n3->next = n4;
n4->prev = n3;
n4->next = n5;
n5->prev = n4;
n5->next = n6;
n6->prev = n5;
// Adding child nodes
n3->child = n7;
n7->next = n8;
n8->prev = n7;
n8->next = n9;
n9->prev = n8;
n9->next = n10;
n10->prev = n9;
n8->child = n11;
n11->next = n12;
n12->prev = n11;
// Assign head
dll.head = n1;
cout << "Original list (with child pointers):" << endl;
dll.printList(dll.head);
// Flatten list
dll.head = dll.flatten(dll.head);
cout << "Flattened list:" << endl;
dll.printList(dll.head);
return 0;
}