You are on page 1of 53

Chapter 2: Linked Lists

• Linear list concepts • Linked lists • Complex linked lists

1

Linear List Concepts
• A linear list is a data structure each element of which has a unique successor:
element 1 element 2 element 3

• Array is the simplest linear list.

2

Linear List Concepts
• General list: no restrictions on where data can be inserted/deleted, and on which operations can be used on the list. • Restricted list: data can be inserted/deleted and operations are performed only at the ends of the list.
3

Linear List Concepts
• General list:
– Random list: there is no ordering on data – Ordered list: data are arranged according to a key

• Restricted list:
– FIFO (First-In-First-Out): queue – LIFO (Last-In-First-Out): stack

4

Linear List Concepts
• Basic operations:
– – – – Insertion Deletion Retrieval Traversal

5

Insertion
• Random list: insertion can be made at the beginning, the middle or the end of the list. • Ordered list: the data must be inserted so that the ordering of the list is maintained. • Array requires physical shifting.
6

Insertion
25

10

20

30

10

20

25

30

7

Deletion
• Deletion from a general list requires searching the list in order to locate the data being deleted. • Array requires physical shifting after the data is deleted.

8

Retrieval and Traversal
• Retrieval also requires list searching, but does not change the contents of the list. • Traversal is retrieval of all elements in sequence.

9

Linked Lists
• A linked list is an ordered collection of data in which each element contains the location of the next element: Element = Data + Link

10

Linked Lists
h ead data link

empty linked list
11

Nodes
A node with one data field number A node with three data fields name id number

A node with one structured data field name id number

12

Nodes
Head structure coun t list count <integer> pos <pointer> end list Data node structure head node data <dataType> link <pointer> end node data link

dataType key <keyType> field1 <…> field2 <…> … fieldN <…> end node

13

Linked List Algorithms
• Create list • Insert node • Delete node • Search list • Retrieve node • Destroy list

14

Create List
Before list
?

?

coun head t list.head = null list.count = 0

Afte r

list

0 coun t head

15

Create List
Algorithm createList (ref list <metadata>) Initializes metadata for a linked list Pre list is a metadata structure passed by reference Post metadata initialized
1 2 3 list.head = null list.count = 0 return

End createList Ham
16

Insert Node
• Allocate memory for the new node and set up data. • Point the new node to its successor. • Point the new node's predecessor to it.

17

Insert into Empty List
Before
0 coun t head list 75

p New pNew -> link = list. head list.head = pNew

Afte r

1 coun t head list

75

p New

18

Insert at the Beginning
Before
1 coun t head list 39 75

p New pNew -> link = list.head list.head = pNew

Afte r

2 coun t head list p New 39

75

19

Insert in Middle
Before
2 coun t head list pPre 52 39 75

p New pNew -> link = pPre -> link pPre -> link = pNew

Afte r

3 coun t head list pPre

39

75

52 p New
20

Insert at End
Before
3 coun t head list p pPre New pNew -> link = pPre -> link pPre -> link = pNew 134 39 52 75

Afte r

4 coun t head list pPre

39

52

75

134 p New
21

Insert Node Algorithm
Algorithm insertNode (ref list <metadata>, val pPre <node pointer>, val dataIn <dataType>) Inserts data into a new node in the linked list Pre list is metadata structure to a valid list pPre is pointer data’s logical predecessor dataIn contains data to be inserted Post data have been inserted in sequence Return true if successful, false if memory overflow
22

Insert Node Algorithm
1 2 3 4 allocate(pNew) if (memory overflow) 1 return false pNew -> data = dataIn if (pPre = null) Adding before first node or to empty list 1 pNew -> link = list.head 2 list.head = pNew else Adding in middle or at end 1 pNew -> link = pPre -> link 2 pPre -> link = pNew list.count = list.count + 1 return true

5

6 7

End

insertNode
23

Delete Node
• Locate the node to be deleted. • Point the node predecessor's link to its successor. • Release the memory for the deleted node.

24

Delete First Node
Before
3 coun t head list pPre pLoc list.head = pLoc -> link recycle(pLoc) 39 52 75

Afte r

2 coun t head list pPre

recycled

52

75

pLoc

25

General Delete Case
Before
3 coun t head list pPre pLoc pPre -> link = pLoc -> link recycle (pLoc) 39 52 75

Afte r

2 coun t head list

39

recycled

75

pPre

pLoc

26

Delete Node Algorithm
Algorithm deleteNode (ref list <metadata>, val pPre <node pointer>, val pLoc <node pointer> ref dataOut <dataType>) Delete data from a linked list and returns it to calling module Pre list is metadata structure to a valid list pPre is a pointer to predecessor node pLoc is a pointer to node to be deleted dataOut is variable to receive deleted data Post data have been deleted and returned to caller
27

Delete Node Algorithm
1 2 dataOut = pLoc -> data if (pPre = null) Delete first node 1 list.head = pLoc -> link else Delete other nodes 1 pPre -> link = pLoc -> link list.count = list.count - 1 recycle (pLoc) return true

3

4 5 6

End

deleteNode
28

Search List
• Sequential search has to be used. • List is ordered according to a key field.

29

Successful Searches
Located first
target 5 10

Located middle
target 15 20

Located last
target 95 100

pPre

pLoc

pPre

pLoc

pPre

pLoc

30

Unsuccessful Searches
Less than first
target < 5 5 10 target > 15 target < 20 20 15

Greater than last
target > 100 95 100

pPre

pLoc

pPre

pLoc

pPre

pLoc

31

Search List Algorithm
Algorithm searchList (val list <metadata>,
ref pPre <node pointer>, ref pLoc <node pointer> val target <keyType>) Searches list and passes back address of node containing target and its predecessor Pre list is metadata structure to a valid list pPre is a pointer to predecessor node pLoc is a pointer to current node target is the key being sought Post pLoc points to smallest node with equal or greater key - or - null if target > key of last node pPre points to largest node with smaller key 32 - or - null if target < key of first node

Search List Algorithm
1 2 3 pPre = null pLoc = list.head loop (pLoc not null AND target > pLoc -> data.key) 1 pPre = pLoc 2 pLoc = pLoc -> link Set return value if (pLoc = null) 1 found = false else if (target equal pLoc -> data.key) 1 found = true else 1 found = false return found
33

4 5 1 2 6

End searchList

Retrieve Node
• Using search list to locate the node. • Retrieving data from the node.

34

Retrieve Node Algorithm
Algorithm retrieveNode (val list <metadata>, val key <keyType>, ref dataOut <dataType>) Retrieves data from a linked list Pre list is metadata structure to a valid list key is target of data to be retrieved dataOut is variable to receive retrieved data Post data placed at location specified in dataOut - or - error returned if not found Return true if successful, false if data not found
35

Retrieve Node Algorithm
1 2 3 found = searchList (pList, pPre, pLoc, key) if (found) 1 dataOut = pLoc -> data return found

End retrieveNode

36

Traverse List
list N coun po t s 5 10 15 20 head

...

95

100

37

Traverse List
• Walking pointer:
pWalker = list.head loop (pWalker not null) process (pWalker -> data) pWalker = pWalker -> list

38

Traverse List
• User controls the loop:
calling traverse algorithm to get the next element in the list

• Traverse module controls the loop:
calling a user-supplied algorithm to process data

39

Traverse List Algorithm
Algorithm getNext (ref list <metadata>, val fromWhere <Boolean>, ref dataOut <dataType>) Traverses a linked list. Each call returns the location of an element in the list. Pre list is metadata structure to a valid list fromWhere is 0 to start at the first element dataOut is variable to receive data Post dataOut contains data and true returned - or if end of list, return false 40 Return false if end of list, true otherwise

Traverse List Algorithm
1 if (fromWhere is 0) Start from first 1 if (list.count is 0) 1 success = false 2 else 1 list.pos = list.head 2 dataOut = list.pos -> data 3 success = true else Start from pos 1 if (list.pos -> link = null) End of list 1 success = false 2 else 1 list.pos = list.pos -> link 2 dataOut = list.pos -> data 3 success = true return success
41

2

3

End getNext

Traverse List
• User controls the loop:
calling traverse algorithm to get the next element in the list success = getNext (list, 0, dataOut) loop (success) process (dataOut) success = getNext (list, 1, dataOut)

42

Destroy List Algorithm
Algorithm destroyList (val list <metadata>) Deletes all data in list. Pre Post list is metadata structure to a valid list all data deleted

43

Destroy List Algorithm
1 loop (list.count not 0) 1 dltPtr = list.head 2 list.head = dltPtr -> link 3 list.count = list.count - 1 4 recycle (dltPtr) No data left in list. Reset metadata list.pos = null return

2 3

End destroyList

44

Sparse Matrices
1 1 A B A C B A 300
45

student s
A C B A B C

8,00 0

course s

Sparse Matrices
1 1 A B A A B C B C A A

student s
C B

8,00 0

course s
300

46

Sparse Matrices
1 1 A B student no. C course no gradeB row link colum link A A A C A

student s
C B

8,00 0

course s
300

B

47

Sparse Matrices
• How about 3-D sparse matrices?

48

Complex Linked Lists
• Circularly-linked lists • Doubly-linked lists • Multi-linked lists

49

Circularly-Linked Lists

list

N coun po t s r link ear

5

10

... 95

To process data in round turns
50

Doubly-Linked Lists
list N count pos r head ear 5 10

...

...

95

...

To process data sequences from both directions
51

Multi-Linked Lists
list N count pos link1 link2

...

...

...

To process multiple data sequences each of which has a distinct key type
52

Reading
• Operations on complex linked lists • Self-organizing lists • Case study: a library

53