SlideShare a Scribd company logo
Data Structure & Algorithm
CS 102
Ashok K Turuk
List Traversal
Head Data Link
2
=
Head Data Link
PTR
3
=
Head Data Link
PTR = PTR - > Link
4
List Traversal
Let Head be a pointer to a linked
list in memory. Write an algorithm
to print the contents of each node
of the list
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
• Let Head be a pointer to a
linked list in memory. Write an
algorithm that finds the location
LOC of the node where ITEM
first appears in the list, or sets
LOC = NULL if search is
unsuccessful.
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
Head Data Link
10
Insertion to Beginning
Head Data Link
New NodePTR
11
Insertion to Beginning
Head Data Link
New NodePTR
PTR->Link = Head
12
Insertion to Beginning
Head Data Link
New NodePTR
PTR->Link = Head , Head = PTR
13
Overflow and Underflow
• Overflow : A new Data to be inserted
into a data structure but there is no
available space.
• Underflow: A situation where one wants
to delete data from a data structure
that is empty.
14
Insertion to Beginning
Head Data Link
New NodePTR
Overflow , PTR == NULL
15
Insertion at the Beginning
Let Head be a pointer to a linked list in
memory. Write an algorithm to insert
node PTR at the beginning of the
List.
16
Insertion at the Beginning
Algorithm
1. If PTR == NULL , then Write
Overflow and Exit
2. Set PTR -> DATA = ITEM
3. Set PTR -> LINK = Head -> LINK
4. Set Head = PTR
5. Exit
17
Insertion After a Given Node
Let Head be a pointer to a linked list in
memory. Write an algorithm to insert
ITEM so that ITEM follows the node
with location LOC or insert ITEM as
the first node when LOC == NULL
18
Insertion at a Given Node
Head Data Link
New NodePTR
A BA
LOC
19
Insertion at a Given Node
Head Data Link
New NodePTR
BA
LOC
PTR->Link = LOC->Link 20
Insertion at a Given Node
Head Data Link
New NodePTR
BA
LOC
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
Head Data Link
1000 2000 3000 4000 5000
3500
3000 < 3500 < 4000
23
Insertion into a Sorted Linked
List
1000 2000 3000 4000 5000
3000 < 3500 < 4000
BA
To Insert Between Node A and B We have to
Remember the Pointer to Node A which is the
predecessor Node of B
24
Insertion into a Sorted Linked List
Steps to Find the LOC of Insertion
1. If Head == NULL, then Set LOC = NULL and
Return
2. If ITEM < Head ->Data , then Set LOC =
NULL and Return
3. Set SAVE = Head and PTR = Head -> Link
4. Repeat Steps 5 and 6 while PTR  NULL
5. If ITEM < PTR -> Data then
LOC = SAVE and Return
6. Set SAVE = PTR and PTR = PTR->Link
7. Set LOC = SAVE
8. Return
25
Deletion Algorithm
A
26
Deletion Algorithm
A
27
Delete the Node Following a Given
Node
• Write an Algorithm that deletes the
Node N with location LOC. LOCP is the
location of the node which precedes N
or when N is the first node LOCP =
NULL
28
Delete the Node Following a
Given Node
• Algorithm: Delete(Head, LOC, LOCP)
1 If LOCP = NULL then
Set Head = Head ->Link. [Deletes the 1st
Node]
Else
Set LOCP->Link = LOC->Link [Deletes Node
N]
2. Exit
29
Delete an Item
Let Head be a pointer to a linked
list in memory that contains integer
data. Write an algorithm to delete
node which contains ITEM.
30
Delete an Item
1000 2000 3000 4000 5000
4000
BA
To delete a Node [Node B] We have to
Remember the Pointer to its predecessor
[Node A]
31
Deletion of an ITEM
Algorithm
1. Set PTR=Head and TEMP = Head
2. Repeat step 3 while PTR  NULL
3. If PTR->DATA == ITEM, then
Set TEMP->LINK = PTR -> LINK, exit
else
TEMP = PTR
PTR = PTR -> LINK
4. Stop
32
Deletion of an ITEM
Algorithm
1. If Head == NULL , then
Write ITEM is not in the
List, Exit
2. If Head ->Data == Item
Head = Head -> Link
3. Set PTR=Head and TEMP =
Head
33
Deletion of an ITEM
Algorithm
4. Repeat step 4 while PTR  NULL
If PTR->DATA == ITEM, then
Set TEMP->LINK = PTR -> LINK, exit
else
TEMP = PTR
PTR = PTR -> LINK
5. Stop
34
Header Linked Lists
• A header linked list is a linked list
which always contains a special node
called header node
• Grounded Header List: A header list
where the last node contains the NULL
pointer.
Header Node
35
Header Linked Lists
• Circular Header List: A header list
where the last node points back to the
header node.
Header Node
36
Header Linked Lists
• Pointer Head always points to the
header node.
• Head->Link == NULL indicates that a
grounded header list is empty
• Head->Link == Head indicates that a
circular header list is empty
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
• Let Head be a circular header list in
memory. Write an algorithm to print
Data in each node in the list.
39
Traversing a Circular Header List
Algorithm
1. Set PTR = Head->Link;
2. Repeat Steps 3 and 4 while PTR  Head
3. Print PTR->Data
4. Set PTR = PTR ->Link
5. Exit
40
Locating an ITEM
• Let Head be a circular header list in
memory. Write an algorithm to find the
location LOC of the first node in Head
which contains ITEM or return LOC =
NULL when the item is not present.
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
Header Node Trailer Node
Applications of Linked Lists
1. Polynomial Representation and operation on
Polynomials
Ex: 10 X 6 +20 X 3 + 55
2. Sparse Matrix Representation
0 0 11
22 0 0
0 0 66
021
021 ...)( ee
m
e
m xaxaxaxA mm
 

Representation of Node
Polynomials
coef expon link
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
123 814
 xxa
61014
1038 xxxb 
null
null
Example
Polynomial Operation
1. Addition
2. Subtraction
3. Multiplication
4. Scalar Division
3 14 2 8 1 0
a
-3 108 14 10 6
b
11 14
d
a->expon == b->expon
a
b
d
a->expon < b->expon
-3 10
Polynomial Addition
3 14 2 8 1 0
-3 108 14 10 6
11 14
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
a->expon > b->expon
-3 10
d
2 8
Polynomial Addition
3 14 2 8
1 0
a
8 14 -3 10 10 6
b
11 14
a->expon > b->expon
-3 10
d
2 8
Polynomial Addition (cont’d)
10 6
1 0
3 14 2 8 1 0
a
-3 108 14 10 6
b
-5 14
d
a->expon == b->expon
a
b
d
a->expon < b->expon
3 10
Polynomial Subtraction
3 14 2 8 1 0
-3 108 14 10 6
-5 14
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14
a->expon > b->expon
3 10
d
2 8
Polynomial Subtraction (cont’d)
3 14 2 8
1 0
a
8 14 -3 10 10 6
b
-5 14
a->expon > b->expon
3 10
d
2 8
Polynomial Addition (cont’d)
-10 6
1 0
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
m n -- i j val i j val Null













0830
0000
9072
0500
A
4 4 -- 0 2 5 3 2 8 Null
Linked list representation
Sparse Matrix
aij
i j
ROWLINK
COLLINK
Linked list representation
Sparse Matrix













0830
0000
9072
0500
A
0
1
2
3
2 5
0 2 1 7 3 9
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
61
INFO field
BACK pointer
FORW pointer
Two-Way List
62
FIRST LAST
Two-Way List
63
Suppose LOCA and LOCB are the locations
of nodes A and B respectively in a two-way
list.
The statement that node B follows node A
is equivalent to the statement that node A
precedes node B
Pointer Property: LOCA->FORW = LOCB if
and only if LOCB->BACK = LOCA
Two-Way List
64
FIRST LAST
A B
Forward Pointer
Node A Backward Pointer
Node A
Two-Way List
65
FIRST LAST
A B
LOCA LOCB
LOCA->FORW = LOCB if and only if
LOCB->BACK = LOCA
Operation in two-way list
• Traversing
• Searching
• Deleting
• Inserting
66
Deletion in Two-Way List
67
FIRST LAST
A B
LOCB
DELETE NODE B
C
LOCB->BACK->FORW LOCB->FORW=
Deletion in Two-Way List
68
FIRST LAST
A B
LOCB
DELETE NODE B
C
LOCB->FORW->BACK LOCB->BACK=
Insertion in Two-Way List
69
FIRST LAST
A B
LOCA
INSERT NODE NEW
C
NEW
LOCA->FORW->BACK = NEW
NEW->FORW = LOCA->FORW
LOCA->FORW = NEW
NEW->BACK = LOCA
?
70

More Related Content

What's hot (16)

PPTX
Linked list
VONI
 
PPT
Data structure lecture 5
Kumar
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
PPTX
Linked List
Ashim Lamichhane
 
PPT
Link list
Ravi Gautam
 
PPTX
Presentation on circular linked list
mehak1996
 
PPT
linked list (c#)
swajahatr
 
PPT
Singly link list
Rojin Khadka
 
PPTX
Ppt of operations on one way link list
Sukhdeep Kaur
 
PPTX
Linked list
Dr. Shashank Shetty
 
PPTX
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
PPT
Operations on linked list
Sumathi Kv
 
PPTX
Linked lists 1
naymulhaque
 
PPT
Linked list
Priyanka Rana
 
PDF
Sortsearch
maverick2203
 
Linked list
VONI
 
Data structure lecture 5
Kumar
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Linked List
Ashim Lamichhane
 
Link list
Ravi Gautam
 
Presentation on circular linked list
mehak1996
 
linked list (c#)
swajahatr
 
Singly link list
Rojin Khadka
 
Ppt of operations on one way link list
Sukhdeep Kaur
 
Linked list
Dr. Shashank Shetty
 
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Operations on linked list
Sumathi Kv
 
Linked lists 1
naymulhaque
 
Linked list
Priyanka Rana
 
Sortsearch
maverick2203
 

Viewers also liked (20)

PPTX
Exception handling
Luis Goldster
 
PPTX
Relationship Management and CRM
Hassan Obaid
 
PPTX
Python data structures
Harry Potter
 
PDF
Fabricating Customized hydrogel contact lens
Andre Childs
 
PPT
Sql database object
James Wong
 
PPTX
Behaviour drivendevelopment
Luis Goldster
 
PPT
Xml and webdata
Luis Goldster
 
DOC
Chapman Resume January 2016 LinkedIn
Judy Chapman
 
DOCX
Amenazas a la máquina
Bochinlution
 
PPT
Gm theory
Harry Potter
 
PPTX
Blockchain: Hype or Revolution?
Kuldeep Sawant
 
PDF
Circular linked list
maamir farooq
 
PDF
Fa qs on foreign investment in the philippines
atoydequit
 
PDF
circular linked list
Materi Kuliah Online
 
PPTX
Circular linked list
Sayantan Sur
 
PPS
Harry Potter and the Philosopher's Stone
Deepshikha Sharma
 
PPSX
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
PPT
Game theory
Young Alista
 
PPTX
Doubly linked list
Fahd Allebdi
 
PPT
Circular linked list
dchuynh
 
Exception handling
Luis Goldster
 
Relationship Management and CRM
Hassan Obaid
 
Python data structures
Harry Potter
 
Fabricating Customized hydrogel contact lens
Andre Childs
 
Sql database object
James Wong
 
Behaviour drivendevelopment
Luis Goldster
 
Xml and webdata
Luis Goldster
 
Chapman Resume January 2016 LinkedIn
Judy Chapman
 
Amenazas a la máquina
Bochinlution
 
Gm theory
Harry Potter
 
Blockchain: Hype or Revolution?
Kuldeep Sawant
 
Circular linked list
maamir farooq
 
Fa qs on foreign investment in the philippines
atoydequit
 
circular linked list
Materi Kuliah Online
 
Circular linked list
Sayantan Sur
 
Harry Potter and the Philosopher's Stone
Deepshikha Sharma
 
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
Game theory
Young Alista
 
Doubly linked list
Fahd Allebdi
 
Circular linked list
dchuynh
 
Ad

Similar to 4.linked list(contd.) (20)

PPT
ds 4Linked lists.ppt
AlliVinay1
 
PPTX
linked list in dsa python (presentation)
MirzaAbdullahTariq
 
PPTX
Engineering.CSE.DataStructure.Linkedlist.notes
limev72215
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PPT
Data Structures with C Linked List
Reazul Islam
 
PDF
unit 2- linked list- PPT for btech students.pdf
soniasharmafdp
 
PPTX
Ppt of operations on one way link list
Sukhdeep Kaur
 
PDF
unit 2- PPT.pdf
PranavMakwana6
 
PPTX
Data structures linked list introduction.pptx
Kalpana Mohan
 
PPT
linked list1.ppt linked list ppts and notes
nisharaheja1986
 
PPT
cp264_lecture13_14_linkedlist.ppt
ssuser9dd05f
 
PPTX
5.Linked list
Mandeep Singh
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
PPTX
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
PPTX
DSModule2.pptx
ChrisSosaJacob
 
PPT
Linkedlists
Rajendran
 
PPTX
Linked list (1).pptx
rajveersingh643731
 
PDF
linkrd_list.pdf
ISHAN194169
 
PDF
ds-lecture-4-171012041008 (1).pdf
KamranAli649587
 
ds 4Linked lists.ppt
AlliVinay1
 
linked list in dsa python (presentation)
MirzaAbdullahTariq
 
Engineering.CSE.DataStructure.Linkedlist.notes
limev72215
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Data Structures with C Linked List
Reazul Islam
 
unit 2- linked list- PPT for btech students.pdf
soniasharmafdp
 
Ppt of operations on one way link list
Sukhdeep Kaur
 
unit 2- PPT.pdf
PranavMakwana6
 
Data structures linked list introduction.pptx
Kalpana Mohan
 
linked list1.ppt linked list ppts and notes
nisharaheja1986
 
cp264_lecture13_14_linkedlist.ppt
ssuser9dd05f
 
5.Linked list
Mandeep Singh
 
Unit ii(dsc++)
Durga Devi
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
DSModule2.pptx
ChrisSosaJacob
 
Linkedlists
Rajendran
 
Linked list (1).pptx
rajveersingh643731
 
linkrd_list.pdf
ISHAN194169
 
ds-lecture-4-171012041008 (1).pdf
KamranAli649587
 
Ad

More from Chandan Singh (20)

PDF
Fundamental of Tissue engineering
Chandan Singh
 
PPT
Resistance Measurement instruments
Chandan Singh
 
PDF
Resistance Measurement instruments
Chandan Singh
 
PDF
Moving iron (MI) instruments
Chandan Singh
 
PPT
Moving iron (MI) instruments
Chandan Singh
 
PPT
Electrical Measurement & Instruments
Chandan Singh
 
PPT
Static characteristics of Instruments
Chandan Singh
 
PPT
Resistance measurement
Chandan Singh
 
PPT
Introduction to sensors
Chandan Singh
 
PPT
Energy meter
Chandan Singh
 
PPT
Classification (Analog instruments)
Chandan Singh
 
PPT
AC Bridges: Balance Condition
Chandan Singh
 
PPT
Cathode Ray Osciloscope
Chandan Singh
 
PPT
Instrument transformer CT & PT
Chandan Singh
 
PPT
Megohmmeter
Chandan Singh
 
PPT
Moving Iron
Chandan Singh
 
PPT
Permanent Magnet Moving Coil
Chandan Singh
 
PPTX
10.m way search tree
Chandan Singh
 
PPTX
9.bst(contd.) avl tree
Chandan Singh
 
PPTX
8.binry search tree
Chandan Singh
 
Fundamental of Tissue engineering
Chandan Singh
 
Resistance Measurement instruments
Chandan Singh
 
Resistance Measurement instruments
Chandan Singh
 
Moving iron (MI) instruments
Chandan Singh
 
Moving iron (MI) instruments
Chandan Singh
 
Electrical Measurement & Instruments
Chandan Singh
 
Static characteristics of Instruments
Chandan Singh
 
Resistance measurement
Chandan Singh
 
Introduction to sensors
Chandan Singh
 
Energy meter
Chandan Singh
 
Classification (Analog instruments)
Chandan Singh
 
AC Bridges: Balance Condition
Chandan Singh
 
Cathode Ray Osciloscope
Chandan Singh
 
Instrument transformer CT & PT
Chandan Singh
 
Megohmmeter
Chandan Singh
 
Moving Iron
Chandan Singh
 
Permanent Magnet Moving Coil
Chandan Singh
 
10.m way search tree
Chandan Singh
 
9.bst(contd.) avl tree
Chandan Singh
 
8.binry search tree
Chandan Singh
 

Recently uploaded (20)

PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
quantum computing transition from classical mechanics.pptx
gvlbcy
 
PDF
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PPTX
Ground improvement techniques-DEWATERING
DivakarSai4
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
Zero Carbon Building Performance standard
BassemOsman1
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
Information Retrieval and Extraction - Module 7
premSankar19
 
quantum computing transition from classical mechanics.pptx
gvlbcy
 
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
Ground improvement techniques-DEWATERING
DivakarSai4
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 

4.linked list(contd.)

  • 1. Data Structure & Algorithm CS 102 Ashok K Turuk
  • 4. = Head Data Link PTR = PTR - > Link 4
  • 5. List Traversal Let Head be a pointer to a linked list in memory. Write an algorithm to print the contents of each node of the list 5
  • 6. 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
  • 7. Search for an ITEM • Let Head be a pointer to a linked list in memory. Write an algorithm that finds the location LOC of the node where ITEM first appears in the list, or sets LOC = NULL if search is unsuccessful. 7
  • 8. 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
  • 9. 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
  • 10. Insertion to a Linked List Head Data Link 10
  • 11. Insertion to Beginning Head Data Link New NodePTR 11
  • 12. Insertion to Beginning Head Data Link New NodePTR PTR->Link = Head 12
  • 13. Insertion to Beginning Head Data Link New NodePTR PTR->Link = Head , Head = PTR 13
  • 14. Overflow and Underflow • Overflow : A new Data to be inserted into a data structure but there is no available space. • Underflow: A situation where one wants to delete data from a data structure that is empty. 14
  • 15. Insertion to Beginning Head Data Link New NodePTR Overflow , PTR == NULL 15
  • 16. Insertion at the Beginning Let Head be a pointer to a linked list in memory. Write an algorithm to insert node PTR at the beginning of the List. 16
  • 17. Insertion at the Beginning Algorithm 1. If PTR == NULL , then Write Overflow and Exit 2. Set PTR -> DATA = ITEM 3. Set PTR -> LINK = Head -> LINK 4. Set Head = PTR 5. Exit 17
  • 18. Insertion After a Given Node Let Head be a pointer to a linked list in memory. Write an algorithm to insert ITEM so that ITEM follows the node with location LOC or insert ITEM as the first node when LOC == NULL 18
  • 19. Insertion at a Given Node Head Data Link New NodePTR A BA LOC 19
  • 20. Insertion at a Given Node Head Data Link New NodePTR BA LOC PTR->Link = LOC->Link 20
  • 21. Insertion at a Given Node Head Data Link New NodePTR BA LOC LOC->Link = PTR 21
  • 22. 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
  • 23. Insertion into a Sorted Linked List Head Data Link 1000 2000 3000 4000 5000 3500 3000 < 3500 < 4000 23
  • 24. Insertion into a Sorted Linked List 1000 2000 3000 4000 5000 3000 < 3500 < 4000 BA To Insert Between Node A and B We have to Remember the Pointer to Node A which is the predecessor Node of B 24
  • 25. Insertion into a Sorted Linked List Steps to Find the LOC of Insertion 1. If Head == NULL, then Set LOC = NULL and Return 2. If ITEM < Head ->Data , then Set LOC = NULL and Return 3. Set SAVE = Head and PTR = Head -> Link 4. Repeat Steps 5 and 6 while PTR  NULL 5. If ITEM < PTR -> Data then LOC = SAVE and Return 6. Set SAVE = PTR and PTR = PTR->Link 7. Set LOC = SAVE 8. Return 25
  • 28. Delete the Node Following a Given Node • Write an Algorithm that deletes the Node N with location LOC. LOCP is the location of the node which precedes N or when N is the first node LOCP = NULL 28
  • 29. Delete the Node Following a Given Node • Algorithm: Delete(Head, LOC, LOCP) 1 If LOCP = NULL then Set Head = Head ->Link. [Deletes the 1st Node] Else Set LOCP->Link = LOC->Link [Deletes Node N] 2. Exit 29
  • 30. Delete an Item Let Head be a pointer to a linked list in memory that contains integer data. Write an algorithm to delete node which contains ITEM. 30
  • 31. Delete an Item 1000 2000 3000 4000 5000 4000 BA To delete a Node [Node B] We have to Remember the Pointer to its predecessor [Node A] 31
  • 32. Deletion of an ITEM Algorithm 1. Set PTR=Head and TEMP = Head 2. Repeat step 3 while PTR  NULL 3. If PTR->DATA == ITEM, then Set TEMP->LINK = PTR -> LINK, exit else TEMP = PTR PTR = PTR -> LINK 4. Stop 32
  • 33. Deletion of an ITEM Algorithm 1. If Head == NULL , then Write ITEM is not in the List, Exit 2. If Head ->Data == Item Head = Head -> Link 3. Set PTR=Head and TEMP = Head 33
  • 34. Deletion of an ITEM Algorithm 4. Repeat step 4 while PTR  NULL If PTR->DATA == ITEM, then Set TEMP->LINK = PTR -> LINK, exit else TEMP = PTR PTR = PTR -> LINK 5. Stop 34
  • 35. Header Linked Lists • A header linked list is a linked list which always contains a special node called header node • Grounded Header List: A header list where the last node contains the NULL pointer. Header Node 35
  • 36. Header Linked Lists • Circular Header List: A header list where the last node points back to the header node. Header Node 36
  • 37. Header Linked Lists • Pointer Head always points to the header node. • Head->Link == NULL indicates that a grounded header list is empty • Head->Link == Head indicates that a circular header list is empty 37
  • 38. 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
  • 39. Traversing a Circular Header List • Let Head be a circular header list in memory. Write an algorithm to print Data in each node in the list. 39
  • 40. Traversing a Circular Header List Algorithm 1. Set PTR = Head->Link; 2. Repeat Steps 3 and 4 while PTR  Head 3. Print PTR->Data 4. Set PTR = PTR ->Link 5. Exit 40
  • 41. Locating an ITEM • Let Head be a circular header list in memory. Write an algorithm to find the location LOC of the first node in Head which contains ITEM or return LOC = NULL when the item is not present. 41
  • 42. 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
  • 43. 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
  • 44. 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 Header Node Trailer Node
  • 45. Applications of Linked Lists 1. Polynomial Representation and operation on Polynomials Ex: 10 X 6 +20 X 3 + 55 2. Sparse Matrix Representation 0 0 11 22 0 0 0 0 66
  • 46. 021 021 ...)( ee m e m xaxaxaxA mm    Representation of Node Polynomials coef expon link
  • 47. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 123 814  xxa 61014 1038 xxxb  null null Example
  • 48. Polynomial Operation 1. Addition 2. Subtraction 3. Multiplication 4. Scalar Division
  • 49. 3 14 2 8 1 0 a -3 108 14 10 6 b 11 14 d a->expon == b->expon a b d a->expon < b->expon -3 10 Polynomial Addition 3 14 2 8 1 0 -3 108 14 10 6 11 14
  • 50. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon > b->expon -3 10 d 2 8 Polynomial Addition
  • 51. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon > b->expon -3 10 d 2 8 Polynomial Addition (cont’d) 10 6 1 0
  • 52. 3 14 2 8 1 0 a -3 108 14 10 6 b -5 14 d a->expon == b->expon a b d a->expon < b->expon 3 10 Polynomial Subtraction 3 14 2 8 1 0 -3 108 14 10 6 -5 14
  • 53. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b -5 14 a->expon > b->expon 3 10 d 2 8 Polynomial Subtraction (cont’d)
  • 54. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b -5 14 a->expon > b->expon 3 10 d 2 8 Polynomial Addition (cont’d) -10 6 1 0
  • 55. 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.
  • 56. Linked list representation Sparse Matrix m n -- i j val i j val Null              0830 0000 9072 0500 A 4 4 -- 0 2 5 3 2 8 Null
  • 57. Linked list representation Sparse Matrix aij i j ROWLINK COLLINK
  • 58. Linked list representation Sparse Matrix              0830 0000 9072 0500 A 0 1 2 3 2 5 0 2 1 7 3 9 1 3 2 8
  • 59. 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
  • 60. 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
  • 61. 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 61 INFO field BACK pointer FORW pointer
  • 63. Two-Way List 63 Suppose LOCA and LOCB are the locations of nodes A and B respectively in a two-way list. The statement that node B follows node A is equivalent to the statement that node A precedes node B Pointer Property: LOCA->FORW = LOCB if and only if LOCB->BACK = LOCA
  • 64. Two-Way List 64 FIRST LAST A B Forward Pointer Node A Backward Pointer Node A
  • 65. Two-Way List 65 FIRST LAST A B LOCA LOCB LOCA->FORW = LOCB if and only if LOCB->BACK = LOCA
  • 66. Operation in two-way list • Traversing • Searching • Deleting • Inserting 66
  • 67. Deletion in Two-Way List 67 FIRST LAST A B LOCB DELETE NODE B C LOCB->BACK->FORW LOCB->FORW=
  • 68. Deletion in Two-Way List 68 FIRST LAST A B LOCB DELETE NODE B C LOCB->FORW->BACK LOCB->BACK=
  • 69. Insertion in Two-Way List 69 FIRST LAST A B LOCA INSERT NODE NEW C NEW LOCA->FORW->BACK = NEW NEW->FORW = LOCA->FORW LOCA->FORW = NEW NEW->BACK = LOCA
  • 70. ? 70