Browse Source

Clearing up the print method of the generic and non-generic linked lists.

Charles Reid 2 years ago
parent
commit
36e56f6f7f
2 changed files with 51 additions and 22 deletions
  1. 23
    10
      lists/linked-lists/IntList.java
  2. 28
    12
      lists/linked-lists/TList.java

+ 23
- 10
lists/linked-lists/IntList.java View File

@@ -83,16 +83,22 @@ public class IntList {
83 83
 	}
84 84
 
85 85
 	public String toString() { 
86
-		StringBuffer result = new StringBuffer();
87
-		IntNode next = head;
88
-		result.append("[  ");
89
-		while(next != null) { 
90
-			result.append(next.toString());
91
-			result.append("  ");
92
-			next = next.getNext();
86
+		StringBuffer sb = new StringBuffer();
87
+		sb.append("[ ");
88
+		if(isEmpty()) {
89
+			sb.append("]");
90
+			return sb.toString();
91
+		} 
92
+		IntNode runner = head;
93
+		sb.append( runner.toString() );
94
+		while(runner.getNext()!=null) {
95
+			// Order is important here!
96
+			runner = runner.getNext();
97
+			sb.append(", ");
98
+			sb.append( runner.toString() );
93 99
 		}
94
-		result.append("]");
95
-		return result.toString();
100
+		sb.append("]");
101
+		return sb.toString();
96 102
 	}
97 103
 
98 104
 	/** Get size of list. */
@@ -145,7 +151,7 @@ public class IntList {
145 151
 	public void add(int x) { addLast(x); }
146 152
 
147 153
 	/** Remove the item at index rmi from the list. */
148
-	public void remove(int rmi) throws Illegal, Empty {
154
+	public int remove(int rmi) throws Illegal, Empty {
149 155
 		// What could possibly go wrong?
150 156
 		//   - list is empty
151 157
 		//   - specified index is too big/too small
@@ -159,10 +165,14 @@ public class IntList {
159 165
 		} else if(rmi>=size || rmi<0) { 
160 166
 			throw new Illegal();
161 167
 		} else if(rmi==0) {
168
+			int result = this.head.getData();
169
+
162 170
 			// Zero case: remove first item in list
163 171
 			IntNode newhead = this.head.getNext();
164 172
 			this.head = newhead;
165 173
 			size--;
174
+
175
+			return result;
166 176
 		} else {
167 177
 			IntNode runner = this.head;
168 178
 			IntNode lagger = this.head;
@@ -173,8 +183,10 @@ public class IntList {
173 183
 				}
174 184
 			}
175 185
 			// Forget the runner
186
+			int result = runner.getData();
176 187
 			IntNode next = runner.getNext();
177 188
 			lagger.setNext(runner.getNext());
189
+			return result;
178 190
 		}
179 191
 	}
180 192
 
@@ -265,6 +277,7 @@ public class IntList {
265 277
 		list.addLast(997);
266 278
 		list.addLast(998);
267 279
 		list.addLast(999);
280
+		list.addLast(1000);
268 281
 		System.out.println(list);
269 282
 
270 283
 		// should not throw a null pointer exception.

+ 28
- 12
lists/linked-lists/TList.java View File

@@ -73,17 +73,18 @@ public class TList<E> {
73 73
 			return sb.toString();
74 74
 		} 
75 75
 		Node<E> runner = head;
76
-		sb.append( runner.getData().toString() );
76
+		sb.append( runner.toString() );
77 77
 		while(runner.getNext()!=null) {
78
-			sb.append(", ");
79
-			sb.append( runner.getData().toString() );
78
+			// Order is important here!
80 79
 			runner = runner.getNext();
80
+			sb.append(", ");
81
+			sb.append( runner.toString() );
81 82
 		}
82 83
 		sb.append("]");
83 84
 		return sb.toString();
84 85
 	}
85 86
 
86
-	/** Size of this thang */
87
+	/** Size of this thing */
87 88
 	public int size() { return size; }
88 89
 	
89 90
 	/** Returns true if empty. */
@@ -125,6 +126,20 @@ public class TList<E> {
125 126
 		size++;
126 127
 	}
127 128
 
129
+	public E removeFirst() throws Empty {
130
+		if(isEmpty()) { 
131
+			throw new Empty();
132
+		}
133
+		E value = this.head.getData();
134
+
135
+		// update head to point to head.next
136
+		Node<E> newhead = this.head.getNext();
137
+		this.head = newhead;
138
+		size--;
139
+
140
+		return value;
141
+	}
142
+
128 143
 
129 144
 	//////////////////////////////////
130 145
 
@@ -149,15 +164,15 @@ public class TList<E> {
149 164
 		System.out.println(list);
150 165
 		System.out.println("Size: "+list.size());
151 166
 
152
-		/*
153
-		System.out.println("Remove at 0 4 times:");
154
-		list.remove(0);
155
-		list.remove(0);
156
-		list.remove(0);
157
-		list.remove(0);
167
+		System.out.println("Remove first 4 times:");
168
+		list.removeFirst();
169
+		list.removeFirst();
170
+		list.removeFirst();
171
+		list.removeFirst();
158 172
 		System.out.println(list);
159 173
 		System.out.println("Size: "+list.size());
160 174
 
175
+		/*
161 176
 		list = getIntList();
162 177
 		System.out.println("Start:");
163 178
 		System.out.println(list);
@@ -178,12 +193,12 @@ public class TList<E> {
178 193
 		list.remove(3);
179 194
 		System.out.println(list);
180 195
 
196
+		*/
197
+
181 198
 		list = getIntList();
182 199
 		System.out.println("Start:");
183 200
 		System.out.println(list);
184 201
 
185
-		*/
186
-
187 202
 		System.out.println("Adding first");
188 203
 		list.addFirst(105);
189 204
 		list.addFirst(104);
@@ -198,6 +213,7 @@ public class TList<E> {
198 213
 		list.addLast(997);
199 214
 		list.addLast(998);
200 215
 		list.addLast(999);
216
+		list.addLast(1000);
201 217
 		System.out.println(list);
202 218
 
203 219
 		/*