-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLinkedList.java
188 lines (174 loc) · 5.69 KB
/
DLinkedList.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.Random;
public class DLinkedList<T> {
class Node {
public T data;
public Node previous;
public Node next;
public Node(T t) { previous = next = null; data = t; }
}
Node tail;
Node head;
public DLinkedList() { tail = head = null; } // default constructor
// appends to tail of list
void Append(T data) {
Node pdata = new Node(data);
if (tail == null) {
head = tail = pdata;
} else {
pdata.previous = tail;
tail.next = pdata;
tail = pdata;
}
}
// prepends to head of list
void Prepend(T data) {
Node pdata = new Node(data);
if (head == null) {
head = tail = pdata;
} else {
head.previous = pdata;
pdata.next = head;
head = pdata;
}
}
// inserts data after found data
// if `find` not exist, don't insert.
void InsertAfter(T find, T data) {
Node temp = head;
while (temp != null && !temp.data.equals(find)) {
temp = temp.next;
}
if (temp != null) {
Node pdata = new Node(data);
pdata.previous = temp;
pdata.next = temp.next;
if (temp.next != null) {
temp.next.previous = pdata;
} else {
tail = pdata;
}
temp.next = pdata;
}
}
// inserts data before found data
void InsertBefore(T find, T data) {
Node temp = tail;
while(temp != null && !temp.data.equals(find)) {
temp = temp.previous;
}
if (temp != null) {
Node pdata = new Node(data);
pdata.previous = temp.previous;
pdata.next = temp;
if (temp.previous != null) {
temp.previous.next = pdata;
} else {
head = pdata;
}
temp.previous = pdata;
}
}
// finds data node and returns it
Node Search(T data) {
Node temp = tail;
while (temp != null && !temp.data.equals(data)) {
temp = temp.previous;
}
return temp;
}
// deletes a node from the list
void Delete(T data) {
Node temp = head;
while (temp != null && !temp.data.equals(data)) {
temp = temp.next;
}
if (temp != null) {
if (temp.previous == null && temp.next == null) {
head = tail = null;
} else if (temp.previous == null) {
head = temp.next;
temp.next.previous = null;
temp.next = null;
} else if (temp.next == null) {
tail = temp.previous;
temp.previous.next = null;
temp.previous = null;
} else {
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
temp.previous = temp.next = null;
}
}
}
// returns number of nodes in list
int Count() {
int c = 0;
Node temp = tail;
while (temp != null) {
++c;
temp = temp.previous;
}
return c;
}
// returns true if list is empty
boolean IsEmpty()
{
return tail == null;
}
// prints list from tail of list
void Output()
{
Node rover = tail;
while (rover != null)
{
System.out.print( rover.data+"\t" );
rover = rover.previous;
}
System.out.println();
}
public static void main(String args[]) {
Random rand = new Random();
int count = 10;
DLinkedList<Integer> list = new DLinkedList<Integer>();
for (int x = 0; x < count; x++)
{
int rnumber = rand.nextInt(100) + 1;
list.Append(rnumber);
System.out.print(rnumber+"\t");
}
System.out.println();
System.out.println("\t1) Output list from tail to head is:");
list.Output();
int a = rand.nextInt(100) + 1;
System.out.println("\t2) Prepend element: " + a);
list.Prepend(a);
System.out.println("\t3) After prepend, the list is (from tail to head): ");
list.Output();
int b = rand.nextInt(100) + 1;
System.out.println("\t4) Insert " + b + " after " + a);
list.InsertAfter(a, b);
System.out.println("\t5) After insert " + b + " after " + a + ", the list is (from tail to head): ");
list.Output();
int c = rand.nextInt(100) + 1;
System.out.println("\t6) Insert " + c + " before " + b);
list.InsertBefore(b, c);
System.out.println("\t7) After insert " + c + " before " + b + ", the list is (from tail to head): ");
list.Output();
System.out.println("\t8) list size is: " + list.Count());
DLinkedList.Node t = list.Search(c);
System.out.println("\t9) Search " + c + " is successful?" + ( t!= null));
System.out.println("\t10) Delete " + c);
list.Delete(c);
System.out.println("\t11) After delete " + c + ", list size is: " + list.Count());
System.out.println("\t12) Output list from tail to head is:");
list.Output();
t = list.Search(c);
System.out.println("\t13) After delete " + c + ", Search " + c + " is successful?" + ( t!= null));
System.out.println("\t14) Delete " + b);
list.Delete(b);
System.out.println("\t15) After delete " + b + ", list size is: " + list.Count());
System.out.println("\t16) Output list from tail to head is:");
list.Output();
System.out.println("\t17) list is Empty now?" + list.IsEmpty());
}
}