1.07k likes | 1.36k Views
2. Overview of Today's Topic. Review of linked listCreate an ordered linked listProject 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
E N D
1. 1 C201: Linked List
2. 2 Overview of Todays Topic Review of linked list
Create an ordered linked list
Project 2 is out
3. 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 boardDraw a linked list on the top of the board
4. 4 Node and Linked list struct node{ int value; node * next;};
5. Data Fields in Node 5
6. Data Fields in Node 6
7. Data Fields in Node 7
8. Data Fields in Node 8
9. 9 How to Create a Linked List
10. 10 How to Create a Linked List
11. 11 How to Create a Linked List
12. 12 How to Create a Linked List 1. Create a head
13. 13 How to Create a Linked List 1. Create a head
node * head = NULL;
14. 14 How to Create a Linked List 2. Create an instance of node
15. How to Create a Linked List 2. Create an instance of node
node * n= new node;
n->value = 303;
n->next = NULL;
15
16. 16 How to Create a Linked List 3. Insert the node to (the head of) the list
n->next = head;
head = n;
17. 17 How to Create a Linked List 3. Insert the node to (the head of) the list
n->next = head;
head = n;
18. 18 How to Create a Linked List 3. Insert the node to (the head of) the list
n->next = head;
head = n;
19. 19 How to Create a Linked List 3. Insert the node to (the head of) the list
20. 20 How to Create a Linked List 3. Insert the node to the list
21. 21 How to Create a Linked List 4. Repeat Step 2 and Step 3 to create and insert all nodes
22. 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;
23. 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;
24. 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;
25. 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;
26. 26 How to Create a Linked List 4. Repeat Step 2 and Step 3 to create and insert all nodes
27. 27 How to Create a Linked List 4. Repeat Step 2 and Step 3 to create and insert all nodes
28. 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;
29. 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;
;
30. 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;
31. 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;
32. 32 How to Create a Linked List 4. Repeat Step 2 and Step 3 to create and insert all nodes
33. 33 How to Create a Linked List 5. A linked list is created
34. 34 Node Access
35. 35 Node Access
36. 36 Node Access
37. 37 Node Access
38. 38 Node Access
39. 39 Node Access
40. 40 Node Access
41. 41 Node Access
42. 42 Node Access
43. 43 Node Access
44. 44 Node Access
45. 45 Node Access
46. 46 Node Access
47. 47 Linked List As a Function Parameter Pass-by-reference
Passy-by-value
48. 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 49
50. 50 Todays New Topic Create an ordered linked list
51. Examples of Ordered Linked List 51
52. 52 Node Used in Our Program struct node{ int value; node * next;};
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. 53
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 54
55. Insert (node * & H, node * n) Condition 1: Empty linked list 55
56. Insert (node * & H, node * n) Condition 1: Empty linked list 56
57. Insert (node * & H, node * n) 57 An ordered list
58. Insert (node * & H, node * n) Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list 58
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 59
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
60
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 61
62. Insert (node * & H, node * n) Linked list is still ordered 62
63. Insert (node * & H, node * n) Linked list is still ordered 63
64. Insert (node * & H, node * n) Condition 2: new node (value) is smaller than (or equal to) the first node (value) of linked list 64
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
65
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 66
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
67
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 68
69. Insert (node * & H, node * n) Linked list is still ordered 69
70. Insert (node * & H, node * n) Linked list is still ordered 70
71. Insert (node * & H, node * n) Condition 3: new node (value) is greater than the first node (value) of linked list 71
72. Insert (node * & H, node * n) Condition 3: new node (value) is greater than the first node (value) of linked list 72
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 73
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 74
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. 75
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. 76
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. 77
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. 78
79. Insert (node * & H, node * n) Condition 3:
After the while loop, insert new node between nodes pointed by t1 and t2 79
80. Insert (node * & H, node * n) Condition 3:
After the while loop, insert new node between nodes pointed by t1 and t2 80
81. Insert (node * & H, node * n) Condition 3:
After the while loop, insert new node between nodes pointed by t1 and t2 81
82. Insert (node * & H, node * n) Linked list is still ordered 82
83. Insert (node * & H, node * n) Linked list is still ordered 83
84. Insert (node * & H, node * n) Linked list is still ordered 84
85. Insert (node * & H, node * n) Linked list is still ordered 85
86. Insert (node * & H, node * n) Linked list is still ordered 86
87. Insert (node * & H, node * n) Condition 3 87
88. Insert (node * & H, node * n) Condition 3 88
89. Insert (node * & H, node * n) Condition 3
Use temporary variable and while to find the location to insert the new node 89
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. 90
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. 91
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. 92
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. 93
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. 94
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. 95
96. Insert (node * & H, node * n) Condition 3 :
After the wile loop, insert new node between the nodes pointed by t1 and t2. 96
97. Insert (node * & H, node * n) Condition 3 :
After the wile loop, insert new node between the nodes pointed by t1 and t2. 97
98. Insert (node * & H, node * n) Condition 3 :
After the wile loop, insert new node between the nodes pointed by t1 and t2. 98
99. Insert (node * & H, node * n) Linked list is still ordered. 99
100. 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 Unix operating systems consider the world starts at:
101
102. The Start of Time Unix operating systems consider the world starts at:
102
103. Where Were You? February 13, 2009 18:31:30 (EST) 103
104. Where Were You? February 13, 2009 18:31:30 (EST) 104
105. 1234567890 Day Where Were You at 1234567890 (UNIX Epoch Clock Time)?
February 13, 2009 18:31:30 (EST)
http://www.1234567890day.com/ 105