Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views71 pages

01 Basic 1 Array Vector List

The material of learning data structure

Uploaded by

wujink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views71 pages

01 Basic 1 Array Vector List

The material of learning data structure

Uploaded by

wujink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Array, Vector, List

Array
Array

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• char a[5] = {'h', 'e', 'l', 'l', 'o'};


Array

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• char a[5] = {'h', 'e', 'l', 'l', 'o'};

Array: Fixed size, contiguous memory.


Array

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// random access
• char x = a[1]; // x is 'e'
Array

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

a[0] = 'b';

a = ‘b’ ‘e’ ‘l’ ‘l’ ‘o’


Dynamic Allocation of Arrays

• A size-𝑛 array can be created in this way:


char a[n];

• When writing the code, 𝑛 must be known.


• What if 𝑛 is unknown until program is running?
Dynamic Allocation of Arrays

a = NULL

• char* a = NULL;
Dynamic Allocation of Arrays

a = NULL

• char* a = NULL;
• int n; // array size
Dynamic Allocation of Arrays

a = NULL

• char* a = NULL;
• int n; // array size
• cin >> n; // read in the size, e.g., get n=5
Dynamic Allocation of Arrays

a = NULL

• char* a = NULL;
• int n; // array size
• cin >> n; // read in the size, e.g., get n=5
• a = new char[n];
Dynamic Allocation of Arrays

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// store something in the array


• a[0] = 'h';
• a[1] = 'e';

• a[4] = 'o';
Dynamic Allocation of Arrays

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// When done, free memory.


// Otherwise, memory leak can happen.
• delete [] a;
Dynamic Allocation of Arrays

a = NULL

// When done, free memory.


// Otherwise, memory leak can happen.
• delete [] a;
• a = NULL;
Properties of Array

a = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

1. The size is fixed. (New elements cannot be appended.)


2. Random access using a[i] has 𝑂 1 time complexity.
3. Removing an element in the middle has 𝑂 𝑛 time complexity.
(Require moving the remaining items leftward.)
Vector
Vector

• Vector is almost the same as array.


• The main difference is that vector’s capacity can automatically
grow.
• New elements can be appended using push_back() in 𝑂 1
time (on average).
• The last element can be removed using pop_back() in 𝑂 1
time.
Vector

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• vector<char> v = {'h', 'e', 'l', 'l', 'o'};


Vector

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• vector<char> v = {'h', 'e', 'l', 'l', 'o'};

Vector: dynamic size, contiguous memory.


Vector

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// random access
• char x = v[1]; // x is ‘e’
Vector

v = ‘b’
‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// assignment
• v[0] = 'b';
Insert

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘s’

// insert a new element to the end


• v.push_back('s');
Delete

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘s’

back back

// delete the element in the end


• v.pop_back();
Delete

v = ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

// delete an element in the middle


• v.erase(v.begin()+1);
Delete

v = ‘h’ ‘l’ ‘l’ ‘o’

// delete an element in the middle


• v.erase(v.begin()+1);
Delete

v = ‘h’ ‘l’ ‘l’ ‘o’

// delete an element in the middle


• v.erase(v.begin()+1);

𝑂 𝑛 time complexity! Slow!


Vector capacity can grow

• vector<char> v(100);
• cout << v.size(); // print “100”
• cout << v.capacity(); // print “100”
Vector capacity can grow

• vector<char> v(100);
• cout << v.size(); // print “100”
• cout << v.capacity(); // print “100”

• v.push_back('x');
• cout << v.size(); // print “101”
• cout << v.capacity(); // print “200”
Vector capacity can grow

0 0 ⋯ 0 0

100
Vector capacity can grow

0 0 ⋯ 0 0

capacity of vector = 100


size of vector = 100
Vector capacity can grow

0 0 ⋯ 0 0

v.push_back('x');
Vector capacity can grow

0 0 ⋯ 0 0

v.push_back('x');

0 0 ⋯ 0 0 'x' ⋯

capacity of vector = 200


Vector capacity can grow

0 0 ⋯ 0 0

v.push_back('x');

0 0 ⋯ 0 0 'x' ⋯

size of vector = 101


Vector capacity can grow

What happens when size is going to exceed capacity?


Vector capacity can grow

What happens when size is going to exceed capacity?

• Create a new array of capacity 200.


• Copy the 100 elements from the old array to the new.
• Put the new element in the 101st position.
• Free the old array from memory.
List
A Node

Previous
Data
Next
Doubly Linked List

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• list<char> l = {'h', 'e', 'l', 'l', 'o'};


Doubly Linked List

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• list<char> l = {'h', 'e', 'l', 'l', 'o'};

List: dynamic size, not contiguous memory.


Doubly Linked List

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

• cout << l[2]; // does not work


• l[0] = 'a'; // does not work
Doubly Linked List

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

front back

• cout << l.front(); // print 'h'


Doubly Linked List

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

front back

• cout << l.front(); // print 'h'


• cout << 1.back(); // print 'o'
Iterator

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

iter

• list<char>::iterator iter = l.begin();


• cout << *iter; // print 'h'
Iterator

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

iter

• list<char>::iterator iter = l.begin();


• cout << *iter; // print 'h'

• iter++;
Iterator

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

iter

• list<char>::iterator iter = l.begin();


• cout << *iter; // print 'h'

• iter++;
• cout << *iter; // print 'e'
Iterator

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

iter

• *iter = 'a'; // change 'e' to 'a'


Iterator

‘h’ ‘a’ ‘l’ ‘l’ ‘o’

iter

• *iter = 'a'; // change 'e' to 'a'


Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

back

l.push_back('s');
Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

‘s’
back

l.push_back('s');
Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

‘s’

back
l.push_back('s');

Only O(1) time.


Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

front

l.push_front('a');
Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

‘a’
front

l.push_front('a');
Insertion

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

‘a’

front
l.push_front('a');

Only O(1) time.


Delete Node

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

back

l.pop_back();
Delete Node

‘h’ ‘e’ ‘l’ ‘l’

back back

l.pop_back();
Delete Node

‘h’ ‘e’ ‘l’ ‘l’

back

l.pop_back();

Only O(1) time.


Delete Node

‘h’ ‘e’ ‘l’ ‘l’ ‘o’

front

l.pop_front();
Delete Node

‘e’ ‘l’ ‘l’ ‘o’

front front

l.pop_front();
Delete Node

‘e’ ‘l’ ‘l’ ‘o’

front

l.pop_front();

Only O(1) time.


Summary
Properties

Array Vector List

can increase can increase


Size fixed
and decrease and decrease
Properties

Array Vector List

can increase can increase


Size fixed
and decrease and decrease
not
Memory contiguous contiguous
contiguous
Time Complexities

Array Vector List

Rand Access 𝑂 1 𝑂 1 ---


Time Complexities

Array Vector List

Rand Access 𝑂 1 𝑂 1 ---


push_back() --- 𝑂 1 (average) 𝑂 1
Time Complexities

Array Vector List

Rand Access 𝑂 1 𝑂 1 ---


push_back() --- 𝑂 1 (average) 𝑂 1
pop_back() --- 𝑂 1 𝑂 1
Time Complexities

Array Vector List

Rand Access 𝑂 1 𝑂 1 ---


push_back() --- 𝑂 1 (average) 𝑂 1
pop_back() --- 𝑂 1 𝑂 1
insert() --- 𝑂 𝑛 (average) 𝑂 1
Time Complexities

Array Vector List

Rand Access 𝑂 1 𝑂 1 ---


push_back() --- 𝑂 1 (average) 𝑂 1
pop_back() --- 𝑂 1 𝑂 1
insert() --- 𝑂 𝑛 (average) 𝑂 1
erase() --- 𝑂 𝑛 (average) 𝑂 1
Which shall we use?

• Array: Fixed size throughout.


Which shall we use?

• Array: Fixed size throughout.


• Vector:
• Random access (i.e., read or write the 𝑖-th element) is fast.
• Insertion and deletion at the end are fast.
• Insertion and deletion in the front and middle are slow.
Which shall we use?

• Array: Fixed size throughout.


• Vector:
• Random access (i.e., read or write the 𝑖-th element) is fast.
• Insertion and deletion at the end are fast.
• Insertion and deletion in the front and middle are slow.
• List:
• Sequentially visiting elements is fast; random access is not allowed.
• Frequent insertion and deletion at any position are OK.
Thank You!

You might also like