# Chapter 3: Linked List Tutor: Angie Hui

## Presentation on theme: "Chapter 3: Linked List Tutor: Angie Hui"— Presentation transcript:

Chapter 3: Linked List Tutor: Angie Hui
CS154 Data Structure in C Chapter 3: Linked List Tutor: Angie Hui

Deleting Nodes Interchanging Nodes Losing nodes Garbage Collection

What is a Linked List? A linked list is a dynamic variable that contains a number of nodes and each node is connected by pointers. Types of Linked list Unidirectional linked list (or Singly Linked List) Bidirectional linked list (or Doubly Linked List) Circular linked list Unidirectional + Circular linked list Bidirectional + Circular linked list

head 11 13 15 38 The list goes in only one direction It can only go forward. It cannot go back. So, it’s important to have a fixed pointer to point to the first node of the list. Otherwise, when you go to the 3rd node, the previous 2 nodes will be lost. We normally call the fixed pointer, head.

head 11 13 15 38 All the nodes in a linked list must have the same structure. Last node references to NULL. To implement the node for the above unidirectional linked list, we can use the struct in the next slide.

struct Node { int item; struct Node *next; };

node1 50 To create a new node, pointed by “node1”: struct Node *node1; node1 = (struct node*)malloc(sizeof(struct Node)); node1item = 50; node1next = NULL;

node2 12 To create another new node, pointed by “node2”: struct Node *node2; node2 = (struct node*)malloc(sizeof(struct Node)); node2item = 12; node2next = NULL;

node1 node2 50 12 To connect node1 and node2 together: node1next = node2; After that: node2 node1 50 12

To travel the entire linked list
head 11 13 15 38 Because we cannot change the reference of head, we have to use a temporary pointer (let say temp) as a moving pointer to move from the 1st node to the last node.

To travel the entire linked list
head 11 13 15 38 temp First of all, we should make temp to point to the 1st node temp = head;

To travel the entire linked list
head 11 13 15 38 temp If we want to print out the item in the node printf(“%d”, tempitem); Or if we want to update the item in the node to 30 tempitem = 30;

To travel the entire linked list
head 11 13 15 38 temp Then we need to move temp to the next node temp = tempnext;

To travel the entire linked list
head 11 13 15 38 temp If we want to print out the item in the node printf(“%d”, tempitem); Or if we want to update the item in the node to 60 tempitem = 60;

To travel the entire linked list
The step will continue until reaching the last node. We can sum up the steps in the previous slides to come out the the code in the next slide

To travel the entire linked list
temp = head; do { printf(“%d ”, tempitem); temp = tempnext; }while(temp!=NULL); Q: Why can’t we set the condition to tempnext!=NULL ???

To travel the entire linked list
Q: Why can’t we set the condition to tempnext!=NULL ??? Ans: The item of the last node cannot be printed out if you set the condition to tempnext!=NULL!!

Case 1: Adding to the front

Case 2: Adding to the middle
head previous current 11 13 15 38 newNode 14 previousnext = newNode; newNodenext = current;

Case 3: Adding to the end tempnext = newNode;
head temp 11 13 15 38 newNode 60 tempnext = newNode; temp = newNode; (Optional, depends on whether you need to update the temp)

Deleting a node from a linked list

Deleting a node from a linked list
3 cases Deleting at the front Deleting at the middle Deleting at the end

Case 1: Deleting at the front

Case 2: Deleting at the middle
previous current head 11 13 15 38 previous current head 11 13 15 38 previousnext = currentnext; free(current);

Case 3: Deleting at the end
previous current head 11 13 15 38 previous current head 11 13 15 38 previousnext = NULL; free(current);

Interchanging 2 nodes

Interchanging the 2nd and the 3rd nodes
head previous current Original List: 11 13 15 38 2 Updated List: head previous current 11 13 15 38 3 1

Interchanging the 2nd and the 3rd nodes
Code: headnext = current; currentnext = previous; previousnext = currentnext;

Terms Suppose the following struct is used for the example in next few slides struct Node { int item; struct Node* next; };

Terms Using typedef to name a struct Node pointer:
typedef struct Node* NodePtr; Declare a new node pointed by node struct Node* node; Or NodePtr node; Remark: Every variable needs to be declared before use.

Terms Create a new node pointed by node
Case 1: If the node hasn’t been declared struct Node* node; node = (struct Node*malloc(sizeof(struct Node)); Case 2: If the node has been declared already

Go to the nth node for(i=1; i<index; i++) temp = tempnext;
Assume the variable “index” holds the position of the required node. If index=1, that means the 1st node, if 2, that means the 2nd one, and so on. for(i=1; i<index; i++) temp = tempnext; printf(“item = %d”, tempitem);

Empty List To indicate an empty list, we assign Head to NULL

Losing Nodes After executing the below code
Head 11 15 38 NULL Original Linked List After executing the below code Head = (struct Node*)malloc(sizeof(struct Node)); Head 10 11 15 38 NULL Losing Nodes

Losing Nodes If we do this way, Head will now point to a new location and this causes the other nodes in the linked list not pointed by any pointers anymore. These nodes are called losing nodes.

Losing Nodes These losing nodes becomes useless memory locations which cannot be reused again and this will waste memory locations

Comparison to Arrays Compare to array, linked list is easier to manipulate in terms of insertion and deletion. It is easier to insert and delete a node in a linked list compared to array because for array, we will have to copy all the data over to make room for a new data

Garbage Collection Garbage is useless memory location which is lost during manipulation of linked list. Those memory locations are not accessed by any pointers any more and cannot be reused again. Garbages will cause wastage of memory Too many garbages will cause memory overflow To locate and remove all garbages so that they can be reused again are called Garbage Collection

A summary of a linked list
Each node in the linked list contains 2 members, an information field and a next address(pointer) field. The information field contains the actual data and the pointer field contains the address of the next node in the list. The entire linked list is accessed by a pointer variable called Head. Head is NULL if the linked list is empty.

~ END ~

Similar presentations