ECE391 - Ch7 - Vectors - Lists - and Sequences
ECE391 - Ch7 - Vectors - Lists - and Sequences
1. Vectors
1.1. The vector Abstract Data Type
⚫A vector S is an ADT that supports the following
fundamental functions
⚫at(i): Return the element of V with index i; an error
condition occurs if i is out of range.
⚫set(i, e): Replace the element at index i with e; an error
condition occurs if i is out of range.
⚫insert(i, e): Insert a new element e into V to have index i; an
error condition occurs if i is out of range.
⚫erase(i): Remove from V the element at index i; an error
condition occurs if i is out of range.
Vector
rank 0 1 2 r
2
9/4/
1. Vectors
1.1. The vector Abstract Data Type
⚫ Example: Some operations on an empty vector V
No. Operation Output V
1 insert(0,5) - {5}
2 insert(0,3)
3 at(1)
4 insert(2,6)
5 at(3)
6 erase(1)
7 insert(1,4)
8 insert(1,2)
9 insert(4,8)
10 at(2)
11 set(3,7)
3
1. Vectors
1.1. The vector Abstract Data Type
Realization of deque by means of a vector
Deque Function Realization with vector function
size() size()
isEmpty() isEmpty()
front() At(0)
back() At(size() – 1)
insertFront(e) insert (0, e)
insertBack(e) insert (size(), e)
eraseFront() erase(0)
eraseBack() erase(size()-1)
4
9/4/
1. Vectors
1.2. A Simple Array-Based Implementation
⚫InsertAtRank
Algorithm insertAtRank(r, e)
for i=n-1, n-2, …, r do
A[i+1] <- A[i]
A[r] <- e
n <- n+1
1. Vectors
1.2. A Simple Array-Based Implementation
⚫RemoveAtRank(r)
for i = r, r+1, …, n-2 do
A[i] <- A[i+1]
n <- n-1
6
9/4/
1. Vectors
1.3. STL vector class
Function Description
size() returns the size of the vector
empty() returns a Boolean indicating whether the vector is empty
capacity() returns the current capacity of the vector
operator[r] returns a reference to the element at rank r (no index
check)
at[r] returns a reference to the element at rank r (index check)
front() returns a reference to the first element
back() returns a reference to the last element
push_back inserts e at the end of the vector
(e)
pop_back() remove the last element
vector(n) creates a vector of size n (default 0)
insert(i, e) Insert a new element e at the index i 7
Class Assignment
⚫Write a C++ program that creates a vector and store
Alphabet characters [A-Z] in that vector.
8
9/4/
2.List
⚫List is a collection of “nodes”
⚫Node of singly linked list:
⚫element
⚫link to the next node
⚫Node of doubly linked list
⚫element
⚫link to the previous node
⚫link to the next node
2. List
Singly Links List and Doubly Linked List
Singly
linked list
Doubly
linked list
10
9/4/
2. List
⚫List is an ADT which supports the following functions:
⚫begin(): return the position of the first element of S
Input: None; Output: Position
⚫end(): return the position of the last element of S
Input: None; Output: Position
⚫insertFront(e): Insert a new element e into S as the first element
Input: Object e; Output: None
⚫insertBack(e): Insert a new element e into L as the last element
Input: Object e; Output: None
⚫insert(p,e): Insert a new element e into L before position p in L.
Input: Object e, position p; Output: None
⚫eraseFront(): Remove the first element of L.
Input: None; Output: None
⚫eraseBack(): Remove the last element of L.
Input: None; Output: None
⚫erase(p): Remove from L the element at position p; invalidates p as
a position.
Input: None; Output: None
11
2. List
No. Operation Output S
1 insertFront(10)
2 p = begin()
3 insertBack(6)
4 q = p; ++q
5 p == begin()
6 insert(q,2)
7 *q = 7
8 insertFront(8)
9 eraseBack()
10 erase(p)
11 eraseFront()
12
9/4/
2. List
The STL List Class
Function Description
size() returns the size of the list
empty() returns a Boolean indicating whether the
list is empty
front() returns a reference to the first element
back() returns a reference to the last element
push_front(e) insert e at the beginning of the list
push_back(e) insert e at the end of the list
pop_front() removes the list element
pop_back() removes the last element
list() creates an empty list
13
Class Assignment
⚫Write a C++ program that perform operations of a
STL list as shown in the following table.
Operation
insertFront(10)
insertAfter(p1, 2)
insertBefore(p2, 3)
insertFront(9)
before(p3)
swapElements(p1, p2)
erase(p2)
14
9/4/
3. Sequences
⚫Sequence is an ADT that provides access to its
elements using both ranks and positions, and is a
versatile data structure for a wide variety of
applications.
⚫atIndex(i): return the position of the element at index
i
Input: Integer; Output: Position
⚫IndexOf(p): return the index of the element at
position p.
Input: Position; Output: Integer
15
3. Sequences
Application of Sequences
⚫The Sequence ADT is a basic, general purpose, data
structure for storing an ordered collection of elements
⚫Direct applications:
⚫Generic replacement for stack, queue, vector, or list
small database (e.g., address book)
⚫Indirect applications:
⚫Building block of more complex data structures
16
9/4/
3. Sequences
Sequence Implementations
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
17
3. Sequences
Array-based Implementation
⚫We use a elements
circular array
storing positions
⚫A position
object stores:
⚫Element
⚫Rank 0 1 2 3
18
9/4/
Class Assignment
⚫Implement Sequence ADT in C++ using array
19
Class Assignment
#include <iostream>
#include <vector> What is the output screen ?
using namespace std;
void main() {
int S,i;
vector <int> Vect;
Vect.push_back(3);
Vect.push_back(5);
Vect.pop_back();
Vect.push_back(12);
Vect.push_back(15);
Vect.insert(Vect.begin( ) + 1, 40 );
S = Vect.size();
for (i=0; i<S; i++)
cout << Vect.at(i) << '\n';
cout << Vect.empty() << '\n';
cout << Vect.at(2) << '\n';
getchar();
}
20
9/4/
#include <iostream>
Class Assignment
#include <list>
using namespace std;
void main() { What is the output screen ?
list <int> L1,L2;
list <int>::iterator L1_Iter;
L1.push_front(5);
L1.push_back(10);
L1.push_front(8);
L1.push_back(2);
L1.pop_front();
L1.push_back(1);
L2.push_back( 10 );
L2.push_back( 20 );