1.00 Tutorial 10 Content Linked List • 1D Linked List Recap

advertisement
1.00 Tutorial 10
Linked List
Content
• 1D Linked List Recap
• 2D Linked List Introduction/Pset 9
1
Linked-list: Recap
Conceptual Diagram
Linked-List (basically a list):
- contains a collection of element (object)
- arbitrary length
- cf. ArrayList in Java
Basic operations
-
AddFirst/AddLast
-
RemoveFirst/RemoveLast
-
Traverse/Print
Linked-list: Tips
• Always think of special cases
– What if your link is empty?
– What if there is only one element?
• Always draw a diagram
• Warming up!
– What are special cases for AddFirst
method?/Draw a diagram
– What are special cases for RemoveLast
method?/Draw a diagram
2
Linked-list: Answers to Warm-up Questions
addFirst Method
Normal case
Length = 0
Before
List
first
...
Node 1
item
List
last
next
first
Node n
item
Item 1
null
next
null
Item n
Length = 0
Normal case
List
After
first
List
last
Node 0
item
last
next
Item 0
first
...
Node 1
item
next
Node n
item
Item 1
null
last
null
Node 1
next
item
Item n
next
Item 1
Linked-list: Answers to Warm-up Questions
removeLast Method
Before
Length = 0
Normal case
List
first
Length = 1
List
last
first
Node 1
item
next
Node 3
Node 2
item
next
item
null
next
Node 1
item
Item 1
List
first
Item 2
After
Item 3
null
next
Item 1
Length = 0
Normal case
Length = 1
last
t
t
Node 1
item
last
next
Node 2
item
next
List
x
first
x
x
Node 3
item
last
null
next
null
Node 1
item
Item 1
Item 2
null
next
Item 3
Item 1
return
3
Linked-list: insertAt(int i)
insertAt Method
Normal case
List
Before
first
last
Node 0
item
...
Node 1
next
item
Item 0
next
Node n
item
Item 1
null
next
Item n
After
Normal case
Linked-list: insertAt(int i)
insertAt Method
Normal case
List
Before
first
i > Length
Length = 0
i > Length
last
Node 0
item
Length = 0
...
Node 1
next
item
Item 0
next
Node n
item
Item 1
null
next
Item n
Normal case
List
After
first
last
Node 0
item
next
Item 0
Node k
item
next
Item k
Node 1
item
next
Item 1
...
Node n
item
next
null
Throw exception
Throw exception
Or
Or
addFirst
addLast
Item n
4
2D Linked List: PSet 9
Container size
40
20
10
Description
- Horizontal: Crane arrival order
Crane
Id Size
0
2
A 40
5
10
Z 40
Q 40
12
W40
3
1
D 40
2
null
3
X 20
6
T 40
3
6
F 10
null
8
U 10
P 40
5
5
-What’re the differences between 2D
linked list and 1D in term of data
structure?
7
R 20
Y 10
Think
null
null
4
4
- Vertical: Weight order by size
null
S 20
6
G 20
null
-How about in term of operations?
7
null
null
null
2D Linked List: Data Structure
2D
N
item next
item down
right
Node
1D
N
You can either create a new class ‘container’ to hold
id, name and size then assign to item variable of
Node class or simply create a ‘container’ as a Node
Size 40
Weight=60
item down
D
item down
...
C
B
item down
Crane 6
Crane 6
...
Size=20
A
right
D
Size=40
Crane 1
Weight=10
item down
Crane 2
Size=40
Size 10
right
Crane 1
Weight=30
B
Size=40
right
C
Weight=20
Size 20
right
A
Crane 2
List
5
2D Linked List: Adding Containers
Size 40
Weight=20
A
Size 10
Crane 6
Crane m
...
...
Crane 2
Crane 2
Size=40
Size 20
Crane 1
Crane 1
List
2D Linked List: Adding Containers
Weight=20
A
Size 10
Crane 6
...
...
Crane m
A
item down
Size 20
Crane 2
Crane 2
Size=40
Crane 1
Size 40
right
Crane 1
List
6
2D Linked List: Adding Containers
Weight=20
A
Weight=30
B
Size=40
A
item down
Size 20
Size 10
Crane 6
Crane m
...
...
Crane 2
Crane 2
Size=40
Crane 1
Size 40
right
Crane 1
List
2D Linked List: Data Structures
Weight=20
Size=40
B
item down
Crane 6
...
...
Crane m
A
item down
Size 10
Crane 2
Crane 2
Weight=30
B
Size=40
Size 20
right
A
Crane 1
Size 40
right
Crane 1
List
7
2D Linked List: Data Structures
Weight=20
Weight=30
B
Crane 2
Size=40
Size=40
Size 10
B
item down
Crane 2
Weight=10
A
item down
Size 20
right
A
Crane 1
Size 40
right
Crane 1
List
C
Crane 6
Crane m
...
...
Size=40
2D Linked List: Data Structures
item down
A
...
C
B
item down
Crane 6
Crane m
...
Size=40
item down
Size 10
right
Weight=10
C
Size=40
Size 20
right
Crane 2
Weight=30
B
Size=40
Crane 1
Weight=20
A
Crane 2
Size 40
right
Crane 1
List
8
2D Linked List: Data Structures
Weight=30
Weight=10
C
item down
A
C
B
item down
Crane 6
Crane m
...
...
Size=40
item down
Size 10
right
Crane 2
Size=40
Size 20
right
B
Size=40
Crane 1
Weight=20
A
Crane 2
Size 40
right
Crane 1
List
2D Linked List: Data Structures
Weight=30
Weight=10
C
Weight=60
D
Size=20
item down
A
...
C
B
item down
Crane 6
Crane m
...
Size=40
item down
Size 10
right
Crane 2
Size=40
Size 20
right
B
Size=40
Crane 1
Weight=20
A
Crane 2
Size 40
right
Crane 1
List
9
2D Linked List: Adding Containers
Size 40
Weight=60
item down
D
item down
...
C
B
item down
Crane 6
Crane m
...
Size=20
A
right
D
Size=40
Crane 1
Weight=10
item down
Crane 2
Size=40
Size 10
right
Crane 1
Weight=30
B
Size=40
right
C
Weight=20
Size 20
right
A
Crane 2
List
2D Linked List: Adding a Container
• We need to add to both crane and size lists
• What are special cases when adding to
crane list?
• What are special cases when adding to size
list?
• Let us write a pseudo code for a method
called “add”, i.e. add(Node c, int crane)
10
Pseudo Code for ‘add’ Method
public void add(Container c, int crane) {
Initializing node for traversing the list
// 1. Container current = head node inside crane list
// 2. Make sure that right node of c points to nothing
//
because we are appending node to the end
Adding container to crane
// 3. if (current == null)
//
head node = c;
//
else
//
use current to traverse to the right till the end
//
and add c to the list
Pseudo Code for ‘add’ Method
// 4. int index = getSizeIndex(c.size);
// 5. int weight = c.weight;
// 6. current = head node inside size list
Adding container to size list
// 7. if (current == null)
//
head node = c;
//
else
//
while (current.right != null) {
//
if (weight <= current.weight) {
//
add c in front of current
//
break;
//
}
//
else
//
move current downward
//
}
//
when reaching the end of the list and still greater
//
add node c to the end of the list
} // end add Method
11
More (To Think) on PSet 9
• Remove method
• Print method
• Recall example from add Method, you need
to pay attention to how to traverse a list
PSet 9 GUI
12
Download