Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS154 Data Structure in C Chapter 3 : Linked List Tutor: Angie Hui."— Presentation transcript:

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

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

3 3 What is a Linked List? 4 A linked list is a dynamic variable that contains a number of nodes and each node is connected by pointers. 4 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 4 Unidirectional linked list 4 The list goes in only one direction 4 It can only go forward. It cannot go back. 4 So, its important to have a fixed pointer to point to the first node of the list. Otherwise, when you go to the 3 rd node, the previous 2 nodes will be lost. 4 We normally call the fixed pointer, head head

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

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

7 7 Unidirectional linked list struct Node *node1; node1 = (struct node*)malloc(sizeof(struct Node)); node1 item = 50; node1 next = NULL; 50 node1 To create a new node, pointed by node1:

8 8 Unidirectional linked list struct Node *node2; node2 = (struct node*)malloc(sizeof(struct Node)); node2 item = 12; node2 next = NULL; 12 node2 To create another new node, pointed by node2:

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

10 10 To travel the entire linked list 4 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 1 st node to the last node head

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

12 12 To travel the entire linked list 4 If we want to print out the item in the node head temp printf(%d, temp item); 4 Or if we want to update the item in the node to 30 temp item = 30;

13 13 To travel the entire linked list 4 Then we need to move temp to the next node head temp temp = temp next;

14 14 To travel the entire linked list 4 If we want to print out the item in the node head temp printf(%d, temp item); 4 Or if we want to update the item in the node to 60 temp item = 60;

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

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

17 17 To travel the entire linked list Q: Why cant 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 18 Adding a node to a linked list

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

20 20 Case 1: Adding to the front newNode next = head; head = newNode; newNode head

21 21 Case 2: Adding to the middle previous next = newNode; newNode next = current; 1414 newNode head previous current

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

23 23 Deleting a node from a linked list

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

25 25 Case 1: Deleting at the front temp = head; head = head next; free(temp); head temp

26 26 Case 2: Deleting at the middle previous next = current next; free(current); current head previous current head previous

27 27 Case 3: Deleting at the end previous next = NULL; free(current); current head previous current head previous

28 28 Interchanging 2 nodes

29 29 Interchanging the 2 nd and the 3 rd nodes head currentprevious head currentprevious Original List: Updated List:

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

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

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

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

34 34 Go to the n th node 4 Assume the variable index holds the position of the required node. If index=1, that means the 1 st node, if 2, that means the 2 nd one, and so on. for(i=1; i

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

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

37 37 Losing Nodes 4 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 38 Losing Nodes 4 These losing nodes becomes useless memory locations which cannot be reused again and this will waste memory locations

39 39 Comparison to Arrays 4 Compare to array, linked list is easier to manipulate in terms of insertion and deletion. 4 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 40 Garbage Collection 4 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. 4 Garbages will cause wastage of memory 4 Too many garbages will cause memory overflow 4 To locate and remove all garbages so that they can be reused again are called Garbage Collection

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

42 42 ~ END ~


Download ppt "1 CS154 Data Structure in C Chapter 3 : Linked List Tutor: Angie Hui."

Similar presentations


Ads by Google