17
Click to add Title Comparison between vector List & Sequence e-Infochips Institute of Training Research and Aca Prepared By: Savani Nirali Sanghani Monika

Vectors,squence & list

Embed Size (px)

Citation preview

Page 1: Vectors,squence & list

Click to add Title

Comparison between vector List & Sequence

e-Infochips Institute of Training Research and Academics Limited

Prepared By:Savani Nirali

Sanghani Monika

Page 2: Vectors,squence & list

2

Sequence: Collection of elements organized in a specified order. Allows random access by rank or position.

Stack: Sequence that can be accessed in LIFO fashion.

Queue: Sequence that can be accessed in FIFO fashion.

Vector: Sequence with random access by rank. The term rank to refer to the index of an element in a vector.

List: Sequence with random access by position. The abstract node objects positions to refer to the elements of list ADT.

Introduction

Page 3: Vectors,squence & list

• The rank of an element e in a sequence S is the number of elements that are before e in S.

• A linear sequence that supports access to its elements by their ranks is called a vector.

• The vector ADT extends the notion of array by storing a sequence of arbitrary objects.

• An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)

• An exception is thrown if an incorrect rank is specified (e.g., a negative rank)

Vector ADT

Page 4: Vectors,squence & list

• Main vector ADT operations:

elemAtRank(int r): returns the element at rank r without removing it replaceAtRank(int r, Object o): replace the element at rank r with o insertAtRank(int r, Object o): insert a new element o to have rank r removeAtRank(int r): removes the element at rank r

• Additional operations: size() and isEmpty().

Page 5: Vectors,squence & list

A Simple Array-Based Implementation of Vector:

• Use an array V of size N• A variable n keeps track of the size of the vector (number of elements stored).• Operation elemAtRank(r) is implemented in O(1) time by returning V[r].

Insertion:

• In operation insertAtRank(r,o), we need to make room for the new element by shifting forward the n − r elements V[r], . . ., V[n − 1].• In the worst case (r = 0), this takes O(n) time.

Page 6: Vectors,squence & list

Deletion:

• In operation removeAtRank(r), we need to fill the hole left by the removed element by shifting backward the n − r − 1 elements V[r + 1], . . ., V[n − 1]

• In the worst case (r = 0), this takes O(n) time

Page 7: Vectors,squence & list

Performance:

•In the array based implementation of a vector ADT

The space used by the data structure is O(n)size, isEmpty, elemAtRank and replaceAtRank run in O(1) timeinsertAtRank and removeAtRank run in O(n) time

•If we use the array in a circular fashion, insertAtRank(0) and removeAtRank(0) run in O(1) time

•In an insertAtRank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

Page 8: Vectors,squence & list

Node-Based Operations and Positions:

Position:• We view a list ADT as a container of elements that stores each element at a position and positions are arranged in a linear order

• The position ADT models the notion of place within a data structure where a single object is store.

• A special nullposition refers to no object

• Positions provide a unified view of many ways of storing data, such as a cell of an arraya node of a linked list

List ADT

Page 9: Vectors,squence & list

• Member functions:

• Object& element(): returns the element stored at this position• boolisNull(): returns true if this is a null position

• A position is always defined relatively, that is in term of its neighbors. p will always be "after" some position q and "before" some position s (unless p is the first or last position).

Page 10: Vectors,squence & list

The List Abstract Data Type

• It establishes a before/after relation between positions

• Generic methods: size(), isEmpty()

• Query methods: isFirst(p), isLast(p)

• Accessor methods: first(), last()before(p), after(p)

• Update methods: replaceElement(p, o), swapElements(p, q)insertBefore(p, o) insertAfter(p, o)insertFirst(o), insertLast(o)remove(p)

Page 11: Vectors,squence & list

Doubly Linked List Implementation:

•A doubly linked list provides a natural implementation of the list ADT

•Nodes implement positions and store: elementlink to the previous nodelink to the next node

•Special trailer and header nodes

Page 12: Vectors,squence & list

Insertion:

• We visualize operation insertAfter(p, X) which returns position q.

Page 13: Vectors,squence & list

Deletion:

• We visualize remove(p), where p = last()

Page 14: Vectors,squence & list

The Sequence Abstract Data Type

• The sequence ADT is the union of the vector and list ADTs

• Elements accessed by Rank, or Position

• Generic methods: size(), isEmpty()

• Vector-based methods: elemAtRank(r), replaceAtRank(r, o),insertAtRank(r, o), removeAtRank(r)

• List-based methods: first(), last(), before(p), after(p), replaceElement(p, o), swapElements(p, q), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p)

Sequence ADT

Page 15: Vectors,squence & list

Implementing a Sequence with an Array

• We use a circular array storing positions• A position object stores:

ElementRank

• Indices f and l keep track of first and last positions

Page 16: Vectors,squence & list

Operation Array List

size, isEmpty 1 1

atRank, rankOf, elemAtRank 1 n

first, last, before, after 1 1

replaceElement, swapElements 1 1

replaceAtRank 1 n

insertAtRank, removeAtRank n n

insertFirst, insertLast 1 1

insertAfter, insertBefore n 1

remove n 1

Sequence Implementation:

Page 17: Vectors,squence & list

Thank you