110 likes | 235 Views
CSC 205 Programming II. Linked List – Implementation. Recap: Linked List. An order is imposed due to an item’s reference to its successor An item is stored in a node, which consists The item, and A reference to its successor. client. add. size. remove. get. numItems. head. x. node.
E N D
CSC 205 Programming II Linked List – Implementation
Recap: Linked List • An order is imposed due to an item’s reference to its successor • An item is stored in a node, which consists • The item, and • A reference to its successor client add size remove get numItems head x node … pde1 pde2 pde3 pdex
A List Consisting of Nodes • A list has a • head : referencing the first item • numItems : the total number of items in list numItems numItems 2 0 null head == null obj1 obj2 head item next item next head A list with two items in it An empty list
Displaying Items in a List • The whole list needs to be traversed. The solution is • Starting from head, use it as the current node (curr) • If curr is not pointing to null • Print (the content of) item • Set curr to next of the current node, and repeat loop curr curr curr obj1 obj1 obj2 obj1 obj2 next next head item next null null init iter1 iter2
Code – displaying items • Direct translation with a while loop • Modified with a for loop Node curr = head; while (curr != null) { System.out.println(cuur.getItem()); curr = curr.getNext(); } for (Node curr = head; curr != null; curr = curr.getNext()) { System.out.println(cuur.getItem()); }
Deleting a Specified Item prev curr • Two references are needed: curr and prev • The solution is (found)?delete:exception • Throw an exception if index is out of range • Locate the node you want to delete • Find the item at index-1 and set it as prev • Set curr to prev.getNext() • Disconnect the node • Set prev.next to curr.next … obj1 obj2 obj3 objx deleted item
Code – deleting a specified item public void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { if (index == 1) { head = head.getNext(); } else { Node prev = find(index-1); Node curr = prev.getNext(); prev.setNext(curr.getNext()); } // end if numItems--; } // end if else { throw new ListIndexOutOfBoundsException( "List index out of bounds exception on remove"); } // end if } // end remove
Adding to a Specified Position prev curr • Two references are needed: curr and prev • The solution is (found)?add:exception • Find the item at index-1 and set it as prev • Set curr to prev.getNext() • Create a new node new Node(obj, curr) and set prev.next to the new node prev.setNext(newNode) … obj1 obj2 obj3 objx nObj inserted item
Code – adding to a specified position public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems+1) { if (index == 1) { Node newNode = new Node(item, head); head = newNode; } else { Node prev = find(index-1); Node newNode = new Node(item, prev.getNext()); prev.setNext(newNode); } // end if numItems++; } else { throw new ListIndexOutOfBoundsException( "List index out of bounds exception on add"); } // end if } // end add
The First Node – a special case • Add to or delete from index = 1 is special • You don’t have to find first! Node newNode = new Node(nObj, head); head = newNode; obj1 head nObj inserted item obj1 obj2 head deleted item head = head.getNext();
Class Exercise • Write a find(int index) method • Write a method to remove all items