## Presentation on theme: "1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5."— Presentation transcript:

Adding a new node at the tail 5 Add node containing the string Zurich to tail of list head Rome Seattle Toronto last Zurich create new node containing (reference to) string Zurich, with next =null head Rome Seattle Toronto last Zurich Redirect last.next to new node head Rome Seattle Toronto last Zurich Reallocate last to new node ADS2 Lecture 5

Doubly linked lists 10 Removing an element from the tail of a singly linked list is not easy Whether we have just a head, or a head and a last node, need to always traverse the whole list to remove the end. Why? In general it is hard to remove any node other than the head We dont have a quick way of working out which is the node in front of the one we want to remove. For applications where we want quick access to the predecessor node of any node, we use a doubly linked list. A list in which we can go in both directions. ADS2 Lecture 5

11 Doubly Linked Lists (Goodrich § 3.3) A doubly linked list is a concrete data structure consisting of a sequence of nodes Each node stores –element –link to the next node –link to previous node ADS2 Lecture 5 next elem node prev AB C head

Doubly linked lists contd. 12 /**Node of a doubly linked list of strings*/ public class DNode { private String element; //String element stored by a node private DNode next,prev; /**Constructor that creates a node with given fields*/ public DNode(String e, DNode p, DNode n){ element=e; prev=p; next=n; } /** Returns the element of this node */ public String getElement(){return element;} /**Returns the previous node of this node */ public DNode getPrev(){return prev;} /**Returns the next node of this node*/ public DNode getNext(){return next;} /**Sets the element of this node */ public void setElement(String newElem){element=newElem;} /**Sets the previous node of this node */ public void setPrev(DNode newPrev){prev=newPrev;} /**Sets the next node of this node */ public void setNext(DNode newNext){next=newNext;} } ADS2 Lecture 5

13 ADS2 Lecture 5 Insertion into the middle of a doubly linked list insert here node d node a node b node c node anode d node b node c make node ds prev link point to node a make node ds next link point to node b make node bs prev link point to node d make node as next link point to node d

ADS2 Lecture 514 Removal from the middle of a doubly linked list node anode d node b node c remove this node make node as next link point to node d.next make node bs prev link point to node d.prev node a node b node c

ADS2 Lecture 5 15 /**Inserts the given node z before the given node v*/ //assuming non-empty list public void addBefore(DNode v, DNode z){ DNode u=getPrev(v); z.setPrev(u); z.setNext(v); v.setPrev(z); u.setNext(z); } Java fragments for insertion and deletion into/from doubly linked list (assume removed node not head or last node). /**Removes the given node v from the list */ public void remove(DNode v){ DNode u=v.getPrev(); DNode w =v.getNext(); w.setPrev(u); u.setNext(w); v.setPrev(null); v.setNext(null); } unlink the node from the list

More on Doubly linked lists 16 Goodrich (p.133) has an implementation of insertion sort for doubly linked lists (+ Java code). Probably easier to just stick to maintaining an ordered list, and using the copying technique as for singly linked lists, for now. We will come back to linked lists when we look at recursion. Some of the methods we have already seen can be implemented recursively. ADS2 Lecture 5 Full implementation of doubly linked list available in Goodrich. Note use of exceptions.

ADS2 Lecture 518 /**Node for a generic singly linked list*/ public class SNodeGen { private E element; private SNodeGen next; /**Creates a node with null references to its element and *next node*/ public SNodeGen(){ this(null,null); } /**Creates a node with the given element and next node */ public SNodeGen(E e, SNodeGen n){ element=e; next=n; } Java code for a node of a generic singly linked list: SNodeGen.java

ADS2 Lecture 519 /**getters and setters */ public E getElement() { return element; } public void setElement(E e) { this.element = e; } public SNodeGen getNext() { return next; } public void setNext(SNodeGen n) { this.next = n; }