This document compares vector lists and sequences. It discusses that sequences can be accessed by rank or position and defines vectors, lists, stacks and queues as types of sequences. It provides details on vector and list abstract data types (ADTs) including common operations. It describes an array-based implementation of vectors and a doubly linked list implementation of lists. Finally, it introduces the sequence ADT as a combination of vectors and lists and discusses implementing sequences with arrays and comparing the time complexities of common operations between array and linked list implementations.
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Vectors,squence & list
1. Click to add Title
Comparison between vector
List & Sequence
e-Infochips Institute of Training Research and Academics Limited
Prepared By:
Savani Nirali
Sanghani Monika
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
3. • 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
4. • 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().
5. 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.
6. 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
7. 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) time
insertAtRank 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
8. 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 array
a node of a linked list
List ADT
9. • 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).
10. 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)
11. Doubly Linked List Implementation:
•A doubly linked list provides a natural implementation of the list ADT
•Nodes implement positions and store:
element
link to the previous node
link to the next node
•Special trailer and header nodes
14. 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
15. Implementing a Sequence with an Array
• We use a circular array storing positions
• A position object stores:
Element
Rank
• Indices f and l keep track of first and last positions
16. 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: