singly linked lists
play

singly linked lists Sept. 18, 2017 1 Recall last lecture: Java - PowerPoint PPT Presentation

COMP 250 Lecture 5 singly linked lists Sept. 18, 2017 1 Recall last lecture: Java array array array array of int of Shape (unspecified objects type) 34 657 -232 -823 23 1192 0 null 0 null I have drawn each of these as array


  1. COMP 250 Lecture 5 singly linked lists Sept. 18, 2017 1

  2. Recall last lecture: Java array array array array of int of Shape (unspecified objects type) 34 657 -232 -823 23 1192 0 null 0 null I have drawn each of these as array lists. 2

  3. Java ArrayList class https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html It uses an array as the underlying data structure It grows the array (by 50%, not 100%) when the array is full and a new element is added. You don’t use the usual array notation a[ ]. Instead, use get() and set() and other methods. 3

  4. Java generic type An array of what? ArrayList<T> Example: ArrayList< Shape > shape = new ArrayList< Shape >(); // initializes the array length (capacity) to 10 ArrayList< Shape > shape = new ArrayList< Shape >( 23 ); // initializes the array length to 23 4

  5. Java ArrayList object Has private field that 6 holds the number of elements in the list (size). Has a private field that references an array a object. (private) These Shape objects do not belong to the ArrayList object. null null Rather they are referenced by it. 5

  6. Lists • array list • singly linked list (today) • doubly linked list (next lecture) : 6

  7. array list linked list “nodes” null null null null size = 4 7

  8. array list linked list null null null null Array slots are in Linked list “nodes” and consecutive locations objects can be anywhere (addresses) in memory, in memory. but objects can be anywhere. 8

  9. Singly linked list node (“S” for singly) element next class SNode<E> { SNode<E> next; E element; : } e.g. E might be Shape 9

  10. A linked list consists of a sequence of nodes, along with a reference to the first (head) and last (tail) node. head tail 10

  11. class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : private class SNode<E> { // inner class SNode<E> next; E element; : } } 11

  12. Linked list operations • addFirst ( e ) • removeFirst( ) • addLast ( e ) • removeLast( ) • ……. many other list operations 12

  13. addFirst ( ) BEFORE AFTER head head tail tail 13

  14. addFirst ( e ) pseudocode construct newNode newNode.element = e e newNode.next = head next element newNode head etc 14

  15. addFirst ( e ) pseudocode construct newNode newNode.element = e e newNode.next = head next element newNode // edge case if head == null head tail = newNode etc BEFORE AFTER head = newNode e next element size = size+1 newNode head etc 15

  16. removeFirst ( ) BEFORE AFTER head head tail tail 16

  17. removeFirst ( ) pseudocode next element tmp head tmp = head 17

  18. removeFirst ( ) pseudocode next element tmp head tmp = head head = head.next tmp.next = null BEFORE size = size – 1 AFTER next element tmp null head 18

  19. removeFirst() edge cases (size is 0 or 1) next element tmp = head tmp head if (size == 0) BEFORE throw exception head = head.next AFTER tmp.next = null next element size = size – 1 tmp if (size == 0) // size was 1 null tail = null head null 19

  20. Worse Case Time Complexity (N = size) array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 ) 20

  21. Worse Case Time Complexity (N = size) array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 ) addLast O( 1 )* ? removeLast O( 1 ) ? *if array is not full 21

  22. addLast ( ) BEFORE AFTER head head tail tail 22

  23. addLast ( ) newNode = construct a new node newNode.element = the new list element tail.next = newNode : next element // … and then after what // figure shows we do: tail tail = tail.next newNode size = size+1 23

  24. removeLast ( ) BEFORE AFTER head head tail tail Problem: we have no direct way to access the node before tail. 24

  25. removeLast ( ) if (head == tail){ head = null next element tail = null } head else { tmp tmp = head while (tmp.next != tail) tmp = tmp.next tail = tmp tail.next = null tail } size = size - 1 // to return the element, you need to do a bit more 25

  26. Time Complexity (N = list size) array list linked list addFirst O( N ) O( 1 ) removeFirst O( N ) O( 1 ) addLast O( 1 )* O( 1 ) removeLast O( 1 ) O( N ) *if array is not full 26

  27. class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : // various methods private class SNode<E> { // inner class SNode<E> next; E element; : } } 27

  28. class SLinkedList<E> { SNode<E> head; SNode<E> tail; int size; : } head SLinkedList size object 4 tail 28

  29. How many objects? head SLinkedList size object 4 tail 29

  30. How many objects? head size 4 tail 1 + 4 + 4 = 9 30 SLinkedList Shape SNode

  31. Announcements • When I make mistakes on slides/lecture notes/exercises, please email me rather posting on discussion board. (However, compare the date on your version with the one on the public web page. I may have already corrected it.) • Assignment 1 should be posted tomorrow (due in 2 weeks) • Quiz 1 on Monday, Sept 25 (lectures 1-2, 4-6). Online. • Coding tutorials on lists (coming soon) 31

Recommend


More recommend