Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan...

31
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes

Transcript of Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan...

Page 1: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Computer Science and Software EngineeringUniversity of Wisconsin - Platteville

5. LinkedList

Yan ShiCS/SE 2630 Lecture Notes

Page 2: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Linked List

Unsorted List Sorted List Double linked list Circular linked list

Page 3: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Sorted and Unsorted Lists

UNSORTED LIST

Elements are placed into the list in no particular order.

SORTED LIST

List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) .

Page 4: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

ADT Unsorted List Operations

Transformers – MakeEmpty – PutItem – DeleteItem

Observers – IsFull– GetLength– GetItem

Iterators – ResetList – GetNextItem

change state

observe state

process all

Page 5: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

class UnsortedType<char>

MakeEmpty

~UnsortedType

DeleteItem . . .

PutItem

UnsortedType

GetItem

GetNextItem

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 6: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Inserting ‘B’ into an Unsorted List

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos ?

Page 7: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

location = new NodeType;

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘X’ ‘C’ ‘L’

Page 8: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

location->info = item ;

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

‘X’ ‘C’ ‘L’

Page 9: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

location->next = listData ;

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

‘X’ ‘C’ ‘L’

Page 10: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

listData = location ;

Private data:

length 3

listData

currentPos ?

item

location

‘B’

‘B’

‘X’ ‘C’ ‘L’

Page 11: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

length++ ;

Private data:

length 4

listData

currentPos ?

item

location

‘B’

‘B’ ‘X’ ‘C’ ‘L’

Page 12: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Sorted Type Class Interface Diagram

SortedType class

IsFull

GetLength

ResetList

DeleteItem

InsertItem

MakeEmpty

GetItem

Private data:

length

info [ 0 ] [ 1 ] [ 2 ]

[MAX_ITEMS-1]

currentPos

GetNextItem

Page 13: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

InsertItem algorithm for Sorted Array Based List

• Find proper location for the new element in the sorted list.

• Create space for the new element by moving down all the list elements that will follow it.

• Put the new element in the list.

• Increment length.

Page 14: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

DeleteItem algorithm for Sorted Array Based List

• Find the location of the element to be deleted from the sorted list.

• Eliminate space occupied by the item by moving up all the list elements that follow it.

• Decrement length.

Page 15: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

class SortedType<char>

MakeEmpty

~SortedType

DeleteItem . . .

InsertItem

SortedType

RetrieveItem

GetNextItem

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos ?

Page 16: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

InsertItem algorithm for Sorted Linked List

• Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location.

• Obtain a node for insertion and place item in it.

• Insert the node by adjusting pointers.

• Increment length.

Page 17: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

The Inchworm Effect

Page 18: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Inserting ‘S’ into a Sorted List

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos ?

predLoc location

moreToSearch

Page 19: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Finding proper position for ‘S’

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos ?

predLoc location

NULL

moreToSearch true

Page 20: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Finding proper position for ‘S’

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos ?

predLoc location

moreToSearch true

Page 21: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Finding Proper Position for ‘S’

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos ?

predLoc location

moreToSearch false

Page 22: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Inserting ‘S’ into Proper Position

‘C’ ‘L’ ‘X’

Private data:

length 4

listData

currentPos

predLoc location

moreToSearch false

‘S’

Page 23: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

What is a Circular Linked List?

• A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

Page 24: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

External Pointer to the Last Node

Page 25: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Why Circular Linked list?

It doesn’t make any operation much shorter or simpler…

It is useful for applications that require access to both ends of the list.

Page 26: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

What is a Doubly Linked List?

• A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

Page 27: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Linking the New Node into the List

Page 28: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

Deleting from a Doubly Linked List

Page 29: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

What are Header and Trailer Nodes?

• A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key.

• A Trailer Node is a node at the end of a list that contains a key larger than any possible key.

• Both header and trailer are placeholding nodes used to simplify list processing: we never have to handle special cases when inserting/deleting the first/end node.

Page 30: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

A Sorted list Stored in an Array of Nodes

Page 31: Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.

An Array with Linked List of Values and Free Space