1 cse1301 computer programming: lecture 30 linked lists (part 2)

57
1 CSE1301 Computer Programming: Lecture 30 Linked Lists (Part 2)

Post on 21-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

1

CSE1301 Computer Programming:

Lecture 30Linked Lists (Part 2)

2

Topics

• Sorted linked list

• The “free” list

• Adding to a linked list

• Deleting from a linked list

3

struct ListElementRec

{

int item;

int next;

};

typedef struct ListElementRec ListElement;

ListElement list[maxListLength];

int first;

Recall: Linked List

8 2 9 5 34

0 1 2 3 54

5 34 02 END

first 1

4

Delete an Item: Typical Case

8 2 9 5 34

0 1 2 3 54

first 1

5 34 02 END

Example: Delete item 8

5

8 2 9 5 34

0 1 2 3 54

first 1

5 34 02 END

Delete an Item: Typical Case (cont)

Example: Delete item 8

6

8 2 9 5 34

0 1 2 3 54

first 1

5 34 22 END

Delete an Item: Typical Case (cont)

Example: Delete item 8

7

2 9 5 34

0 1 2 3 54

first 1

5 34 2END

Delete an Item: Typical Case (cont)

Example: Delete item 8

8

Delete the Last Item

2 9 5 34

0 1 2 3 54

first 1

5 34 2END

Example: Delete item 9

9

2 5 34

0 1 2 3 54

first 1

5 34 END

9

END

Delete the Last Item (cont)

Delete last item: Delete item 9Example: Delete item 9

10

2 5 34

0 1 2 3 54

first 1

5 34 END

Delete the Last Item (cont)

Example: Delete item 9

11

2 5 34

0 1 2 3 54

first 1

5 34 END

Delete the First Item

Example: Delete item 2

12

2 5 34

0 1 2 3 54

first 5

5 34 END

Delete the First Item (cont)

Example: Delete item 2

13

5 34

0 1 2 3 54

first 5

34 END

Delete the First Item (cont)

Example: Delete item 2

14

Filling In the Gaps

• Problem: How do we find a free slot if we want to insert a new item into the list?

15

Filling In the Gaps: Solution 1

• Mark free slots with special value, and perform linear search

2 34

0 1 2 3 54

first 1

5 3END FREEFREEFREE

const int END = -1;const int FREE = -2;

16

Filling In the Gaps: Solution 1 (cont)

• Mark free slots with special value, and perform linear search

2 34

0 1 2 3 54

first 1

5 3END FREEFREEFREE

Example: Insert 5 into the linked list

17

Filling In the Gaps: Solution 1 (cont)

• Mark free slots with special value, and perform linear search

Example: Insert 5 into the linked list

2 34

0 1 2 3 54

first 1

5 3END FREEFREEFREE

5

18

Filling In the Gaps: Solution 1 (cont)

• Mark free slots with special value, and perform linear search

Example: Insert 5 into the linked list

2 34

0 1 2 3 54

first 1

5 30 FREEFREEFREE

5

19

Filling In the Gaps: Solution 1 (cont)

• Mark free slots with special value, and perform linear search

Example: Insert 5 into the linked list

2 34

0 1 2 3 54

first 1

5 30 FREEFREEEND

5

20

Filling In the Gaps: Solution 1 (cont)

• Mark free slots with special value, and perform linear search

Example: Insert 5 into the linked list

2 34

0 1 2 3 54

first 1

5 30 FREEFREEEND

5

Problem:Linear search for free slots

21

Filling In the Gaps: Solution 2

• Close the gaps after a deletion, and always append at the end

Example: Delete 4 from the linked list

2 9 5 34

0 1 2 43

4 23 1END

5

first 0 count 5

22

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9 5 34

0 1 2 43

4 23 1END

5

first 0

Example: Delete 4 from the linked list

5count

23

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9 5 34

0 1 2 43

4 33 1END

5

first 0

Example: Delete 4 from the linked list

5count

24

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9 5 3

0 1 2 43

4 31END

5

first 0

Example: Delete 4 from the linked list

5count

25

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9 3

0 1 2 43

first 0

4 251END

5

5

Example: Delete 4 from the linked list

count

26

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9 3

0 1 2 43

first 0

4 2

51END

5

5

Example: Delete 4 from the linked list

count

27

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9

0 1 2 43

first 0

332

51END

5

Example: Delete 4 from the linked list

4count

28

Filling In the Gaps: Solution 2 (cont)

• Close the gaps after a deletion, and always append at the end

2 9

0 1 2 43

first 0

332

51END

5

4

Example: Delete 4 from the linked list

count

Problem:Moving items andadjusting links

29

Filling In the Gaps: Solution 3

• Link the free slots together to form a “free linked list”

2 34

0 1 2 3 54

first 1

5 3END END04

free2

30

Filling In the Gaps: Solution 3

• Link the free slots together to form a “free linked list”

2 34

0 1 2 3 54

first 1

5 3END END04

free2 To linked list

of items

31

Filling In the Gaps: Solution 3 (cont)

• Link the free slots together to form a “free linked list”

2 34

0 1 2 3 54

first 1

5 3END END04

free2 To linked list

of free elements

32

Initialization• Begin with empty item list and a full free list

0 1 2 3 54

first END

2 54 END31

free0

33

Adding an Item -- Example 1

Add item with value = 5 (first item)Step 1: Ensure free != END

0 1 2 3 54

first END

2 54 END31

free0

34

Adding an Item -- Example 1 (cont)

0 1 2 3 54

first END

2 54 END31

free0

5

Step 2: Put value = 5 in the first free slot list[free].item = value;

35

Adding an Item -- Example 1 (cont)

0 1 2 3 54

first 0

2 54 END31

free0

5

Step 3: Remember position of new item first=free;

36

Adding an Item -- Example 1 (cont)

0 1 2 3 54

first 0

2 54 END31

free1

5

Step 4: Update the free linked list to next free slot free = list[free].next;

37

Adding an Item -- Example 1 (cont)

0 1 2 3 54

first 0

2 54 END3END

free1

5

Step 5: Put END marker in new element list[first].next = END;

38

Adding an Item -- Example 2

0 1 2 3 54

first 0

2 54 END3END

free1

5

Add item with value = 9Step 1: Ensure free != END

39

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

2 54 END3END

free1

5

Step 2: Put value = 9 in the first free slot list[free].item = value;

9

40

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

2 54 END3END

free1

5

Step 3: Remember the position of the new item curr = free;

9

1 curr

41

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

2 54 END3END

free2

5

Step 4: Update the free linked list to next free slot free = list[free].next;

9

1 curr

42

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

2 54 END3END

free2

5

Step 5: Find the position of the preceding itemprev = findPrevious(first,value,list);

9

1 currprev 0

43

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

END 54 END3END

free2

5

Step 6: Link current item to the next item in the listlist[curr].next = list[prev].next;

9

1 currprev 0

44

Adding an Item -- Example 2 (cont)

0 1 2 3 54

first 0

END 54 END31

free2

5

Step 7: Insert current item after the previous item list[prev].next = curr;

9

1 currprev 0

45

Adding an Item -- Codeconst int END = -1;int addItem (int first, int free, int value, ListElement *list){

int curr, prev;if (free != END){ list[free].item = value; curr = free; free = list[free].next; prev = findPrevious(first, value, list); /* assumption: the list is never empty */ list[curr].next = list[prev].next; list[prev].next = curr; return free;}else{ printf(“Not enough space\n”); exit(1);}

}

46

Exercise 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

0 1 2 3 54

first 0

54 END3

51

9

free 2

END

47

Deleting an Item -- Example• Delete item with value = 3

2 34

0 1 2 3 54

first 1

5 3END END04

free 2

48

Deleting an Item -- Example (cont)• Step 1: Find position of the item to be deleted curr = findIndex(first,value,list);

2 34

0 1 2 3 54

first 1

5 3END END04

free 2

curr5

49

Deleting an Item -- Example (cont)• Step 2: Ensure the item is in the list curr != END

2 34

0 1 2 3 54

first 1

5 3END END04

free 2

curr5

50

Deleting an Item -- Example (cont)• Step 3: Find the position of the previous item prev = findPrevious(first,value,list);

2 34

0 1 2 3 54

first 1

5 3END END04

free 2

curr5

prev 1

51

Deleting an Item -- Example (cont)• Step 4: Bypass the current item in the list list[prev].next = list[curr].next;

2 34

0 1 2 3 54

first 1

3 3END END04

free 2

curr5

prev 1

52

Deleting an Item -- Example (cont)• Step 5: Link current position to first free slot list[curr].next = free;

2 34

0 1 2 3 54

first 1

3 2END END04

free 2

curr5

prev 1

53

Deleting an Item -- Example (cont)• Step 6: Make current position the first free slot free = curr;

2 34

0 1 2 3 54

first 1

3 2END END04

free 5

curr5

prev 1

54

Deleting an Item -- Codeconst 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 */ list[prev].next = list[curr].next; list[curr].next = free; free = curr; return free;}

55

Exercise 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

56

Main 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)

57

Reading

• 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