adding animation
This commit is contained in:
@@ -141,27 +141,29 @@ assert (*itr == 100); // might seem ok... but rewrite the code to avoid this!
|
|||||||
template <class T>
|
template <class T>
|
||||||
class Node {
|
class Node {
|
||||||
public:
|
public:
|
||||||
T value;
|
T value;
|
||||||
Node* ptr;
|
Node* ptr;
|
||||||
};
|
};
|
||||||
int main() {
|
int main() {
|
||||||
Node<int>* ll; // ll is a pointer to a (non-existent) Node
|
Node<int>* ll; // ll is a pointer to a (non-existent) Node
|
||||||
ll = new Node<int>; // Create a Node and assign its memory address to ll
|
ll = new Node<int>; // Create a Node and assign its memory address to ll
|
||||||
ll->value = 6; // This is the same as (*ll).value = 6;
|
ll->value = 6; // This is the same as (*ll).value = 6;
|
||||||
ll->ptr = NULL; // NULL == 0, which indicates a "null" pointer
|
ll->ptr = NULL; // NULL == 0, which indicates a "null" pointer
|
||||||
Node<int>* q = new Node<int>;
|
Node<int>* q = new Node<int>;
|
||||||
q->value = 8;
|
q->value = 8;
|
||||||
q->ptr = NULL;
|
q->ptr = NULL;
|
||||||
// set ll's ptr member variable to
|
// set ll's ptr member variable to
|
||||||
// point to the same thing as variable q
|
// point to the same thing as variable q
|
||||||
ll->ptr = q;
|
ll->ptr = q;
|
||||||
cout << "1st value: " << ll->value << "\n"
|
cout << "1st value: " << ll->value << "\n"
|
||||||
<< "2nd value: " << ll->ptr->value << endl;
|
<< "2nd value: " << ll->ptr->value << endl;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
- Play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/lists/creation/index.html) to see how this program works.
|
||||||
|
|
||||||
## 10.5 Definition: A Linked List
|
## 10.5 Definition: A Linked List
|
||||||
|
|
||||||
- The definition is recursive: A linked list is either:
|
- The definition is recursive: A linked list is either:
|
||||||
|
|||||||
Reference in New Issue
Block a user