Browse Source

reorganizing doubly-linked list class DLinkedList and expanding tests.

Charles Reid 2 years ago
parent
commit
c287a00c69
1 changed files with 198 additions and 29 deletions
  1. 198
    29
      lists/linked-lists/DLinkedList.java

+ 198
- 29
lists/linked-lists/DLinkedList.java View File

@@ -3,6 +3,204 @@ class Illegal extends IllegalArgumentException{};
3 3
 
4 4
 public class DLinkedList<E> {
5 5
 
6
+
7
+	public static void main(String[] args) throws Empty { 
8
+
9
+
10
+		DLinkedList<String> d = new DLinkedList<String>();
11
+		try {
12
+			d.get(5);
13
+		} catch(Empty e) { 
14
+			System.out.println("Caught Empty exception correctly.");
15
+		}
16
+
17
+		d.addLast("JFK");
18
+		d.addLast("PVD");
19
+		d.addLast("SFO");
20
+		d.addFirst("Fortyseven");
21
+		d.addFirst("Thirtysix");
22
+		d.addFirst("Eighteen");
23
+		System.out.println(d);
24
+
25
+		System.out.printf("Peeking first %s...\n", d.first());
26
+		System.out.printf("Peeking last %s...\n", d.last());
27
+
28
+		System.out.println("Before empty: " + d);
29
+		while(!d.isEmpty()) { 
30
+			System.out.printf("Removing %s... \n", d.removeFirst());
31
+		}
32
+		System.out.println("After empty: " + d);
33
+
34
+		d.addFirst("SLC");
35
+		d.addLast("PDX");
36
+		d.addLast("SEA");
37
+		d.addLast("LAX");
38
+		System.out.println(d);
39
+
40
+	
41
+
42
+	}
43
+
44
+
45
+
46
+	//////////////////////////////////
47
+
48
+
49
+
50
+	private DNode<E> alpha = null;
51
+	private DNode<E> omega = null;
52
+	private int size;
53
+
54
+	public DLinkedList() { 
55
+		alpha = new DNode<E>(null);
56
+		omega = new DNode<E>(null);
57
+		
58
+		// In the beginning, there was darkness...
59
+		link(alpha,omega);
60
+
61
+		size = 0;
62
+	}
63
+
64
+	// Linked list adt: 
65
+	// - size
66
+	// - isEmpty
67
+	// - first
68
+	// - last 
69
+	// - addFirst
70
+	// - addLast
71
+	// - removeFirst
72
+	// - removeLast
73
+	// - get(i)
74
+	public int size() { return size; } 
75
+	public boolean isEmpty() { return size==0; }
76
+
77
+	public String toString() { 
78
+		StringBuffer sb = new StringBuffer();
79
+		sb.append("[ ");
80
+		if(isEmpty()) {
81
+			sb.append("]");
82
+			return sb.toString();
83
+		}
84
+
85
+		int c = 1;
86
+
87
+		DNode<E> iter = alpha.getNext();
88
+		sb.append(iter.getData().toString());
89
+		c++;
90
+		while(c<=size) {
91
+			iter = iter.getNext();
92
+			sb.append(", ");
93
+			sb.append(iter.getData().toString());
94
+			c++;
95
+		}
96
+		sb.append(" ]");
97
+		return sb.toString();
98
+	}
99
+
100
+	public E first() throws Empty {
101
+		if(isEmpty()) {
102
+			throw new Empty();
103
+		}
104
+		return alpha.getNext().getData();
105
+	};
106
+
107
+	public E last() throws Empty {;
108
+		if(isEmpty()) {
109
+			throw new Empty();
110
+		}
111
+		return omega.getPrev().getData();
112
+	}
113
+
114
+	public E get(int ix) throws Empty {
115
+		if(isEmpty()) {
116
+			throw new Empty();
117
+		}
118
+
119
+		int n = size();
120
+
121
+		DNode<E> runner;
122
+		int irunner, cmax;
123
+		if(ix > n/2) {
124
+
125
+			// ix is in second half: start from the back
126
+			cmax = (n-ix-1); // plus-one shift to index, or minus 1 shift to n
127
+			irunner = 0;
128
+			runner = omega;
129
+			while(irunner < cmax) { 
130
+				runner = runner.getPrev();
131
+				irunner++;
132
+			}
133
+
134
+		} else {
135
+			// ix is in first half: start from zero
136
+			cmax = n/2;
137
+			irunner = 0;
138
+			runner = alpha;
139
+			while(irunner < cmax) {
140
+				runner = runner.getNext();
141
+				irunner++;
142
+			}
143
+
144
+		}
145
+		return runner.getData();
146
+	}
147
+
148
+	public void link(DNode<E> a, DNode<E> b) {
149
+		a.setNext(b);
150
+		b.setPrev(a);
151
+	}
152
+
153
+	public void addFirst(E e) { 
154
+		DNode<E> oldnext = alpha.getNext();
155
+		DNode<E> addme = new DNode<E>(e);
156
+
157
+		link(alpha,addme);
158
+		link(addme,oldnext);
159
+
160
+		size++;
161
+	}
162
+
163
+	public void addLast(E e) { 
164
+		DNode<E> oldlast = omega.getPrev();
165
+		DNode<E> lastme = new DNode<E>(e);
166
+
167
+		link(lastme, omega);
168
+		link(oldlast, lastme);
169
+		
170
+		size++;
171
+	}
172
+
173
+	public E removeFirst() throws Empty { 
174
+		if(isEmpty()) { 
175
+			throw new Empty();
176
+		}
177
+		E ret = alpha.getNext().getData();
178
+		DNode<E> newhead = alpha.getNext().getNext();
179
+		link(alpha, newhead);
180
+		size--;
181
+		return ret;
182
+	}
183
+
184
+
185
+	public E removeLast() throws Empty { 
186
+		if(isEmpty()) { 
187
+			throw new Empty();
188
+		}
189
+		E ret = omega.getPrev().getData();
190
+		DNode<E> newtail = omega.getPrev().getPrev(); 
191
+		link(newtail, omega);
192
+		size--;
193
+		return ret;
194
+	}
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
6 204
 	//////////////////////////////////
7 205
 
8 206
 	/** Templated linked list node class. 
@@ -40,37 +238,8 @@ public class DLinkedList<E> {
40 238
 	}
41 239
 
42 240
 
43
-	//////////////////////////////////
44
-
45 241
 
46
-	private DNode<E> header = null;
47
-	private DNode<E> trailer = null;
48
-	private int size;
49
-
50
-	public DLinkedList() { }
51
-
52
-	// Access methods:
53
-	public int size() { return size; } 
54
-	public boolean isEmpty() { return size==0; }
55 242
 
56
-
57
-
58
-	///////////////////////////////////////////////////////
59
-
60
-	public static void main(String[] args) { 
61
-
62
-
63
-		DLinkedList<String> d = new DLinkedList<String>();
64
-		/*
65
-		for(int i=0; i<10; i++) {
66
-			d.addLast("JFK");
67
-			d.addLast("PVD");
68
-			d.addLast("SFO");
69
-		}
70
-		*/
71
-
72
-
73
-	}
74 243
 }
75 244
 
76 245