adding animation

This commit is contained in:
Jidong Xiao
2023-10-02 10:58:42 -04:00
parent f04c83f428
commit 8e2189de61

View File

@@ -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;
} }
``` ```
![alt text](linking.png "linking") ![alt text](linking.png "linking")
- 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: