Browse Source

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

master
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 {
}

public String toString() {
StringBuffer result = new StringBuffer();
IntNode next = head;
result.append("[ ");
while(next != null) {
result.append(next.toString());
result.append(" ");
next = next.getNext();
StringBuffer sb = new StringBuffer();
sb.append("[ ");
if(isEmpty()) {
sb.append("]");
return sb.toString();
}
IntNode runner = head;
sb.append( runner.toString() );
while(runner.getNext()!=null) {
// Order is important here!
runner = runner.getNext();
sb.append(", ");
sb.append( runner.toString() );
}
result.append("]");
return result.toString();
sb.append("]");
return sb.toString();
}

/** Get size of list. */
@@ -145,7 +151,7 @@ public class IntList {
public void add(int x) { addLast(x); }

/** Remove the item at index rmi from the list. */
public void remove(int rmi) throws Illegal, Empty {
public int remove(int rmi) throws Illegal, Empty {
// What could possibly go wrong?
// - list is empty
// - specified index is too big/too small
@@ -159,10 +165,14 @@ public class IntList {
} else if(rmi>=size || rmi<0) {
throw new Illegal();
} else if(rmi==0) {
int result = this.head.getData();

// Zero case: remove first item in list
IntNode newhead = this.head.getNext();
this.head = newhead;
size--;

return result;
} else {
IntNode runner = this.head;
IntNode lagger = this.head;
@@ -173,8 +183,10 @@ public class IntList {
}
}
// Forget the runner
int result = runner.getData();
IntNode next = runner.getNext();
lagger.setNext(runner.getNext());
return result;
}
}

@@ -265,6 +277,7 @@ public class IntList {
list.addLast(997);
list.addLast(998);
list.addLast(999);
list.addLast(1000);
System.out.println(list);

// should not throw a null pointer exception.

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

@@ -73,17 +73,18 @@ public class TList<E> {
return sb.toString();
}
Node<E> runner = head;
sb.append( runner.getData().toString() );
sb.append( runner.toString() );
while(runner.getNext()!=null) {
sb.append(", ");
sb.append( runner.getData().toString() );
// Order is important here!
runner = runner.getNext();
sb.append(", ");
sb.append( runner.toString() );
}
sb.append("]");
return sb.toString();
}

/** Size of this thang */
/** Size of this thing */
public int size() { return size; }
/** Returns true if empty. */
@@ -125,6 +126,20 @@ public class TList<E> {
size++;
}

public E removeFirst() throws Empty {
if(isEmpty()) {
throw new Empty();
}
E value = this.head.getData();

// update head to point to head.next
Node<E> newhead = this.head.getNext();
this.head = newhead;
size--;

return value;
}


//////////////////////////////////

@@ -149,15 +164,15 @@ public class TList<E> {
System.out.println(list);
System.out.println("Size: "+list.size());

/*
System.out.println("Remove at 0 4 times:");
list.remove(0);
list.remove(0);
list.remove(0);
list.remove(0);
System.out.println("Remove first 4 times:");
list.removeFirst();
list.removeFirst();
list.removeFirst();
list.removeFirst();
System.out.println(list);
System.out.println("Size: "+list.size());

/*
list = getIntList();
System.out.println("Start:");
System.out.println(list);
@@ -178,12 +193,12 @@ public class TList<E> {
list.remove(3);
System.out.println(list);

*/

list = getIntList();
System.out.println("Start:");
System.out.println(list);

*/

System.out.println("Adding first");
list.addFirst(105);
list.addFirst(104);
@@ -198,6 +213,7 @@ public class TList<E> {
list.addLast(997);
list.addLast(998);
list.addLast(999);
list.addLast(1000);
System.out.println(list);

/*

Loading…
Cancel
Save