1 / 17

Linked Lists

Linked Lists. Contents . LinkedList implements the Interface List Doubly Linked List implementation of common methods boolean add( Object x) -- append to end of the list boolean add(int i, Object x) – insert at position i boolean remove(int i) – remove object at position i.

stacy
Download Presentation

Linked Lists

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. Linked Lists Contents • LinkedList implements the Interface List • Doubly Linked List implementation of common methods • boolean add( Object x) -- append to end of the list • boolean add(int i, Object x) – insert at position i • boolean remove(int i) – remove object at position i • Singly Linked List -- outline of methods • Circularly Linked List

  2. Linked Lists A LinkedList implements the interface List There are a number of ways in which a LinkedList can be implemented internally, but each implementation should provide the client (code that uses a LinkedList) with a single common set of operations that can be evoked by messages to the LinkedList object. Types of LinkedList implementations include: • Doubly linked list • Singly linked list • Circular-linked list We will examine how the main List operations are implemented in each of these alternatives

  3. Linked Lists Doubly-linked List A LinkedList is a connected sequence of Nodes. It is composed of two classes of objects: LinkedLists and Nodes. class Node { //Node methods go here //attributes private Node pred, succ; private Object data; } public class LinkedList implements List { //class Node (see left) is declared here //implementation of List methods goes here //attributes private Node head, tail; privateint size; } Node is an internal class visible only to LinkedList objects.

  4. aList head size tail p p 24 21 Linked Lists LinkedList aList = new LinkedList( ); Integer temp = new Integer(21); Node p = new Node(temp); Integer temp = new Integer(24); Node p = new Node (temp); aList.add( new Integer(24) ); aList.add( new Integer(21) ); //head != 0 tail.setNext(p); if (head == 0) head = p; tail = p; size++; p.setPrev(tail); tail = p; size++; 1 0 2

  5. Linked Lists Node methods used in add( ) Putting it together Node (Object x) { //Node constructor data = x; pred = 0; succ = 0; } publicboolean add (Object theObj) { Node p = new Node(theObj); if (head == 0) head = p; else { tail.setNext(p); p.setPrev(tail); } tail = p; size++; return true; } publicvoid setNext(Node q) { succ = q; } publicvoid setPrev(Node q) { pred = q; }

  6. aList head size tail 10 21 24 p 3 Linked Lists Inserting an object at position i Integertemp = new Integer (3); Node p = new Node(temp); p.setPrev(0); p.setNext(head); if (head != 0) head.setPrev(p); 3 4 head = p; size++; Insert at front of the list (i == 0) The successor of Node referenced by p is Node referenced by head else (head ==0) -- inserting into an empty list -- set tail = p

  7. aList head size tail 10 21 24 p 41 Linked Lists Inserting an object at position i Integertemp = new Integer (3); Node p = new Node(temp); if (i == size) { //insert at end p.setPrev = tail; p.setNext(0); tail.setNext(p); 3 4 tail = p; size++; Inserting at end of non-empty list (i==size) p’ s predecessor is the Node tail points to Make Node p points to the successor of Node tail points to The Node p points to is now at the end of the list

  8. aList head size tail q qp q q 10 21 24 p 23 Linked Lists Integertemp = new Integer (3); Node p = new Node(temp); Inserting an object at position i int count = 0; Node q = head; while (count < i) { count++; q = q.getNext( ); } 3 4 Node qp = q.getPrev(); p.setPrev(qp); Inserting at general position – (0 <= i < size) count = 1 count = 0 p.setNext(q); count = 2 qp. setNext(p); The order of these statements is important – statements 2 and 3 MUST precede statements 4 and 5. Move a handle to the Node at position i – Let i = 2 in this example Insert new Node before the Node that q points to. q.setPrev(p); size++;

  9. else { //insert at general position int count = 0; Node q = head; while (count < i) { count++; q = q.getNext( ); } Node qp = q.getPrev( ); p.setPrev(qp); p.setNext(q); qp.setNext( p); q.setPrev(p); } size++; return true; } Linked Lists Putting it all together publicboolean add(int i, Object x) { if (i < 0 || i > size) { //range error System.err.println (“error – index out of range”); System.exit(1); } Node p = new Node(x); if (i ==0) { //insert at front p.setPrev(0); p.setNext(head); if (head !=0) head.setPrev(p); else tail = p; head = p; } elseif (i == size) { //insert at end p.setNext(0); p.setPrev(tail); tail.setNext(p); tail = p; } Node methods used public Node getNext( ) { return succ; } public Node getPrev( ) { return pred; }

  10. aList head size tail p 41 21 24 Linked Lists Removing objects from a List Node p = head; if (i ==0) { //remove first Node head = p.getNext( ); if (head != 0) head.setPrev(0); else //removing last node tail = 0; size--; } 3 2 Remove first Node – (i == 0) Automatic garbage collection reclaims the unreferenced Node when p goes out of scope.

  11. aList head size tail p 41 21 24 Linked Lists if (i ==size-1) { //remove last Node p = tail; //Node * p; set previously if (head != tail) { tail.getPrev().setNext(0); tail = tail.getPrev( ); } else {//removing last node tail = 0; head = 0; } size--; } Removing objects from a List 3 2 Remove the last node – (i == size – 1) tail.getPrev( ) .setNext(0); Automatic garbage collection reclaims memory when p goes out of scope

  12. aList head size tail p p 41 21 24 Linked Lists Removing objects from a List Node p = head; int pos = 0; while (pos < i) { //find node to cut p = p.getNext( ); //move handle pos++; } p.getPrev( ).setNext(p.getNext( )); p.getNext( ).setPrev(p.getPrev( )); size--; } 3 2 p.getPrev( ) .setNext( p.getNext( ) ) p.getNext( ) .setPrev( p.getPrev( ) ) The Node referenced by p is deleted when p goes out of scope pos = 1 pos = 0 Remove Node in general position – ( i ==1)

  13. Linked Lists Bringing it all back home elseif (i == size-1) { //remove last node p = tail; if (head != tail) { tail.getPrev.setNext(0); tail = tail.getPrev( ); } else { head = 0; tail = 0; } } //end else if else { //remove Node in general position p = head; int pos = 0; while (pos < i ) { //move handle p = p.getNext( ); pos++ } p.getPrev.setNext(p.getNext( ) ); p.getNext.setPrev(p.getPrev( ) ); } //end else p.setPrev(0); p.setNext(0); //for safety size--; return true; publicboolean remove(int i) { if (i < 0 || i >= size) { //range error System.err.println (“error – index out of range”); System.exit(1); } Node p; if (i == 0 ) // remove first node p = head; head = p.getNext( ); if (head != 0) head.setPrev(0); else tail = 0; } //end if } //end remove

  14. aList size head tail p q p q p Linked Lists class Node { public Node(Object x) {…} publicvoid setNext( ) {…} public Node getNext( ) {…} public Object getData( ) {…} private Node succ; private Object data; } Singly-linked Lists 3 data succ 21 24 47 pos = 0 pos =2 pos = 1 Implementing methods add( ) and remove( ) will be left as an exercise. To remove the Node at index pos, remove the Node that p references. Use the Node that q references to reattach the links. Move two handles in sequence until p reaches position i – (let i = 2) for(int pos = 0; pos < i; pos++) { q = p; p = p.getNext( ); } To perform an insert at pos, insert the new Node after the Node that q references and before the Node that p references Node p = head, q = head;

  15. aList size head Linked Lists Circularly Linked Lists 3 21 24 47 A circularly-linked list is a singly-linked list where the last Node refers back to the first Node in the list. A tail reference is not used.

  16. aList size head p q q p p Linked Lists Traversing a circularly-linked list To traverse a circularly-linked list, move a pair of handles until pos == i (or p.getNext( ) == head) An insertion can be performed after the Node that q references, and a removal will remove the Node referenced by p, using q to reattach the list. Locate the node containing x = 47 3 21 24 47 Node p = head, q; while (p.getNext( ) != head && p.getData( ) != x) { q = p; p = p.getNext( ); }

  17. aList size head Linked Lists A circularly-linked list with a single Node Special case – List contains a single Node The succ field of a single Node contains a handle to itself 1 Constructor for Node public Node(Object obj) { data = obj; succ = this; } 21 The object under construction The only special circumstances about adding the first or removing the last Node from a circularly-linked list involve setting the contents of head. Create a Node referring to itself and overwrite the succ field using setNext( ) when adding to the list if Node is not the first Node to be added.

More Related