summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorloshprung <lshprung@scu.edu>2020-01-31 11:25:01 -0800
committerloshprung <lshprung@scu.edu>2020-01-31 11:25:01 -0800
commit685e4c9e8272aa15bf16357ff4a7c79998733d7b (patch)
tree169e52e7f020d6badc1ada2afaa514b6f61d874e
parentbf8af6c4697cf380445bc028109200d3a4cf288c (diff)
Post-class 01/31
-rw-r--r--01-29.md4
-rw-r--r--01-31.md95
2 files changed, 99 insertions, 0 deletions
diff --git a/01-29.md b/01-29.md
index aa13f39..c77ac6e 100644
--- a/01-29.md
+++ b/01-29.md
@@ -149,3 +149,7 @@ Visual Representation of the previous example:
| - | - | - | - | - | - | - | - | - | - |
| **Before:** | head | -> | n1 | -> | n2 (q) | -> | n3 | | |
| **After:** | head | -> | n1 | -> | n2 (q) | -> | inserted n (p) | -> | n3 |
+
+---
+
+[-> Notes 01/31](01-31.md)
diff --git a/01-31.md b/01-31.md
new file mode 100644
index 0000000..4158e08
--- /dev/null
+++ b/01-31.md
@@ -0,0 +1,95 @@
+[\<- Notes 01/29](01-29.md)
+
+---
+
+# Linked Lists Continued
+
+Recall: Linked List Declaration:
+
+```
+#define NODE struct node
+
+struct node{
+ char name[20];
+ int number;
+ NODE *next;
+};
+
+NODE *head = NULL;
+```
+
+## Deleting a Node
+
+- if the Linked List only has one Node:
+
+```
+head->next = NULL;
+free(p); //where p is a pointer to the deleted node
+```
+
+- if the Linked List has more than one Node and you want to delete the first Node:
+
+| Visual Representation | | | | | | | | | |
+| - | - | - | - | - | - | - | - | - | - |
+| **Before:** | head | -> | n1 | -> | n2 | -> | n3 | | |
+| **After:** | head | -> | n2 | -> | n3 | | | | |
+
+```
+head = head->next;
+free(p); //where p is a pointer to the deleted node
+```
+
+the order of steps is important (freeing memory should always be the last step)
+
+- Find nodes with matching numbers 'n' (p and q are pointers pointing to the nodes they are in):
+
+| Visual Representation | | | | | | | | | |
+| - | - | - | - | - | - | - | - | - | - |
+| **Before:** | head | -> | n1 (p) (q)| -> | n2 | -> | n3 | | |
+
+```
+NODE *p, *q;
+p = q = head;
+
+while(p->number != n && p != NULL){
+ q = p;
+ p = p->next; //traverse in n1 until it finds 'n' or it gets to the end (NULL);
+}
+```
+
+| Visual Representation | | | | | | | | | |
+| - | - | - | - | - | - | - | - | - | - |
+| **After above While loop:** | head | -> | n1 | -> | n2 (q)| -> | n3 (p)| | |
+
+```
+//eventually...
+q->next = NULL;
+free(p);
+```
+
+Conditional Statements to determine info about the above Linked List (where we want to delete the Node 'p' is pointing to):
+
+```
+if(head->next == NULL){ //there is only one node (head)
+ head = head->next;
+ free(p);
+}
+
+else{
+ if(p == head){ //this is the First Node
+ head = head->next;
+ free(p);
+ }
+ else if(p->next == NULL){ //this is the last node
+ q->next = NULL;
+ free(p);
+ }
+ else{ //the node is somewhere in the middle of the list
+ q->next = p->next;
+ free(p);
+ }
+}
+```
+
+- It can be helpful to define a tail pointer in addition to the head pointer
+ - the tail pointer would point to the end of the list