From 1c2b7cd0dc267bc9e6c8f21d52a4d70176604d0a Mon Sep 17 00:00:00 2001 From: jjenista Date: Wed, 25 Feb 2009 20:04:55 +0000 Subject: [PATCH] Add remove operation to LinkedListIterator and fix bug with LinkedList.removeFirst() and removeLast() --- Robust/src/ClassLibrary/Iterator.java | 5 +++++ Robust/src/ClassLibrary/LinkedList.java | 25 ++++++++++++++++++++---- Robust/src/Tests/LinkedListTest.java | 26 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Robust/src/ClassLibrary/Iterator.java b/Robust/src/ClassLibrary/Iterator.java index 0dd24e04..e88a4854 100644 --- a/Robust/src/ClassLibrary/Iterator.java +++ b/Robust/src/ClassLibrary/Iterator.java @@ -8,4 +8,9 @@ public class Iterator { System.out.println( "Iterator is an abstract class." ); System.exit(-1); } + + void remove() { + System.out.println( "Iterator is an abstract class." ); + System.exit(-1); + } } diff --git a/Robust/src/ClassLibrary/LinkedList.java b/Robust/src/ClassLibrary/LinkedList.java index 69356870..b4f26e69 100644 --- a/Robust/src/ClassLibrary/LinkedList.java +++ b/Robust/src/ClassLibrary/LinkedList.java @@ -113,6 +113,8 @@ public class LinkedList { head = head.next; if( head != null ) { head.prev = null; + } else { + tail = null; } size--; } @@ -125,6 +127,8 @@ public class LinkedList { tail = tail.prev; if( tail != null ) { tail.next = null; + } else { + head = null; } size--; } @@ -168,23 +172,36 @@ public class LinkedList { } public class LinkedListIterator extends Iterator { + LinkedList ll; LinkedListElement itr; + Object removeable; public LinkedListIterator( LinkedList ll ) { + this.ll = ll; itr = ll.head; + removeable = null; } - boolean hasNext() { + public boolean hasNext() { return itr != null; } - Object next() { + public Object next() { if( itr == null ) { System.out.println( "LinkedListIterator: illegal next()" ); System.exit(-1); } - Object o = itr.element; + removeable = itr.element; itr = itr.next; - return o; + return removeable; + } + + public void remove() { + if( removeable == null ) { + System.out.println( "LinkedListIterator: illegal remove()" ); + System.exit(-1); + } + ll.remove( removeable ); + removeable = null; } } diff --git a/Robust/src/Tests/LinkedListTest.java b/Robust/src/Tests/LinkedListTest.java index fa3af9da..81243ff2 100644 --- a/Robust/src/Tests/LinkedListTest.java +++ b/Robust/src/Tests/LinkedListTest.java @@ -13,5 +13,31 @@ public class LinkedListTest { x = (Integer)list.pop(); System.out.println( "should be a 3: "+x ); + + list.addLast( (Object)new Integer( 6 ) ); + list.addLast( (Object)new Integer( 5 ) ); + list.addLast( (Object)new Integer( 4 ) ); + list.addLast( (Object)new Integer( 3 ) ); + + System.out.println( "Looking for list 6, 5, 4, 3: " ); + System.out.print( " " ); + Iterator i = list.iterator(); + while( i.hasNext() ) { + System.out.print( i.next() + ", " ); + } + System.out.println( "" ); + + i = list.iterator(); + i.next(); + i.next(); + i.remove(); + + System.out.println( "Removed 5, looking for list 6, 4, 3: " ); + System.out.print( " " ); + i = list.iterator(); + while( i.hasNext() ) { + System.out.print( i.next() + ", " ); + } + System.out.println( "" ); } } \ No newline at end of file -- 2.34.1