Chapter 2: Linked Lists: - Linear List Concepts - Linked Lists - Complex Linked Lists
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
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.
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 data link
ead
empty
linked list
11
Nodes
12
Nodes
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 list 0
r coun head
t
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 list.head = null
2 list.count = 0
3 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 75
coun head
t
list
p
New
pNew -> link = list. head
list.head = pNew
Afte 1 75
r coun head
t
list
p 18
New
Insert at the Beginning
Before 1 75
coun head
t
list
39
p
pNew -> link =New
list.head
list.head = pNew
Afte 2 75
r coun head
t
list
39
p 19
New
Insert in Middle
Before 2 39 75
coun head
t
list
52
pPre p
New
pNew -> link = pPre -> link
pPre -> link = pNew
Afte 3 39 75
r coun head
t
list
52
pPre p 20
New
Insert at End
Before 3 39 52 75
coun head
t
list
134
pPre p
New
pNew -> link = pPre -> link
pPre -> link = pNew
Afte 4 39 52 75
r coun head
t
list
134
pPre p 21
New
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 allocate(pNew)
2 if (memory overflow)
1 return false
3 pNew -> data = dataIn
4 if (pPre = null)
Adding before first node or to empty list
1 pNew -> link = list.head
2 list.head = pNew
5 else
Adding in middle or at end
1 pNew -> link = pPre -> link
2 pPre -> link = pNew
6 list.count = list.count + 1
7 return true
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 39 52 75
coun head
t
list
pPre pLoc
list.head = pLoc -> link
recycle(pLoc)
Afte 2 recycled 52 75
r coun head
t
list
pPre pLoc 25
General Delete Case
Before 3 39 52 75
coun head
t
list
pPre pLoc
pPre -> link = pLoc -> link
recycle (pLoc)
Afte 2 39 recycled 75
r coun head
t
list
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 dataOut = pLoc -> data
2 if (pPre = null)
Delete first node
1 list.head = pLoc -> link
3 else
Delete other nodes
1 pPre -> link = pLoc -> link
4 list.count = list.count - 1
5 recycle (pLoc)
6 return true
End deleteNode
28
Search List
• Sequential search has to be used.
• List is ordered according to a key
field.
29
Successful Searches
Located first Located Located last
middle
target target target
5 10 15 20 95 100
30
Unsuccessful Searches
Less than first Greater than
last
target >
target < 15 target >
5 target < 100
5 10 15 20 20 95 100
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
- or - null if target < key of first node 32
Search List Algorithm
1 pPre = null
2 pLoc = list.head
3 loop (pLoc not null AND target > pLoc -> data.key)
1 pPre = pLoc
2 pLoc = pLoc -> link
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 found = searchList (pList, pPre, pLoc, key)
2 if (found)
1 dataOut = pLoc -> data
3 return found
End retrieveNode
36
Traverse List
list
N
coun po head
t s
5 10 15 20 ... 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
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
Return false if end of list, true otherwise 40
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
2 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
3 return success
End getNext 41
Traverse List
• User controls the loop:
calling traverse algorithm to get the next element
in the list
42
Destroy List Algorithm
Algorithm destroyList (val list <metadata>)
Deletes all data in list.
Pre list is metadata structure to a valid list
Post 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
2 list.pos = null
3 return
End destroyList
44
Sparse Matrices
student
1 s 8,00
0
1 A C
A B B
A
course
C A
B B C
s
A
300
45
Sparse Matrices
student
1 s 8,00
0
1 A C
A B B
A
course
A A
B B C
s
300 C
46
Sparse Matrices
student
1 s 8,00
0
1 A C
A B B
student A
no.
course
C course no A
B gradeB C
row link
s
colum link
300 A
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 5 10 ... 95
coun po r link
t s ear
50
Doubly-Linked Lists
list
51
Multi-Linked Lists
list
52
Reading
• Operations on complex linked lists
• Self-organizing lists
• Case study: a library
53