CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector Array based implementation of Vectors Lists Doubly linked list implementation of Lists Sequences 6/30/2016 CS210-Summer 2005, Lecture 7 1 Practice A palindrome is a String of characters that is equal to its reverse. Write a method palindrome which takes String as argument and determines whether or not the string is a palindrome by using a Stack. 6/30/2016 CS210-Summer 2005, Lecture 7 2 Practice Lets queue of integers is implemented using linked list. Write a method addAll which adds all the elements of queue and returns the sum as result. Suppose a linked list is made from objects of type Node. Write a method reverse that will make a copy of the list with the order of the items of the list reversed. This method takes the Node (head) as parameter and returns the Node (head of the reversed list) as result. 6/30/2016 CS210-Summer 2005, Lecture 7 3 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 7 4 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 7 5 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 7 6 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 7 7 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 7 8 Realization of Deque by means of Vector Deque Method Vector Method first() last() insertFirst(o) insertLast(o) removeFirst() removeLast() elemAtRank(0) elemAtRank(size() – 1) insertAtRank(0, o) insertAtRank(size(), o) removeAtRank(o) removeAtRank(size() – 1) 6/30/2016 CS210-Summer 2005, Lecture 7 9 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 7 10 Simple Array based implementation of Vector Algorithm insertAtRank(r , e): for i = n -1, n-2,….., r do A[i + 1] <- A[i]; A[r] <- e n <- n + 1 Algorithm removeAtRank(r): e <- A[r] for i = r, r+1, ……, n-2 do A[i] <- A[i + 1] n <- n – 1 return e 6/30/2016 CS210-Summer 2005, Lecture 7 11 Performance of a Vector with n elements Method size() Time O(1) isEmpty() O(1) elemAtRank(r) O(1) replaceAtRank(r, e) O(1) insertAtRank(r, e) O(n) removeAtRank(r) O(n) 6/30/2016 CS210-Summer 2005, Lecture 7 12 Looking at analysis more closely insertAtRank(r, e) and removeAtRank(r) take O(n – r + 1) time as only elements at rank r and higher have to be shifted up or down. Inserting and removing at the end using the methods insertAtRank(n, e) and removeAtRank(n-1) take O(1) time each. insertLast and removeLast methods of deque run in O(1) time each. insertFirst and removeFirst run in O(n) time. 6/30/2016 CS210-Summer 2005, Lecture 7 13 An Extendable Array Implementation To fix the problem of fixed size with arrays we can dynamically grow the array. When an overflow occurs (n = N) and method insertAtRank is called, following steps are performed: Allocate a new Array B of capacity 2N Let B[i] <- A[i] for I = 0,….., N-1 Let A <- B we use B as array supporting S Insert the new element in A 6/30/2016 CS210-Summer 2005, Lecture 7 14 The java.util.ArrayList and java.util.Vector Classes Similar functionality as our Vector ADT. Implementation use extendable arrays. Vector ADT Methods elemAtRank(r) replaceAtRank(r, e) insertAtRank(r, e) removeAtRank(r) 6/30/2016 Java.util.ArrayList methods get(r) set(r, e) add(r, e) remove(r) CS210-Summer 2005, Lecture 7 15 Lists Using ranks is not the only way of referring to the place where an element appears in a sequence. If the sequence S is implemented with a singly or doubly linked list, then it is more natural and efficient to use a node instead of a rank. 6/30/2016 CS210-Summer 2005, Lecture 7 16 Node-Based Operations We want to define methods for S (singly or doubly linked list) that takes nodes as parameters and provide nodes as return types. Such methods provide significant speed ups over rank-based methods as finding the rank of the element in a linked list requires searching through the list incrementally, from its beginning or end. 6/30/2016 CS210-Summer 2005, Lecture 7 17 Node Based Operations Example: Lets define some hypothetical methods: removeAtNode(n): removes the element of S stored in node n of the list. What is the running time? insertAfterNode(n , e): inserts the new element e after the node n. 6/30/2016 CS210-Summer 2005, Lecture 7 18 Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as a cell of an array a node of a linked list Just one method: object element(): returns the element stored at the position Lists 19 Position ADT A position is always defined relatively, that is, in terms of its neighbors. In a list, a position p will always be “after” some position q and “before” some position s (unless p is first or last position). A position p, which is associated with some element e in a list S, does not change, even if the rank of e changes in S, unless we explicitly remove e (destroy position p). 6/30/2016 CS210-Summer 2005, Lecture 7 20 List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: Accessor methods: Update methods: size(), isEmpty() Lists first(), last() prev(p), next(p) replace(p, e) insertBefore(p, e), insertAfter(p, e), insertFirst(e), insertLast(e) remove(p) 21 The List ADT Operations insertFirst(8) Output p1(8) S (8) insertAfter(p1, 5) insertBefore(p2, 3) insertFirst(9) p2(5) p3(3) p4(9) (8, 5) (8, 3, 5) (9, 8, 3, 5) prev(p3) last() remove(p4) p1(8) p2(5) 9 (9, 8, 3, 5) (9, 8, 3, 5) (8, 3, 5) replace(p3, 7) insertAfter(first(), 2) 3 p5(2) (8, 7, 5) (8, 2, 7, 5) 6/30/2016 CS210-Summer 2005, Lecture 7 22 Doubly Linked list implementation of List ADT A doubly linked list provides a natural implementation of the List ADT Simply make the nodes of the linked list implement the position ADT. Each node implement the position interface and therefore define a method element(). Nodes themselves acts as positions. Viewed internally by linked list as nodes, but from outside they are viewed as generic positions. 6/30/2016 CS210-Summer 2005, Lecture 7 23 Doubly Linked List Nodes implement Position and store: prev element link to the previous node link to the next node next elem node Special trailer and header nodes nodes/positions header A B C trailer D elements Lists 24 Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p A q B C X p A 6/30/2016 q B X CS210-Summer 2005, Lecture 7 C 25 Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} 6/30/2016 CS210-Summer 2005, Lecture 7 26 Deletion We visualize remove(p), where p = last() A B C A B C p D p D A 6/30/2016 B C CS210-Summer 2005, Lecture 7 27 Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t 6/30/2016 CS210-Summer 2005, Lecture 7 28