280 likes | 293 Views
CS210- Lecture 7 Jun 14, 2005. Agenda Practice Session Vector Array based implementation of Vectors Lists Doubly linked list implementation of Lists Sequences . Practice.
E N D
CS210- Lecture 7Jun 14, 2005 • Agenda • Practice Session • Vector • Array based implementation of Vectors • Lists • Doubly linked list implementation of Lists • Sequences CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 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. CS210-Summer 2005, Lecture 7
Realization of Deque by means of Vector CS210-Summer 2005, Lecture 7
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 1 2 r n -1 N -1 CS210-Summer 2005, Lecture 7
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 CS210-Summer 2005, Lecture 7
Performance of a Vector with n elements CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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 CS210-Summer 2005, Lecture 7
The java.util.ArrayList and java.util.Vector Classes • Similar functionality as our Vector ADT. • Implementation use extendable arrays. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
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 Position ADT Lists
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). CS210-Summer 2005, Lecture 7
The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: size(), isEmpty() Accessor methods: first(), last() prev(p), next(p) Update methods: replace(p, e) insertBefore(p, e), insertAfter(p, e), insertFirst(e), insertLast(e) remove(p) List ADT Lists
The List ADT CS210-Summer 2005, Lecture 7
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. CS210-Summer 2005, Lecture 7
Doubly Linked List • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes prev next elem node trailer nodes/positions header A B C D elements Lists
Insertion • We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C CS210-Summer 2005, Lecture 7
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} CS210-Summer 2005, Lecture 7
p A B C D Deletion • We visualize remove(p), where p = last() A B C p D A B C CS210-Summer 2005, Lecture 7
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 CS210-Summer 2005, Lecture 7