Problems
Why Linked Lists?
- What are the performance limitations of vectors?
- What are the space limitations of vectors?
- What capabilities do linked lists provide that vectors do not?
- What capabilities do vectors provice that linked list do not?
- Give an example in which you would utilize a linked list over a vector? What about a vector over a linked list?
- What would be the Big O of inserting a single node into a linked list?
- What is the Big O of remove a single node frome a linked list?
- What is the space complexity of storing nodes in a linked list?
- What is the time complexity of a linear search of a linked list?
- Can you perform a traditional binary serach of a linked list? If so, what is the Big O of the search?
- What is the Big O of printing all elements in a linked list?
Implementation of Linked Lists
A linked list is built using nodes. The following is the implementation of a node for a Singly Linked List in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | #include <iostream>
using namespace std;
template <typename T>
class Node {
T data;
Node<T> *next;
Node() : next(nullptr) {}
};
int main() {
Node<int> *head = nullptr;
}
|
1. Sketch the linked list for the following code snippet. Assume the Node class has been defined. | int main() {
Node<int> *head = new Node<T>();
head->data = 0;
head->next = new Node<T>();
head->next->data = 1;
head->next->next = new Node<T>();
head->next->next->data = 2
}
|
2. Does the following code have a memory leak? | int main() {
Node<int> *head = new Node<T>();
head->data = 0;
Node<int> *newNode = new Node<T>();
newNode->data = 1
head->next = newNode;
newNode->next = new Node<T>();
newNode->next->data = 2;
newNode = newNode->next;
}
|
Forward List - Standard Template Library
CPP Forward List reference: http://www.cplusplus.com/reference/forward_list/forward_list/
- What header needs to be included to utilize forward lists?
- What functions are utilized to add and remove items from the forward list?
- Declare a forward list and push the integers 1, 2, 3. Print the elements stored in the list. In what order are the elements printed? Why?
- What does the following program output?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | #include <iostream>
#include <forward_list>
using namespace std;
int main() {
forward_list<int> list;
for (int i = 0; i < 10; i++)
list.push_front(i);
for (int i = 0; i < 5; i++)
list.pop_front();
cout << endl;
for (int i : list)
cout << i << " ";
}
|
- What does the following program output?
1
2
3
4
5
6
7
8
9
10
11
12 | #include <iostream>
#include <forward_list>
using namespace std;
int main() {
forward_list<int> list;
for (int i = 10; i > 0; i--)
list.push_front(i);
list.pop_front();
cout << list.front();
}
|