Professional Documents
Culture Documents
CS 102
Ashok K Turuk
List Traversal
2
Head Data Link
PTR
3
Head Data Link
4
List Traversal
5
List Traversal
Algorithm
1. set PTR = Head
2. Repeat step 3 and 4 while
PTR NULL
3. Print PTR->DATA
4. Set PTR = PTR -> LINK
5. Stop
6
Search for an ITEM
7
Search for an ITEM
Algorithm
1. Set PTR = Head
2. Repeat step 3 while PTR NULL
3. if ITEM == PTR -> DATA, then
Set LOC = PTR, and Exit
else
Set PTR = PTR -> LINK
4. Set LOC = NULL /*search
unsuccessful */
5. Stop
8
Search for an ITEM
Algorithm [Sorted ]
1. Set PTR = Head
2. Repeat step 3 while PTR NULL
3. if ITEM < PTR -> DATA, then
Set PTR = PTR->LINK,
else if ITEM == PTR->DATA, then
Set LOC = PTR, and Exit
else
Set LOC = NULL, and Exit
4. Set LOC = NULL /*search unsuccessful */
5. Stop
9
Insertion to a Linked List
10
Insertion to Beginning
11
Insertion to Beginning
14
Insertion to Beginning
16
Insertion at the Beginning
Algorithm
17
Insertion After a Given Node
18
Insertion at a Given Node
LOC
A B
19
Insertion at a Given Node
LOC
A B
PTR->Link = LOC->Link 20
Insertion at a Given Node
LOC
A B
LOC->Link = PTR 21
Insertion After a Given Node
Algorithm
1. If PTR == NULL , then Write
Overflow and Exit
2. Set PTR -> DATA = ITEM
3. If LOC == NULL
Set PTR -> LINK = Head
Set Head = PTR
Else Set PTR->Link = LOC->Link
Set LOC->Link = PTR
4. Exit
22
Insertion into a Sorted Linked List
25
Deletion Algorithm
26
Deletion Algorithm
27
Delete the Node Following a Given
Node
28
Delete the Node Following a Given
Node
• Algorithm: Delete(Head, LOC, LOCP)
2. Exit
29
Delete an Item
30
Delete an Item
To delete a Node [Node B] We have to
Remember the Pointer to its predecessor
[Node A]
4000
31
Deletion of an ITEM
Algorithm
Header Node
35
Header Linked Lists
Header Node
36
Header Linked Lists
• Pointer Head always points to the
header node.
37
Header Linked Lists
• The first node in a header list is the
node following the header node
• Circular Header list are frequently used
instead of ordinary linked list
– Null pointer are not used, hence all pointer
contain valid addresses
– Every node has a predecessor, so the first
node may not require a special case.
38
Traversing a Circular Header List
39
Traversing a Circular Header List
Algorithm
40
Locating an ITEM
41
Locating an ITEM
Algorithm
1. Set PTR = Head->Link
2. Repeat while PTR ->Data ITEM and PTR
Head
Set PTR = PTR ->Link
3. If PTR->Data == ITEM then
Set LOC = PTR
else
Set LOC = NULL
4. Exit
42
Other variation of Linked List
• A linked list whose last node points back
to the first node instead of containing a
NULL pointer called a circular list
43
Other variation of Linked List
• A linked list which contains both a
special header node at the beginning of
the list and a special trailer node at the
end of list
44
Applications of Linked Lists
Ex: 10 X 6 +20 X 3 + 55
0 0 11
22 0 0
0 0 66
Polynomials
A ( x ) a m 1 x e m 1
am2 x em 2
... a 0 x e0
Representation of Node
Example
a 3x 14
2x 1 8
a
3 14 2 8 1 0 null
b 8x 14
3x 10
10 x 6
b
8 14 -3 10 10 6 null
Polynomial Operation
1. Addition
2. Subtraction
3. Multiplication
4. Scalar Division
Polynomial Addition
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 a->expon < b->expon
-3 10
d
Polynomial Addition
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
d
a->expon > b->expon
Polynomial Addition (cont’d)
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8 10 6 1 0
d
a->expon > b->expon
Polynomial Subtraction
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14 a->expon < b->expon
3 10
d
Polynomial Subtraction (cont’d)
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14 3 10 2 8
d
a->expon > b->expon
Polynomial Addition (cont’d)
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14 3 10 2 8 -10 6 1 0
d
a->expon > b->expon
Sparse Matrix
A sparse matrix is a very large
matrix that contains lot of zero’s.
In order to save storage space, a
sparse matrix stores only the
nonzero elements.
Linked list representation
Sparse Matrix
0 0 5 0
2 7 0 9
A
0 0 0 0
0 3 8 0
4 4 -- 0 2 5 3 2 8 Null
Linked list representation
Sparse Matrix
ROWLINK
i j
aij
COLLINK
Linked list representation
Sparse Matrix
0 2 5
0 0 5 0 1 0 2 1 7 3 9
2 7 0 9
A
0 0 0 0
2
0 3 8 0
3 1 3 2 8
Two-Way List
• What we have discussed till now is a
one-way list [ Only one way we can
traversed the list]
• Two-way List : Can be traversed in two
direction
– Forward : From beginning of the list to end
– Backward: From end to beginning of the list
59
Two-Way List
• A two-way list is a linear collection of
data element called nodes where each
node N is divided into three parts:
– A information field INFO which contains
the data of N
– A pointer field FORW which contains the
location of the next node in the list
– A pointer field BACK which contains the
location of the preceding node in the list
60
Two-Way List
• List requires two pointer variables:
– FIRST: which points to the first node in
the list
– LAST: which points to the last node in the
list
INFO field
BACK pointer
FORW pointer
61
Two-Way List
FIRST LAST
62
Two-Way List
Suppose LOCA and LOCB are the locations
of nodes A and B respectively in a two-way
list.
FIRST LAST
A B
Forward Pointer
Node A Backward Pointer
Node A
64
Two-Way List
FIRST LAST
A B
LOCA LOCB
LOCA->FORW = LOCB if and only if
LOCB->BACK = LOCA 65
Operation in two-way list
• Traversing
• Searching
• Deleting
• Inserting
66
Deletion in Two-Way List
DELETE NODE B
FIRST LAST
A B C
LOCB
LOCB->BACK->FORW = LOCB->FORW
67
Deletion in Two-Way List
DELETE NODE B
FIRST LAST
A B C
LOCB
LOCB->FORW->BACK = LOCB->BACK
68
Insertion in Two-Way List
INSERT NODE NEW
LOCA->FORW->BACK = NEW
FIRST NEW->FORW = LOCA->FORW
LAST
A B C
LOCA
LOCA->FORW = NEW
NEW NEW->BACK = LOCA
69
?
70