Browse Source

moving linked list materials to new directory.

Charles Reid 2 years ago
parent
commit
5f90881dd9

lists/IntList.java → lists/linked-lists/IntList.java View File


+ 5
- 0
lists/linked-lists/Makefile View File

@@ -0,0 +1,5 @@
1
+default:
2
+	javac IntList.java && java IntList
3
+
4
+clean:
5
+	rm -rf *.class

lists/README.md → lists/linked-lists/README.md View File

@@ -45,6 +45,7 @@ Linked list methods should automatically become a list of bookkeeping things to
45 45
 
46 46
 
47 47
 
48
+
48 49
 Size of linked list:;
49 50
 * Number of bytes: 
50 51
 	* See [https://charlesreid1.com/wiki/Java/Memory](https://charlesreid1.com/wiki/Java/Memory)

+ 296
- 0
lists/linked-lists/SimpleIntList.java View File

@@ -0,0 +1,296 @@
1
+import java.io.*;
2
+import java.util.*;
3
+
4
+/** SIMPLE integer linked list.
5
+ *
6
+ * LinkedList ADT implements:
7
+ *   - size
8
+ *   - isEmpty
9
+ *   - first 
10
+ *   - last 
11
+ *   - addFirst
12
+ *   - addLast
13
+ *   - removeFirst
14
+ *
15
+ * Nothing fancy - no exception handling.
16
+ */
17
+public class IntList {
18
+
19
+	// ------------- linked node class --------------
20
+	/**
21
+	 * Linked list node class.
22
+	 *
23
+	 * Implements:
24
+	 *   - getData
25
+	 *   - getNext
26
+	 *   - setNext
27
+	 * */
28
+	class IntNode {
29
+		public int data;
30
+		public IntNode next;
31
+		/** New node with specified data and no next pointer. */
32
+		public IntNode(int data) {
33
+			this(data,null);
34
+		}
35
+		/** New node with specified data and specified next pointer. */
36
+		public IntNode(int data, IntNode next) {
37
+			this.data = data;
38
+			this.next = next;
39
+		}
40
+		public String toString() { return Integer.toString(this.data); }
41
+		public int getData(){ return data; }
42
+		public IntNode getNext(){ return next; }
43
+		public void setNext(IntNode next){ this.next = next; }
44
+		public boolean equals(int i) { return data==i; }
45
+	}
46
+
47
+	// ------------- end linked node class ----------
48
+
49
+	private IntNode head;
50
+	private IntNode tail;
51
+	private int size;
52
+	
53
+	/** Make an empty new IntList. */
54
+	public IntList() { 
55
+		head = null;
56
+		tail = null;
57
+	}
58
+
59
+	public String toString() throws IxException { 
60
+		StringBuffer result = new StringBuffer();
61
+		IntNode next = head;
62
+		result.append("[  ");
63
+		while(next != null) { 
64
+			result.append(next.toString());
65
+			result.append("  ");
66
+			next = next.getNext();
67
+		}
68
+		result.append("]");
69
+		return result.toString();
70
+	}
71
+
72
+	/** Get size of list. */
73
+	public int size() { return size; }
74
+
75
+	/** Returns true if empty. */
76
+	public boolean isEmpty() { return size==0; }
77
+
78
+	/** Return the first integer in the list. */
79
+	public int first() { return head.getData(); }
80
+
81
+	/** Return the last integer in the list. */
82
+	public int last() { return tail.getData(); }
83
+
84
+	/** Add an integer at the beginning of the list. */
85
+	public void addFirst(int x) {
86
+		addFirst(new IntNode(x));
87
+	}
88
+
89
+	/** Add a node at the beginning of the list. */
90
+	private void addFirst(IntNode newHead) { 
91
+		newHead.setNext(this.head);
92
+		this.head = newHead;
93
+		size++;
94
+	}
95
+
96
+	/** Add an integer at the end of the list. */
97
+	public void addLast(int x) { 
98
+		addLast(new IntNode(x));
99
+	}
100
+
101
+	/** Add a node at the end of the list. */
102
+	public void addLast(IntNode newLast) {
103
+		this.tail.setNext(newLast);
104
+		this.tail = newLast;
105
+		size++;
106
+	}
107
+
108
+	/** Remove the item at index rmi from the list. */
109
+	public void remove(int rmi) throws IxException {
110
+		if(isEmpty()) {
111
+			return;
112
+		} else if(rmi>=size || rmi<0) { 
113
+			throw new IxException();
114
+		} else if(rmi==0) {
115
+			// Zero case: remove first item in list
116
+			IntNode newhead = this.head.getNext();
117
+			this.head = newhead;
118
+			size--;
119
+		} else {
120
+			IntNode thisOne = this.head;
121
+			IntNode lagger = this.head;
122
+			for(int j=0;j<rmi;j++){
123
+				thisOne = thisOne.getNext();
124
+				if(j>=1) {
125
+					lagger = lagger.getNext();
126
+				}
127
+			}
128
+			// Forget thisOne
129
+			IntNode next = thisOne.getNext();
130
+			lagger.setNext(thisOne.getNext());
131
+		}
132
+	}
133
+
134
+
135
+
136
+
137
+	/** Remove the item at the specified index. */
138
+/*
139
+	public void remove(int rmi) throws IxException { 
140
+		// What could possibly go wrong?
141
+		// - empty list
142
+		// - size specified is too big/too small
143
+		// - special case (remove front of list) specified
144
+		if(isEmpty()) {
145
+			return;
146
+		} else if(rmi>=size || i<0) { 
147
+			throw new IxException();
148
+		} else {
149
+
150
+			// List index counter
151
+			int ix = 0;
152
+			
153
+			if(rmi==0) { 
154
+				// Zero index case
155
+				IntNode newhead = this.head.getNext();
156
+				this.head = newhead;
157
+				size--;
158
+
159
+			} else {
160
+				// list index counter goes from 
161
+				// 1 <= ix <= size-1
162
+                for(int ix=0; 
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+			// c=0 is taken care of above
172
+			int c=1;
173
+			IntNode previous = head;
174
+			c++;
175
+			// Loop until the next node is the one we want to remove
176
+			while(c<i) { 
177
+				// Increment "previous" pointer by 1
178
+				previous = previous.getNext();
179
+				c++;
180
+			}
181
+
182
+			// Pointer "previous" now points to previous node in list
183
+			// Get pointer to new next node
184
+			IntNode next = previous.getNext().getNext();
185
+			// Forget about old next
186
+			previous.setNext(next);
187
+
188
+			// Update size of list
189
+			size--;
190
+
191
+			// Update tail pointer if we just removed last item in list
192
+			if(i==(size-1)) {
193
+				// We just removed last item from list
194
+				// New tail is previous, previous.getNext() is null
195
+				this.tail = previous;
196
+			}
197
+		}
198
+	}
199
+	*/
200
+
201
+	/** Remove item at beginning of list. */
202
+	public void removeFirst() { 
203
+		if(isEmpty()) {
204
+			return;
205
+		} else {
206
+			IntNode newhead = this.head.getNext();
207
+			this.head = newhead;
208
+		}
209
+	}
210
+
211
+	/** Remove item at end of list. */
212
+	public void removeLast() { 
213
+		if(isEmpty()) {
214
+			return;
215
+		} else {
216
+			remove(size-1);
217
+		}
218
+	}
219
+
220
+
221
+
222
+
223
+	public static IntList getIntList() {
224
+		IntList l = new IntList();
225
+		l.addFirst(3);
226
+		l.addFirst(2);
227
+		l.addFirst(1);
228
+		l.addFirst(0);
229
+		return l;
230
+	}
231
+		
232
+
233
+	// Main method
234
+	public static void main(String[] args) throws IxException { 
235
+
236
+		IntList list;
237
+		
238
+		System.out.println("Start:");
239
+		list = getIntList();
240
+		System.out.println(list);
241
+
242
+		System.out.println("Remove at 0 4 times:");
243
+		list.remove(0);
244
+		list.remove(0);
245
+		list.remove(0);
246
+		list.remove(0);
247
+		System.out.println(list);
248
+
249
+		list = getIntList();
250
+		System.out.println("Start:");
251
+		System.out.println(list);
252
+		System.out.println("Removing index 1...");
253
+		list.remove(1);
254
+		System.out.println(list);
255
+
256
+		list = getIntList();
257
+		System.out.println("Start:");
258
+		System.out.println(list);
259
+		System.out.println("Removing index 2...");
260
+		list.remove(2);
261
+		System.out.println(list);
262
+
263
+		list = getIntList();
264
+		System.out.println("Start:");
265
+		System.out.println(list);
266
+		System.out.println("Removing index 3...");
267
+		list.remove(3);
268
+		System.out.println(list);
269
+
270
+		list = getIntList();
271
+		System.out.println("Start:");
272
+		System.out.println(list);
273
+		System.out.println("Adding stuff");
274
+		list.addFirst(105);
275
+		list.addFirst(104);
276
+		list.addFirst(103);
277
+		list.addFirst(102);
278
+		list.addFirst(101);
279
+		list.addFirst(100);
280
+		System.out.println(list);
281
+
282
+		// should not throw a null pointer exception.
283
+		for(int i=0; i<1000; i++) { 
284
+			try {
285
+				list.remove(0);
286
+			} catch(IxException e) { 
287
+				System.out.println("list is emptied out");
288
+				break;
289
+			}
290
+		}
291
+		System.out.println("Finished with tests of IntList.");
292
+	}
293
+
294
+}
295
+
296
+

+ 29
- 0
lists/linked-lists/TList.java View File

@@ -0,0 +1,29 @@
1
+/** Templated linked list class. */
2
+public class TList<E> {
3
+
4
+	/** Linked list node class. 
5
+	 *
6
+	 * Implements:
7
+	 *   - getData
8
+	 *   - getNext
9
+	 *   - setNext
10
+	 */
11
+	private static class Node<E> {
12
+		private E data;
13
+		private Node next;
14
+		/** Constructor with specified value that points to nobody. */
15
+		public Node(E data) {
16
+			this(data,null);
17
+		}
18
+		/** Constructor with specified value that points to target. */
19
+		public Node(E data, Node next) { 
20
+			this.data = data;
21
+			this.next = next;
22
+		}
23
+		/** Get data stored by node. */
24
+		public E getData() { return data; }
25
+		/** Get next node pointer. */
26
+		public Node getNext() { return next; }
27
+	}
28
+
29
+}