Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Linked List Tutor: Angie Hui

Similar presentations


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

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

2 Chapter Objectives Definition of linked list Adding Nodes
Deleting Nodes Interchanging Nodes Losing nodes Garbage Collection

3 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

4 Unidirectional 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.

5 Unidirectional linked list
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.

6 Unidirectional linked list
struct Node { int item; struct Node *next; };

7 Unidirectional linked list
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;

8 Unidirectional linked list
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;

9 Unidirectional linked list
node1 node2 50 12 To connect node1 and node2 together: node1next = node2; After that: node2 node1 50 12

10 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.

11 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;

12 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;

13 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;

14 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;

15 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

16 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 ???

17 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!!

18 Adding a node to a linked list

19 Adding a node to a linked list
3 cases Adding to the front Adding to the middle Adding to the end

20 Case 1: Adding to the front
head 11 13 15 38 newNode 8 newNodenext = head; head = newNode;

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

22 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)

23 Deleting a node from a linked list

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

25 Case 1: Deleting at the front
head 11 13 15 38 temp temp = head; head = headnext; free(temp);

26 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);

27 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);

28 Interchanging 2 nodes

29 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

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

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

32 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.

33 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

34 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);

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

36 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

37 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.

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

39 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

40 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

41 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.

42 ~ END ~


Download ppt "Chapter 3: Linked List Tutor: Angie Hui"

Similar presentations


Ads by Google