CS210- Lecture 6 Jun 13, 2005 Announcements Assignment 2 has been posted. Part 1: Programming - Due on 6/19 (Sunday) Part 2: Problems – Due on 6/21 (Tuesday) 6/30/2016 CS210-Summer 2005, Lecture 6 1 Agenda Linked Lists Linked List implementation of Stack Linked List implementation of Queue Double Ended Queue Implementing a deque with a doubly linked list Implementing a Stack with doubly linked list Vectors 6/30/2016 CS210-Summer 2005, Lecture 6 2 Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores head next element link to the next node node elem A 6/30/2016 B C CS210-Summer 2005, Lecture 6 D 3 Inserting at the head Allocate a new node Insert new element Have new node point to old head Update head to point to new node 6/30/2016 CS210-Summer 2005, Lecture 6 4 Removing at the head 6/30/2016 Update head to point to next node in the list Allow garbage collector to reclaim the former first node CS210-Summer 2005, Lecture 6 5 Inserting at the tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node 6/30/2016 Update tail to point to new node CS210-Summer 2005, Lecture 6 6 Removing at tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node. 6/30/2016 CS210-Summer 2005, Lecture 6 7 Analyzing the Singly Linked List Method 6/30/2016 Time Inserting at the head O(1) Inserting at the tail O(1) Deleting at the head O(1) Deleting at the tail O(n) CS210-Summer 2005, Lecture 6 8 Singly Linked List implementation of Stack We can implement a stack with a singly linked list by inserting and removing elements at head. The top element is stored at the first node of the list. The space used is O(n) and each operation of the Stack ADT takes O(1) time. 6/30/2016 CS210-Summer 2005, Lecture 6 9 Singly Linked List implementation of Stack top null top push (“A”) nul l “A” top null “B” 6/30/2016 push (“B”) “A” CS210-Summer 2005, Lecture 6 10 Singly Linked List implementation of Stack top null pop() “A” top pop() null 6/30/2016 CS210-Summer 2005, Lecture 6 11 Singly Linked List implementation of Queue We can implement a queue with a singly linked list The front element is stored at the first node (head) The rear element is stored at the last node (tail) We insert at tail and remove at head Why would it be bad to insert at the head and remove at the tail ? The space used is O(n) and each operation of the Queue ADT takes O(1) time 6/30/2016 CS210-Summer 2005, Lecture 6 12 Singly Linked List implementation of Queue head null tail head enqueue (“A”) null tail “A” head null “A” 6/30/2016 enqueue (“B”) “B” CS210-Summer 2005, Lecture 6 13 Singly Linked List implementation of Queue tail head null dequeue () “B” head null 6/30/2016 dequeue () CS210-Summer 2005, Lecture 6 14 Double Ended Queues (Deque) Deque is a queue like data structure that supports insertion and deletion at both the front and rear of the queue. Also known as “deck” to avoid confusion with the dequeue method of the regular queue ADT. 6/30/2016 CS210-Summer 2005, Lecture 6 15 The Deque ADT insertFirst(o): Insert a new object o at the beginning of the deque. insertLast(o): Insert a new object o at the end of the deque removeFirst(): Remove and return the first element of the deque. removeLast(): Remove and return the last element of the deque. 6/30/2016 CS210-Summer 2005, Lecture 6 16 Implementing a Deque with a Doubly Linked List As deque requires insertion and deletion at both ends of a list, using a singly linked list would be inefficient. There is another kind of linked list that allows us to insert and remove elements at both ends in O(1) time – doubly linked list Node in a double linked list has two references – a next link and a prev link. 6/30/2016 CS210-Summer 2005, Lecture 6 17 Doubly Linked List prev next DLNode header trailer A 6/30/2016 elem B CS210-Summer 2005, Lecture 6 18 Doubly Linked List Header and Trailer are dummy or sentinel nodes. These do not store any element. The header has a valid next reference but a null prev reference. The trailer has a valid prev reference but a null next reference. 6/30/2016 CS210-Summer 2005, Lecture 6 19 Inserting at the head of DLL header trailer A B C 6/30/2016 CS210-Summer 2005, Lecture 6 20 Deleting at the tail of DLL header trailer A 6/30/2016 B CS210-Summer 2005, Lecture 6 21 Insert at the tail of DLL header trailer p A B C 6/30/2016 CS210-Summer 2005, Lecture 6 22 Deleting at the head of DLL header trailer A B Analysis Method size, isEmpty Time O(1) first, last O(1) insertFirst, insertLast O(1) removeFirst, removeLast O(1) 6/30/2016 CS210-Summer 2005, Lecture 6 23 Implementing Stack using a DLL top null top push (“A”) null null “A” top null null “B” 6/30/2016 push (“B”) “A” CS210-Summer 2005, Lecture 6 24 Implementing Stack using a DLL top null null pop () “A” top pop () null 6/30/2016 CS210-Summer 2005, Lecture 6 25 Vector, List and Sequence ADT Suppose we have a collection S of n elements stored in a certain linear order, so that we can refer to the elements is S as first, second and third so on. Such a collection is generically referred to as a sequence. Vector, List and Sequence ADTs are sequences. 6/30/2016 CS210-Summer 2005, Lecture 6 26 Vector, List and Sequence ADT Each represents a collection of linearly arranged elements and provides methods for accessing, inserting and removing arbitrary elements. These sequences differ in the ways in which the different operations are defined. Stacks, Queues and Deques can be viewed as restricted types of sequences that access only first and/or last elements. 6/30/2016 CS210-Summer 2005, Lecture 6 27 Vectors and Array Lists A vector which is also known as array list, is an abstraction and extension of the concrete array data structure. It provides accessor methods that can index into the middle of a sequence and it also provides update methods for adding and removing elements by their indices. We typically use the term rank to refer to the index of an element in a vector. 6/30/2016 CS210-Summer 2005, Lecture 6 28 Vectors and Array Lists We define rank of an element e in S to be the number of elements that are before e in S. First element in S has rank 0 and the last element has rank n – 1. 6/30/2016 CS210-Summer 2005, Lecture 6 29 Vector ADT elemAtRank(r): return the element of S with rank r. Error occurs if r < 0 and r > size() - 1 . replaceAtRank(r, e): Replace with e and return the element at rank r. insertAtRank(r, e): Insert a new element e into S to have rank r. When will error occur? removeAtRank(r): Remove from S the element at rank r. 6/30/2016 CS210-Summer 2005, Lecture 6 30 Realization of Deque by means of Vector Deque Method Vector Method first() last() insertFirst(o) insertLast(o) removeFirst() removeLast() elemAtRank(0) ? ? ? ? ? 6/30/2016 CS210-Summer 2005, Lecture 6 31 Simple Array based implementation of Vector Shifting up for an insertion at rank r ( O(n) ) S 0 1 2 r n -1 N -1 Shifting down for a removal at rank r ( O(n) ) S 0 6/30/2016 1 2 r n -1 N -1 CS210-Summer 2005, Lecture 6 32