1 / 11

CSC 205 Programming II

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.

mabyn
Download Presentation

CSC 205 Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 205 Programming II Linked List – Implementation

  2. 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

  3. 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

  4. 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

  5. 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()); }

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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();

  11. Class Exercise • Write a find(int index) method • Write a method to remove all items

More Related