Download presentation
Presentation is loading. Please wait.
1
Sequences 6/18/2018 8:51 PM C201: Linked List
2
Overview of Today’s Topic
Review of linked list Create an ordered linked list Project 2 is out
3
Review of Linked Lists What is a linked list?
How to create a linked list? How to define a node? How to create a head? How to create a node? How to insert a node to the linked list? How to search a linked list? Draw a linked list on the top of the board
4
Node and Linked list struct node { int value; node * next; }; value
head value next value next value next 202 101 303 NULL
5
Data Fields in Node struct student { int ID;
double GPA; student * next; }; ID GPA next
6
Data Fields in Node struct student { int ID;
double GPA; student * next; }; head ID GPA next ID GPA next ID GPA next 1001 3.5 3003 3.3 2002 2.5 NULL
7
Data Fields in Node struct Friend { string name;
image * picture; Friend * next; }; name picture next
8
Data Fields in Node struct Friend { string name;
image * picture; Friend * next; }; head name picture next name picture next name picture next “Dora” “Pooh” “Kitty” NULL
9
How to Create a Linked List
10
How to Create a Linked List
0. Define the node
11
How to Create a Linked List
0. Define the node struct node { int value; node * next; };
12
How to Create a Linked List
1. Create a head struct node { int value; node * next; };
13
How to Create a Linked List
1. Create a head node * head = NULL; struct node { int value; node * next; }; head NULL
14
How to Create a Linked List
2. Create an instance of node struct node { int value; node * next; }; head NULL
15
How to Create a Linked List
2. Create an instance of node node * n= new node; n->value = 303; n->next = NULL; struct node { int value; node * next; }; n value next 303 NULL head NULL
16
How to Create a Linked List
3. Insert the node to (the head of) the list n->next = head; head = n; struct node { int value; node * next; }; n value next 303 NULL head NULL
17
How to Create a Linked List
3. Insert the node to (the head of) the list n->next = head; head = n; struct node { int value; node * next; }; n value next 303 head NULL
18
How to Create a Linked List
3. Insert the node to (the head of) the list n->next = head; head = n; struct node { int value; node * next; }; n value next 303 head NULL
19
How to Create a Linked List
3. Insert the node to (the head of) the list struct node { int value; node * next; }; value next 303 head NULL
20
How to Create a Linked List
3. Insert the node to the list struct node { int value; node * next; }; head value next 303 NULL
21
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; head value next 303 NULL
22
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes node * n= new node; n->value = 101; n->next = NULL; struct node { int value; node * next; }; n value next head 101 NULL value next 303 NULL
23
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 NULL value next 303 NULL
24
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 value next 303 NULL
25
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next head 101 value next 303 NULL
26
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; value next head 101 value next 303 NULL
27
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; head value next value next 101 303 NULL
28
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes node * n= new node; n->value = 101; n->next = NULL; struct node { int value; node * next; }; n value next 101 NULL head value next value next 101 303 NULL
29
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; ; struct node { int value; node * next; }; n value next 101 NULL head value next value next 101 303 NULL
30
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next 101 head value next value next 101 303 NULL
31
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes n->next = head; head = n; struct node { int value; node * next; }; n value next 101 head value next value next 101 303 NULL
32
How to Create a Linked List
4. Repeat Step 2 and Step 3 to create and insert all nodes struct node { int value; node * next; }; value next 101 head value next value next 101 303 NULL
33
How to Create a Linked List
5. A linked list is created struct node { int value; node * next; }; head value next value next value next 101 101 303 NULL
34
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL
35
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
36
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
37
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
38
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
39
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
40
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
41
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
42
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
43
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
44
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
45
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } head value next value next value next 404 101 505 NULL temp
46
Node Access node * temp = head; While (temp != NULL) {
cout << temp->value; // access data fields temp = temp->next; } //finished accessing the node head value next value next value next 404 101 505 NULL temp
47
Linked List As a Function Parameter
Pass-by-reference Passy-by-value
48
Linked List as a Function Parameter
Pass-by-reference of the head of the list Insert a node Remove a node Pass-by-value of the head of the list Search a node Read value from nodes Key: whenever you might need to change the value of the head of the linked list, use pass-by-reference
49
End of Review
50
Today’s New Topic Create an ordered linked list
51
Examples of Ordered Linked List
struct student { int ID; double GPA; student * next; }; head ID GPA next ID GPA next ID GPA next 1001 3.0 2002 4.0 3003 2.0 NULL Sorted linked list with increasing order of field ID head ID GPA next ID GPA next ID GPA next 3003 2.0 1001 3.0 2002 4.0 NULL Sorted linked list with increasing order of field GPA
52
Node Used in Our Program
struct node { int value; node * next; }; value next node
53
Create an Ordered Linked List
Rewrite the insert function Maintain the linked list ordered After a new node is inserted, the resulting list should still be ordered. B value next value next value next 100 200 300 NULL bk value next ? NULL
54
Insert (node * & H, node * n)
Two parameters: H: head of an ordered linked list n: pointer to a new node Result: The new node is inserted in the list The list is still ordered The head of the list is still H
55
Insert (node * & H, node * n)
Condition 1: Empty linked list H value next NULL 250 NULL n
56
Insert (node * & H, node * n)
Condition 1: Empty linked list H value next 250 NULL n
57
Insert (node * & H, node * n)
An ordered list H value next 250 NULL
58
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list H value next 250 NULL value next n 150 NULL
59
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next 250 NULL value next n 150 NULL
60
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next 250 NULL value next n 150
61
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next 250 NULL value next n 150
62
Insert (node * & H, node * n)
Linked list is still ordered H value next 250 NULL value next 150
63
Insert (node * & H, node * n)
Linked list is still ordered H value next value next 150 250 NULL
64
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list H value next value next 150 ? NULL value next n 100 NULL
65
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next value next 150 ? NULL value next n 100 NULL
66
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next value next 150 ? NULL value next n 100
67
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next value next 150 ? NULL value next n 100
68
Insert (node * & H, node * n)
Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list Insert to head H value next value next 150 ? NULL value next 100
69
Insert (node * & H, node * n)
Linked list is still ordered H value next value next 150 250 NULL value next 100
70
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next 100 150 200 NULL
71
Insert (node * & H, node * n)
Condition 3: new node (value) is greater than the first node (value) of linked list H value next value next value next 100 ? ? NULL n value next 200 NULL
72
Insert (node * & H, node * n)
Condition 3: new node (value) is greater than the first node (value) of linked list H value next value next value next 100 ? ? NULL n value next 200 NULL
73
Insert (node * & H, node * n)
Condition 3: new node (value) is greater than the first node (value) of linked list Use temporary variables and while loop Find the location and then insert H value next value next value next 100 ? ? NULL n value next 200 NULL
74
Insert (node * & H, node * n)
Condition 3: new node (value) is greater than the first node (value) of linked list Use temporary variables and while loop Find the location and then insert H value next value next value next 100 ? ? NULL t1 t2 n value next 200 NULL
75
Insert (node * & H, node * n)
Condition 3: Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next 100 ? ? NULL t1 t2 n value next 200 NULL
76
Insert (node * & H, node * n)
Condition 3: Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next 100 150 ? NULL t1 t2 n value next 200 NULL
77
Insert (node * & H, node * n)
Condition 3: Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next 100 150 ? NULL t1 t2 n value next 200 NULL
78
Insert (node * & H, node * n)
Condition 3: Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next 100 150 250 NULL t1 t2 n value next 200 NULL
79
Insert (node * & H, node * n)
Condition 3: After the while loop, insert new node between nodes pointed by t1 and t2 H value next value next value next 100 150 250 NULL t1 t2 n value next 200 NULL
80
Insert (node * & H, node * n)
Condition 3: After the while loop, insert new node between nodes pointed by t1 and t2 H value next value next value next 100 150 250 NULL t1 t2 n value next 200
81
Insert (node * & H, node * n)
Condition 3: After the while loop, insert new node between nodes pointed by t1 and t2 H value next value next value next 100 150 250 NULL t1 t2 n value next 200
82
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next 100 150 250 NULL value next 200
83
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next 100 150 250 NULL value next 200
84
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next 100 150 250 NULL value next 200
85
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next 100 150 250 NULL value next 200
86
Insert (node * & H, node * n)
Linked list is still ordered H value next value next value next value next 100 150 200 250 NULL
87
Insert (node * & H, node * n)
Condition 3 H value next value next value next value next 100 ? ? ? NULL n value next 300 NULL
88
Insert (node * & H, node * n)
Condition 3 H value next value next value next value next 100 ? ? ? NULL n value next 300 NULL
89
Insert (node * & H, node * n)
Condition 3 Use temporary variable and while to find the location to insert the new node H value next value next value next value next 100 ? ? ? NULL t1 t2 n value next 300 NULL
90
Insert (node * & H, node * n)
Condition 3 Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 ? ? NULL t1 t2 n value next 300 NULL
91
Insert (node * & H, node * n)
Condition 3 Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 ? ? NULL t1 t2 n value next 300 NULL
92
Insert (node * & H, node * n)
Condition 3 Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 200 ? NULL t1 t2 n value next 300 NULL
93
Insert (node * & H, node * n)
Condition 3 Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 200 ? NULL t1 t2 n value next 300 NULL
94
Insert (node * & H, node * n)
Condition 3 : Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 200 250 NULL t1 t2 n value next 300 NULL
95
Insert (node * & H, node * n)
Condition 3 : Move t1 and t2 forward until new node value is smaller than or equals to the node value pointed by t2 or t2 points to NULL. H value next value next value next value next 100 150 200 250 NULL t1 t2 n value next 300 NULL
96
Insert (node * & H, node * n)
Condition 3 : After the wile loop, insert new node between the nodes pointed by t1 and t2. H value next value next value next value next 100 150 200 250 NULL t1 t2 n value next 300 NULL
97
Insert (node * & H, node * n)
Condition 3 : After the wile loop, insert new node between the nodes pointed by t1 and t2. H value next value next value next value next 100 150 200 250 NULL t1 t2 n value next 300
98
Insert (node * & H, node * n)
Condition 3 : After the wile loop, insert new node between the nodes pointed by t1 and t2. H value next value next value next value next 100 150 200 250 NULL t1 t2 n value next 300
99
Insert (node * & H, node * n)
Linked list is still ordered. H value next value next value next value next 100 150 200 250 NULL value next 300
100
The Start of Time The start of time is the start of the world
“Big bang theory” speculates that the world (time) stars 14 billions ago
101
The Start of Time January 1, 1970 UTC
Unix operating systems consider the world starts at: January 1, 1970 UTC
102
The Start of Time January 1, 1970 UTC
Unix operating systems consider the world starts at: January 1, 1970 UTC Unix time (Epoch time): Number of seconds has passed since January 1, 1970
103
Where Were You? February 13, :31:30 (EST)
104
Where Were You? February 13, :31:30 (EST) Day
105
1234567890 Day Where Were You at 1234567890 (UNIX Epoch Clock Time)?
February 13, :31:30 (EST)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.