Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.

Similar presentations


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:

1 1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5

2 Sorting a singly linked list 2 Cant really use equivalent of insertion sort or selection sort (as used for arrays) as we can only access a given node by starting from the head (so cant swap nodes at position i with node at position j) In general, best to keep list sorted from the start (using addValueInOrder, say), or do the following: nullhead list to be sorted : list1 headnull make new empty list, list2 Successively remove nodes from head of list1, and add new node containing same element as old node, to list 2 (using addValueInOrder) Finally make head of list1 point to head of list2 ADS2 Lecture 5

3 3 public class TestSortingSLinkedList { public static void main(String[] args) { SLinkedList1 mySLinkedList1=new SLinkedList1(); mySLinkedList1.addValue("alice"); mySLinkedList1.addValue("bob"); mySLinkedList1.addValue("craig"); mySLinkedList1.addValue("douglas"); System.out.println("The list is originally: " + mySLinkedList1); SLinkedList1 mySLinkedList2=new SLinkedList1(); String s=""; while(!mySLinkedList1.isEmpty()){ s=mySLinkedList1.getHead().getElement(); mySLinkedList2.addValueInOrder(s); mySLinkedList1.removeFirst(); } mySLinkedList1.setHead(mySLinkedList2.getHead()); System.out.println("The list is finally: " + mySLinkedList1); } Sorting a list via copying, in Java Note have added getHead and setHead methods to SLinkedList1 new empty list keep going until first list emptied What value at head of list 1? Add new node with that value to list 2 Remove head from list 1 Make head of list1 point to head of list2 ADS2 Lecture 5

4 Singly linked lists with head and tail nodes 4 Makes insertion from end easier Enables us to keep things in first in, first out order e.g lab exercise 1. Need to read contents from a text file and create a linked list with PNodes containing references to variables of type Person. Do this by adding at the tail. Also need to then add new entries in correct order. Do this by starting at the head as before. Baltimore head Rome Seattle Toronto last Singly linked list with head and last nodes: ADS2 Lecture 5

5 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

6 Other operations 6 If operation could affect the tail of the list, need to redirect last. E.g. Adding new node to head of list - if head ==null then same as adding to tail of list - else add to head as before Adding new node in order -if adding to empty list, same as adding tail to list as before -if list not empty, but new node to be added to end of list, same as adding to tail -else add in order as before ADS2 Lecture 5

7 Java code for part of the SlinkedList2 list (with head and tail) 7 /** Singly linked list with head and tail, storing strings*/ public class SLinkedList2 { private Node head; //head node of the list private Node last; //tail of the list /** Default constructor that creates an empty list */ public SLinkedList2(){ head=null; last=null; } /**is the list empty?*/ public boolean isEmpty(){ return(head==null); } ADS2 Lecture 5

8 8 /**Add a new node n to tail of list*/ public void addLast(Node n){ if (isEmpty()) {head=n; last=n;} else {last.setNext(n);last=n;} } /**Add note to tail of list containing a given value*/ public void addLastValue(String s){ Node temp = new Node(s,null); addLast(temp); } /**add a new node at front of list */ public void addFirst(Node n){ if (isEmpty()){head=n;last=n;} else{n.setNext(head);head=n;} } /** add node to front of list containing a given value */ public void addValue(String s){ Node temp = new Node(s,null); addFirst(temp); } ADS2 Lecture 5

9 Use of linked lists in lab1 (b) 9 Need a linked list with head and tail to hold nodes of type Person The class for the nodes has been completed for you ( Pnode ). Make sure you understand it. Need to complete the linked list implementation MembersLinkedList. This is a linked list of type PNode. Need to adapt the methods we have just seen (not by much) and implement others: - just slightly modified from the ones for SLinkedList1 (e.g. addInOrder) - completely new ones (like constructor method taking stream from file, public MembersLinkedList(Scanner myScanner) - need to follow the one you have already done for part (A) and adapt for linked list ADS2 Lecture 5

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

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

14 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

15 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

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

17 Generic linked lists ADS2 Lecture 517 Rather than use a linked list that can only store objects of a certain type, can use a generic linked list (either generic singly linked list or generic doubly linked list). Need a generic node to implement the list You will need to implement a generic singly linked list in lab2 (= assessment 1). But not for lab 1. (You are required to implement a specific linked list for lab1) Lab 2 will also use recursion (see next 2 lectures) The generic singly linked list here has only a few basic methods No addInOrder method say. This would add the complication of needing to insist that the type E is comparable.

18 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

19 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; }

20 ADS2 Lecture 520 Java code for a node of a generic singly linked list: SGenLinkedList.java public class SGenLinkedList { /** Generic singly linked list with just a head node*/ private SNodeGen head; //head node of the list /** Default constructor that creates an empty list */ public SGenLinkedList(){ head=null; } /**return the head of the list*/ public SNodeGen getHead(){ return head; } /**set the head of the list*/ public void setHead(SNodeGen n){ head=n; } /**is the list empty?*/ public boolean isEmpty(){ return(head==null); }

21 ADS2 Lecture 5 21 /**add a new node at front of list */ public void addFirst(SNodeGen n){ n.setNext(head); head=n; } /** remove node from front of list */ public void removeFirst(){ if (!isEmpty()) head=head.getNext(); } /** String representation of list */ public String toString(){ SNodeGen temp=head; String myString=""; while(temp!=null){ myString=myString + temp.getElement()+" "; temp=temp.getNext(); } return myString; }


Download ppt "1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5."

Similar presentations


Ads by Google