-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListFlat.java
More file actions
117 lines (110 loc) · 3.13 KB
/
ListFlat.java
File metadata and controls
117 lines (110 loc) · 3.13 KB
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
/**
* Created by RasPat on 6/28/2014.
*
*/
public class ListFlat {
public static void main(String[] args) {
DLNode head = new DLNode(1);
DLNode tail =
head.link(new DLNode(4)).
link(new DLNode(3)).
link(new DLNode(5));
// 1 -> 4 -> 3 -> 5
//_2->10_ 3
head.child = new DLNode(2);
head.child.link(new DLNode(10));
head.next.linkChild(new DLNode(3));
DLList notFlat = new DLList(head, tail);
System.out.println(notFlat);
DLList flat = notFlat.flatten();
System.out.println(flat);
}
}
class DLNode {
Object data;
DLNode next;
DLNode prev;
DLNode child;
public DLNode(Object data) {
this.data = data;
}
public DLNode link(DLNode n1) {
this.next = n1;
n1.prev = this;
return n1;
}
public DLNode linkChild(DLNode n1) {
this.child = n1;
return n1;
}
@Override
public String toString() {
return this.data.toString();
}
}
class DLList {
DLNode head;
DLNode tail;
public DLList(DLNode head, DLNode tail) {
this.head = head;
this.tail = tail;
}
/*
This function takes in a doubly linked list in which each DLNode
may have a pointer to a child DLNode and "flattens" that list
@Params
head: Pointer to the head of the list
tail: Pointer to the tail of that same list
@Returns: a pointer to the head of the list
*/
public DLList flatten() {
// Process
// Start iterating at the head. If the head has a child
// move that child to the end of the list and advance the tail pointer
// Start at beginning of the first level
// While not at the end of first level
// If current DLNode ahs a child
// Append that child to the end the list
// advance the tail pointer
// Go to next DLNode
DLNode head = this.head;
DLNode tail = this.tail;
while (head.next != null) {
if (head.child != null) {
this.append(head.child);
}
head = head.next;
}
// The local head has been consumed and is now pointing to the tail
// Use Original head
DLList flatList = new DLList(this.head, tail);
return flatList;
}
public DLNode append(DLNode n) {
this.tail.link(n);
// Find end
while (this.tail.next != null) {
this.tail = this.tail.next;
}
return n;
}
@Override
public String toString() {
DLNode head = this.head;
StringBuffer out = new StringBuffer();
while (head != null) {
out.append(head);
if (head.next != null) {
out.append("->");
}
if (head.child != null) {
out.append("_>");
DLList tmp = new DLList(head.child, null);
out.append(tmp);
out.append("<_");
}
head = head.next;
}
return out.toString();
}
}