Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.

Similar presentations


Presentation on theme: "Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College."— Presentation transcript:

1 Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College

2 Linked List Elements Each node contains two parts: –The current element (of class E) –Reference to the next element (another Node) private static class node { private E data; private node next; }

3 Ending the list The last node in a list still has a next pointer The next pointer of the last node in the list will be null –Null is never a valid reference (causes exception) Graphical representations for null:

4 Inserting into a Linked List /* Assume aNode is a reference to the node that contains “Barry”*/ Node newNode = new Node (“Carol”); newNode.next = aNode.next; aNode.next = newNode;

5 Deleting from a Linked List Just the reverse of adding to the list! Make sure you start with the item before the one to be deleted. //Again, assume aNode references “Barry” toDelete = aNode.next; //reference to “Carol” aNode.next = toDelete.next;

6 Traversing a Linked List //print elements, starting at the node called head; // Uses a for loop for(Node current=head; current!=null; current=current.next){ System.out.println(current.data); }

7 Another Traversal //prints Ann==>Barry==>Carol==>Ellen Node current = head; while(current != null){ if (current.next == null) System.out.println(current.data); else System.out.print(current.data + “==>”); current = current.next; }

8 From a Node to a List… We could simply represent the list by its first node but, how do you represent an empty list? –Empty list has no nodes Solution is to use a head reference –The list object contains a reference to the first node in the list –If this is null, the list is empty

9 Partial LinkedList Class public class LinkedList { private Node head; //first element or null private int size; //list length … private static class Node { //inner class private E data; private Node next; } //methods go here }

10 Constructor //Construct an empty list public SingleLinkedList () { head = null; size=0; }

11 Adding a New First Item //a node for item is added at the beginning public void addFirst(E item){ newNode = Node (item); newNode.next = head; // head is a reference to the first node, so // our new node is now the head. head = newNode; }

12 Copying Every Node //old is a non-empty list to copy head = new Node(); //make a new head ptr; head.data = old.head.data; Node cur = head; //current position in new list for(Node ptr=old.head.next;ptr !=null; ptr=ptr.next){ cur.next = new Node(); cur = cur.next; cur.data = ptr.data; } cur.next = null;

13 Is the list empty? //Is it empty? boolean isEmpty(){ return (head == null); }

14 Implementing get //get - retrieve node at the given index //This is not efficient - worst case O(N) Public E LinkedList.get(int index){ if((index >= size) || (index<0)) throw(IndexOutofRangeException); Node cur = head; for(int j=0;j<index;j++){ cur = cur.next; } return cur.data; }

15 Accessing Elements of a LinkedList How do we access elements within a list? –Item after which to add –Item after which to delete –Location of item that was found If we return the Node, we break the abstraction –Only LinkedLists have Nodes (cannot be part of the List interface) –If Nodes were accessible, clients could “break” the list by modifying them.

16 Iterators An Iterator is an object that keeps track of a location in a list Methods include: –Boolean hasNext() - is this the end of the list? –E next() - returns an element and advances the pointer to the next element. –Remove() - removes the last element accessed by next().

17 Advantage of Iterator Keeps track of the current place in the list (don’t need to traverse all the elements before it to get back) –Printing all elements is back to O(N) if you use an iterator Hides the underlying implementation from the user –Iterators can be implemented for ArrayLists, too.

18 Iterator Traversal Example //prints the list, one item per line //returns iterator pointing before the list… Iterator itr = aList.iterator(); while(itr.hasNext()){ String name = itr.next(); System.out.println(name); }

19 More Methods for ListIterator All of the Iterator methods plus… add () –insert item at current location hasPrevious() and Previous() –Allows reverse movement (doubly-linked list) nextIndex() –returns index of next item or size if at end Set (E obj) –Sets value of current item (must be preceded by a call to next)

20 Getting an Iterator from a Linked List listIterator() - returns iterator before beginning of list listIterator(int index) - returns iterator before index

21 Enhanced for (for each) Original for(int x=0;x<max;x++) { do something with x } Iterator based ( intList is List ) for (int x : intList){ do something with x }


Download ppt "Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College."

Similar presentations


Ads by Google