adding the remaining of lecture 10
This commit is contained in:
@@ -161,12 +161,147 @@ cout << "1st value: " << ll->value << "\n"
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 10.5 Iterator Declarations and Operations
|
## 10.5 Definition: A Linked List
|
||||||
|
|
||||||
to be added soon.
|
- The definition is recursive: A linked list is either:
|
||||||
|
- Empty, or
|
||||||
|
- Contains a node storing a value and a pointer to a linked list.
|
||||||
|
- The first node in the linked list is called the head node and the pointer to this node is called the head pointer.
|
||||||
|
The pointer’s value will be stored in a variable called head.
|
||||||
|
|
||||||
## 10.6 Leetcode Exercises
|
## 10.6 Visualizing Linked Lists
|
||||||
|
|
||||||
|
- The head pointer variable is drawn with its own box. It is an individual variable. It is important to have a
|
||||||
|
separate pointer to the first node, since the “first” node may change.
|
||||||
|
- The objects (nodes) that have been dynamically allocated and stored in the linked lists are shown as boxes,
|
||||||
|
with arrows drawn to represent pointers.
|
||||||
|
- Note that this is a conceptual view only. The memory locations could be anywhere, and the actual values
|
||||||
|
of the memory addresses aren’t usually meaningful.
|
||||||
|
- The last node MUST have NULL for its pointer value — you will have all sorts of trouble if you don’t ensure this!
|
||||||
|
- You should make a habit of drawing pictures of linked lists to figure out how to do the operations.
|
||||||
|
|
||||||
|
## 10.7 Basic Mechanisms: Stepping Through the List
|
||||||
|
|
||||||
|
- We’d like to write a function to determine if a particular value, stored in x, is also in the list.
|
||||||
|
- We can access the entire contents of the list, one step at a time, by starting just from the head pointer.
|
||||||
|
– We will need a separate, local pointer variable to point to nodes in the list as we access them.
|
||||||
|
– We will need a loop to step through the linked list (using the pointer variable) and a check on each value
|
||||||
|
|
||||||
|
## 10.8 Exercise: Write is there
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template <class T> bool is_there(Node<T>* head, const T& x) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- If the input linked list chain contains n elements, what is the order notation of is there?
|
||||||
|
|
||||||
|
## 10.9 Basic Mechanisms: Pushing on the Back
|
||||||
|
|
||||||
|
- Goal: place a new node at the end of the list.
|
||||||
|
- We must step to the end of the linked list, remembering the pointer to the last node.
|
||||||
|
- This is an O(n) operation and is a major drawback to the ordinary linked-list data structure we are
|
||||||
|
discussing now. We will correct this drawback by creating a slightly more complicated linking structure
|
||||||
|
in our next lecture.
|
||||||
|
- We must create a new node and attach it to the end.
|
||||||
|
- We must remember to update the head pointer variable’s value if the linked list is initially empty.
|
||||||
|
– Hence, in writing the function, we must pass the pointer variable by reference.
|
||||||
|
|
||||||
|
## 10.10 Exercise: Write push_front
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template <class T> void push_front( Node<T>* & head, T const& value ) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- If the input linked list chain contains n elements, what is the order notation of the implementation of push front?
|
||||||
|
|
||||||
|
## 10.11 Exercise: Write push_back
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template <class T> void push_back( Node<T>* & head, T const& value ) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- If the input linked list chain contains n elements, what is the order notation of this implementation of
|
||||||
|
push back?
|
||||||
|
|
||||||
|
## 10.12 Inserting a Node into a Singly-Linked List
|
||||||
|
|
||||||
|
- With a singly-linked list, we’ll need a pointer to the node before the spot where we wish to insert the new
|
||||||
|
item.
|
||||||
|
- If p is a pointer to this node, and x holds the value to be inserted, then the following code will do the insertion.
|
||||||
|
Draw a picture to illustrate what is happening.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
Node<T> * q = new Node<T>; // create a new node
|
||||||
|
q -> value = x; // store x in this node
|
||||||
|
q -> next = p -> next; // make its successor be the current successor of p
|
||||||
|
p -> next = q; // make p's successor be this new node
|
||||||
|
```
|
||||||
|
|
||||||
|
- Note: This code will not work if you want to insert x in a new node at the front of the linked list. Why not?
|
||||||
|
|
||||||
|
## 10.13 Removing a Node from a Singly-Linked List
|
||||||
|
|
||||||
|
- The remove operation itself requires a pointer to the node before the node to be removed.
|
||||||
|
- Suppose p points to a node that should be removed from a linked list, q points to the node before p, and head
|
||||||
|
points to the first node in the linked list. Note: Removing the first node is an important special case.
|
||||||
|
- Write code to remove p, making sure that if p points to the first node that head points to what was the second
|
||||||
|
node and now is the first after p is removed. Draw a picture of each scenario.
|
||||||
|
|
||||||
|
## 10.14 Exercise: Singly-Linked List Copy
|
||||||
|
|
||||||
|
Write a recursive function to copy all nodes in a linked list to form an new linked list of nodes with identical structure
|
||||||
|
and values. Here’s the function prototype:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template <class T> void CopyAll(Node<T>* old_head, Node<T>*& new_head) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 10.15 Exercise: Singly-Linked List Remove All
|
||||||
|
|
||||||
|
Write a recursive function to delete all nodes in a linked list. Here’s the function prototype:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
template <class T> void RemoveAll(Node<T>*& head) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- ## 10.16 Leetcode Exercises
|
||||||
|
|
||||||
- [Leetcode problem 27: Remove Element](https://leetcode.com/problems/remove-element/). Solution: [p27_removeelement.cpp](../../leetcode/p27_removeelement.cpp)
|
- [Leetcode problem 27: Remove Element](https://leetcode.com/problems/remove-element/). Solution: [p27_removeelement.cpp](../../leetcode/p27_removeelement.cpp)
|
||||||
- [Leetcode problem 58: Length of Last Word](https://leetcode.com/problems/length-of-last-word/). Solution: [p58_lengthoflastword.cpp](../../leetcode/p58_lengthoflastword.cpp)
|
- [Leetcode problem 58: Length of Last Word](https://leetcode.com/problems/length-of-last-word/). Solution: [p58_lengthoflastword.cpp](../../leetcode/p58_lengthoflastword.cpp)
|
||||||
- [Leetcode problem 283: Move Zeroes](https://leetcode.com/problems/move-zeroes/). Solution: [p283_movezeroes.cpp](../../leetcode/p283_movezeroes.cpp)
|
- [Leetcode problem 283: Move Zeroes](https://leetcode.com/problems/move-zeroes/). Solution: [p283_movezeroes.cpp](../../leetcode/p283_movezeroes.cpp)
|
||||||
|
-->
|
||||||
|
|||||||
Reference in New Issue
Block a user