1 cs154 data structure in c chapter 3 : linked list tutor: angie hui
TRANSCRIPT
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
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.
38151311head
5
Unidirectional linked list
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.
38151311head
7
Unidirectional linked list
struct Node *node1;node1 = (struct node*)malloc(sizeof(struct Node));
node1item = 50;
node1next = NULL;
50node1
To create a new node, pointed by “node1”:
8
Unidirectional linked list
struct Node *node2;node2 = (struct node*)malloc(sizeof(struct Node));
node2item = 12;
node2next = NULL;
12node2
To create another new node, pointed by “node2”:
9
Unidirectional linked list
node1next = node2;
12node2
To connect node1 and node2 together:
50node1
After that:
12
node2
50node1
10
To travel the entire linked list
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.
38151311head
11
To travel the entire linked list
First of all, we should make temp to point to the 1st node
38151311head
temp
temp = head;
12
To travel the entire linked list
If we want to print out the item in the node
38151311head
temp
printf(“%d”, tempitem);
Or if we want to update the item in the node to 30
tempitem = 30;
13
To travel the entire linked list
Then we need to move temp to the next node
38151311head
temp
temp = tempnext;
14
To travel the entire linked list
If we want to print out the item in the node
38151311head
temp
printf(“%d”, tempitem);
Or if we want to update the item in the node to 60
tempitem = 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 ”, tempitem);
temp = tempnext;
}while(temp!=NULL);
Q: Why can’t we set the condition to tempnext!=NULL ???
17
To travel the entire linked list
Q: Why can’t we set the condition to
tempnext!=NULL ???
Ans: The item of the last node cannot be
printed out if you set the condition to
tempnext!=NULL!!
19
Adding a node to a linked list
3 cases
– Adding to the front
– Adding to the middle
– Adding to the end
21
Case 2: Adding to the middle
previousnext = newNode;
newNodenext = current;
14newNode
head
38151311
previous current
22
Case 3: Adding to the end
tempnext = newNode;
temp = newNode; (Optional, depends on whether you need to update the temp)
60newNode
head
38151311
temp
24
Deleting a node from a linked list
3 cases
– Deleting at the front
– Deleting at the middle
– Deleting at the end
26
Case 2: Deleting at the middle
previousnext = currentnext;
free(current);
current
38151311head
previous
current
38151311head
previous
27
Case 3: Deleting at the end
previousnext = NULL;
free(current);
current
38151311head
previous
current
38151311head
previous
29
Interchanging the 2nd and the 3rd nodes
head
38151311
currentprevious
head
38151311
currentprevious
Original List:
1
2
3
Updated List:
30
Interchanging the 2nd and the 3rd nodes
Code:
headnext = current;
currentnext = previous;
previousnext = currentnext;
31
Terms
Suppose the following struct is used for the example in next few slides
struct Node {
int item;
struct Node* next;
};
32
Terms
Declare a new node pointed by node
struct Node* node;
Or NodePtr node;
Remark: Every variable needs to be declared before use.
Using typedef to name a struct Node pointer:
typedef struct Node* NodePtr;
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
node = (struct Node*malloc(sizeof(struct Node));
34
Go to the nth node
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 = tempnext;
printf(“item = %d”, tempitem);
36
Losing Nodes
38 NULL1511
Head
10
Losing Nodes
38 NULL1511
Head
Original Linked List
After executing the below code
Head = (struct Node*)malloc(sizeof(struct Node));
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.