Title: CSE1301 Computer Programming: Lecture 30 Linked Lists Part 2
1CSE1301 Computer Programming Lecture 30Linked
Lists (Part 2)
2Topics
- Sorted linked list
- The free list
- Adding to a linked list
- Deleting from a linked list
3Recall Linked List
- struct ListElementRec
-
- int item
- int next
-
- typedef struct ListElementRec ListElement
- ListElement listmaxListLength
- int first
5
3
4
0
2
END
4Delete an Item Typical Case
Example Delete item 8
first
1
0
1
2
3
5
4
4
8
2
9
5
3
5
3
4
0
2
END
5Delete an Item Typical Case (cont)
Example Delete item 8
first
1
0
1
2
3
5
4
4
8
2
9
5
3
5
3
4
0
2
END
6Delete an Item Typical Case (cont)
Example Delete item 8
first
1
0
1
2
3
5
4
4
8
2
9
5
3
2
5
3
4
2
END
7Delete an Item Typical Case (cont)
Example Delete item 8
first
1
0
1
2
3
5
4
4
2
9
5
3
5
3
4
2
END
8Delete the Last Item
Example Delete item 9
first
1
0
1
2
3
5
4
4
2
9
5
3
5
3
4
2
END
9Delete the Last Item (cont)
Delete last item Delete item 9
Example Delete item 9
first
1
0
1
2
3
5
4
4
2
5
3
9
5
3
4
END
END
10Delete the Last Item (cont)
Example Delete item 9
first
1
0
1
2
3
5
4
4
2
5
3
5
3
4
END
11Delete the First Item
Example Delete item 2
first
1
0
1
2
3
5
4
4
2
5
3
5
3
4
END
12Delete the First Item (cont)
Example Delete item 2
first
5
0
1
2
3
5
4
4
2
5
3
5
3
4
END
13Delete the First Item (cont)
Example Delete item 2
first
5
0
1
2
3
5
4
4
5
3
3
4
END
14Filling In the Gaps
- Problem How do we find a free slot if we want
to insert a new item into the list?
15Filling In the Gaps Solution 1
- Mark free slots with special value, and perform
linear search
const int END -1 const int FREE -2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
FREE
FREE
FREE
16Filling In the Gaps Solution 1 (cont)
- Mark free slots with special value, and perform
linear search
Example Insert 5 into the linked list
first
1
0
1
2
3
5
4
4
2
3
5
3
END
FREE
FREE
FREE
17Filling In the Gaps Solution 1 (cont)
- Mark free slots with special value, and perform
linear search
Example Insert 5 into the linked list
first
1
0
1
2
3
5
4
4
5
2
3
5
3
END
FREE
FREE
FREE
18Filling In the Gaps Solution 1 (cont)
- Mark free slots with special value, and perform
linear search
Example Insert 5 into the linked list
first
1
5
4
0
1
2
3
4
5
2
3
0
5
3
FREE
FREE
FREE
19Filling In the Gaps Solution 1 (cont)
- Mark free slots with special value, and perform
linear search
Example Insert 5 into the linked list
first
1
0
1
2
3
5
4
4
5
2
3
5
3
0
END
FREE
FREE
20Filling In the Gaps Solution 1 (cont)
- Mark free slots with special value, and perform
linear search
Example Insert 5 into the linked list
Problem Linear search for free slots
first
1
0
1
2
3
5
4
4
5
2
3
5
3
0
END
FREE
FREE
21Filling In the Gaps Solution 2
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
4
3
5
0
1
2
4
2
9
5
3
4
2
3
1
END
22Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
first
0
5
0
1
2
4
3
4
2
9
5
3
4
2
3
1
END
23Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
first
0
5
0
1
2
4
3
4
2
9
5
3
3
4
3
1
END
24Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
first
0
5
0
1
2
4
3
2
9
5
3
4
3
1
END
25Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
count
first
5
0
4
3
5
0
1
2
2
9
3
2
4
END
26Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
count
first
5
0
4
3
5
0
1
2
2
9
3
4
2
END
27Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
first
0
4
3
5
0
1
2
2
9
3
END
28Filling In the Gaps Solution 2 (cont)
- Close the gaps after a deletion, and always
append at the end
Example Delete 4 from the linked list
Problem Moving items and adjusting links
count
first
4
0
4
3
5
0
1
2
3
2
9
3
2
END
29Filling In the Gaps Solution 3
- Link the free slots together to form a free
linked list
free
2
first
1
0
1
2
3
4
5
4
2
3
5
3
END
END
0
4
30Filling In the Gaps Solution 3
- Link the free slots together to form a free
linked list
To linked list of items
free
2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
END
0
4
31Filling In the Gaps Solution 3 (cont)
- Link the free slots together to form a free
linked list
free
To linked list of free elements
2
first
1
5
0
1
2
3
4
4
2
3
5
3
END
END
0
4
32Initialization
- Begin with empty item list and a full free list
free
0
first
END
5
4
0
1
2
3
2
5
4
END
3
1
33Adding an Item -- Example 1
- Add item with value 5 (first item)
- Step 1 Ensure free ! END
free
0
first
END
5
4
0
1
2
3
2
5
4
END
3
1
34Adding an Item -- Example 1 (cont)
- Step 2 Put value 5 in the first free slot
- listfree.item value
free
0
first
END
5
4
0
1
2
3
5
2
5
4
END
3
1
35Adding an Item -- Example 1 (cont)
- Step 3 Remember position of new item
- firstfree
free
0
first
0
5
4
0
1
2
3
5
2
5
4
END
3
1
36Adding an Item -- Example 1 (cont)
- Step 4 Update the free linked list to next free
slot - free listfree.next
first
0
5
4
0
1
2
3
5
2
5
4
END
3
1
37Adding an Item -- Example 1 (cont)
- Step 5 Put END marker in new element
- listfirst.next END
first
0
5
4
0
1
2
3
5
2
5
4
END
3
END
38Adding an Item -- Example 2
Add item with value 9 Step 1 Ensure free !
END
first
0
5
4
0
1
2
3
5
2
5
4
END
3
END
39Adding an Item -- Example 2 (cont)
Step 2 Put value 9 in the first free slot
listfree.item value
first
0
5
4
0
1
2
3
5
9
2
5
4
END
3
END
40Adding an Item -- Example 2 (cont)
Step 3 Remember the position of the new item
curr free
first
0
5
4
0
1
2
3
5
9
2
5
4
END
3
END
1
curr
41Adding an Item -- Example 2 (cont)
Step 4 Update the free linked list to next free
slot free listfree.next
first
0
5
4
0
1
2
3
5
9
2
5
4
END
3
END
1
curr
42Adding an Item -- Example 2 (cont)
Step 5 Find the position of the preceding
item prev findPrevious(first,value,list)
first
0
5
4
0
1
2
3
5
9
2
5
4
END
3
END
prev
1
0
curr
43Adding an Item -- Example 2 (cont)
Step 6 Link current item to the next item in the
list listcurr.next listprev.next
first
0
5
4
0
1
2
3
5
9
5
4
END
3
END
prev
1
0
curr
44Adding an Item -- Example 2 (cont)
Step 7 Insert current item after the previous
item listprev.next curr
first
0
5
4
0
1
2
3
5
9
END
5
4
END
3
1
prev
1
0
curr
45Adding an Item -- Code
const int END -1 int addItem (int first, int
free, int value, ListElement
list) int curr, prev if (free ! END)
listfree.item value curr free free
listfree.next prev findPrevious(first,
value, list) / assumption the list is never
empty / listcurr.next listprev.next
listprev.next curr return free else
printf(Not enough space\n) exit(1)
46Exercise 1
- Given the list below, follow the algorithm for
adding the following items in order - Add 6 In the middle of list
- Add 3 Becomes new first item in list
- Add an item to an empty list
free
2
first
0
5
4
0
1
2
3
5
9
5
4
END
3
1
END
47Deleting an Item -- Example
free
2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
END
0
4
48Deleting an Item -- Example (cont)
- Step 1 Find position of the item to be deleted
- curr findIndex(first,value,list)
free
2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
END
0
4
curr
5
49Deleting an Item -- Example (cont)
- Step 2 Ensure the item is in the list
- curr ! END
free
2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
END
0
4
curr
5
50Deleting an Item -- Example (cont)
- Step 3 Find the position of the previous item
- prev findPrevious(first,value,list)
free
2
first
1
5
4
0
1
2
3
4
2
3
5
3
END
END
0
4
curr
prev
1
5
51Deleting an Item -- Example (cont)
- Step 4 Bypass the current item in the list
- listprev.next listcurr.next
free
2
first
1
5
4
0
1
2
3
4
2
3
3
3
END
END
0
4
curr
prev
1
5
52Deleting an Item -- Example (cont)
- Step 5 Link current position to first free slot
- listcurr.next free
free
2
first
1
5
4
0
1
2
3
4
2
3
2
3
END
END
0
4
curr
prev
1
5
53Deleting an Item -- Example (cont)
- Step 6 Make current position the first free slot
- free curr
first
1
5
4
0
1
2
3
4
2
3
2
3
END
END
0
4
curr
prev
1
5
54Deleting an Item -- Code
const int END -1 int deleteItem (int first,
int free, int value, ListElement
list) int curr, prev curr findIndex(first,
value, list) / assumption the list is never
empty / if (curr END) printf("d is
NOT in the list\n", value) return free
prev findPrevious(first, value, list) /
assumption the last item is never deleted /
listprev.next listcurr.next
listcurr.next free free curr return
free
55Exercise 2
- Consider the following cases when deleting
- the first item in the linked list
- the last item in the linked list
- an item that is not in the linked list
- Modify the deleteItem function to allow
- for the deletion of the first item
- for the list to become empty
- Write the algorithm and the C code for
- findIndex() finding the index of the item with a
given value - findPrev() finding the index of the item
previous to a given value
56Main points
- Linked list
- has an item list and a free list
- makes it easier to add and delete elements
- searching for an element is slow (linear)
57Reading
- Knuth, Fundamental Algorithms, Section 2.2.3
- pages 251-258 (1st edition)
- pages 254-261 (3rd edition)
- Horowitz and Sahni, Fundamentals of Data
Structures, Chapter 4, pages 106-112