1 linked lists continued lecture 5 copying and sorting singly linked lists lists with head and last...

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

Upload: nicole-daly

Post on 28-Mar-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

1

Linked Lists Continued

Lecture 5

•Copying and sorting singly linked lists•Lists with head and last nodes•Doubly linked lists

ADS2 Lecture 5

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

Sorting a singly linked list

2

Can’t 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 can’t 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:

nullheadlist to be sorted : list1

head nullmake new empty list, list2Successively 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 list2ADS2 Lecture 5

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

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 2Remove head from list 1

Make head of list1 point to head of list2

ADS2 Lecture 5

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

Singly linked lists with head and tail nodes

4

Makes insertion from end easierEnables 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.

Baltimorehead

Rome Seattle Toronto

last

Singly linked list with head and last nodes:

ADS2 Lecture 5

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

Adding a new node at the tail

5

Add node containing the string “Zurich” to tail of list

head Rome Seattle Toronto

last

Zurichcreate 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

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

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

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

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

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

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

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

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

Page 10: 1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists 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 don’t 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

Page 11: 1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists 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

A B C head

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

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

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

13ADS2 Lecture 5

Insertion into the middle of a doubly linked list

insert here

node d

node a node b node c

node a node d node b node c

• make node d’s prev link point to node a• make node d’s next link point to node b• make node b’s prev link point to node d• make node a’s next link point to node d

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

ADS2 Lecture 5 14

Removal from the middle of a doubly linked list

node a node d node b node c

remove this node

• make node a’s next link point to node d.next• make node b’s prev link point to node d.prev

node a node b node c

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

ADS2 Lecture 515

/**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

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

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.

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

Generic linked lists

ADS2 Lecture 5 17

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

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

ADS2 Lecture 5 18

/**Node for a generic singly linked list*/public class SNodeGen<E>{ private E element; private SNodeGen<E> 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<E> n){ element=e; next=n; }

Java code for a node of a generic singly linked list: SNodeGen.java

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

ADS2 Lecture 5 19

/**getters and setters */ public E getElement() { return element; }

public void setElement(E e) { this.element = e; }

public SNodeGen<E> getNext() { return next; }

public void setNext(SNodeGen<E> n) { this.next = n; }

}

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

ADS2 Lecture 5 20

Java code for a node of a generic singly linked list: SGenLinkedList.java

public class SGenLinkedList<E> {/** Generic singly linked list with just a head node*/ private SNodeGen<E> 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<E> getHead(){ return head; }/**set the head of the list*/ public void setHead(SNodeGen<E> n){ head=n; }/**is the list empty?*/ public boolean isEmpty(){ return(head==null); }

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

ADS2 Lecture 521

/**add a new node at front of list */ public void addFirst(SNodeGen<E> 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<E> temp=head; String myString=""; while(temp!=null){ myString=myString + temp.getElement()+" "; temp=temp.getNext(); } return myString; }}