Professional Documents
Culture Documents
Linked Lists: Data Structures
Linked Lists: Data Structures
Linked Lists
PIC 10B
Todd Wittman
Data Structures
Queues
Dynamic
Binary
Arrays
Linked Lists
Stacks
Search Trees
Hash Tables
Heaps
Erasing in a Vector
Dynamic arrays and vectors allocate sequential memory
blocks.
This is good for indexing. If the 10 block below is at
address 1201, then the 40 block is at address 1204.
But this is bad for inserting and erasing.
If we want to erase the 20 block in the picture below, we
have to shift all the elements over to the right.
10
20
30
40
10
30
40
20
10
10
30
30
40
40
NULL
NULL
20
30
40
10
30
40
NULL
Vector
Linked List
Data access
O(1)
O(N)
Insert / Delete
O(N)
O(1)
30
40
NULL
30
40
NULL
NULL
20
10
20
20
30
40
The 3 Classes
data
next
10
last
20
30
NULL
position
10
last
20
30
NULL
position
We will also use the Iterator to mark the position of a node we want to
erase or insert.
When we erase a node, we will move the iterator to next position in list.
myList.erase(iter);
When we insert, we will insert the given value before the iterator position.
myList.insert(iter, 25);
LinkedList Constructor
The default constructor just sets up an empty list.
template <typename T>
LinkedList<T>::LinkedList( ) {
first = NULL;
last = NULL;
}
The template allows us to store lists of ints, strings, etc.
LinkedList<string> words;
LinkedList<double> numbers;
LinkedList<Matrix<double>> markovChains;
Well discuss the destructor ~LinkedList() later, because it
will use our erase() function.