SlideShare ist ein Scribd-Unternehmen logo
1 von 70
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

PTR

Data Link

New Node
11
Insertion to Beginning
Head

PTR

Data Link

New Node
PTR->Link = Head
12
Insertion to Beginning
Head

PTR

Data Link

New Node
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

PTR

Data Link

New Node
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
LOC

A

PTR

B

New Node
19
Insertion at a Given Node
Head

Data Link
LOC

A

PTR

B

New Node

PTR->Link = LOC->Link

20
Insertion at a Given Node
Head

Data Link
LOC

B

A

PTR

New Node

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

3500

3000

4000

5000

3000 < 3500 < 4000
23
Insertion into a Sorted Linked
List
To Insert Between Node A and B We have to
Remember the Pointer to Node A which is the
predecessor Node of B
1000

2000

3000

4000

A

5000

B

3000 < 3500 < 4000
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
To delete a Node [Node B] We have to
Remember the Pointer to its predecessor
[Node A]
1000

2000

3000

4000

A

5000

B

4000
31
Deletion of an ITEM

Algorithm
1.
2.
3.

Set PTR=Head and TEMP = Head
Repeat step 3 while PTR NULL
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.
2.
3.
4.
5.

Set PTR = Head->Link;
Repeat Steps 3 and 4 while PTR
Print PTR->Data
Set PTR = PTR ->Link
Exit

Head

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
PTR Head

ITEM and

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
Header Node

Trailer Node

44
Applications of Linked Lists
1. Polynomial Representation and operation on
Polynomials

Ex: 10 X

6

+20 X

3

+ 55

2. Sparse Matrix Representation
0
22
0

0
0
0

11
0
66
Polynomials
A( x)

am 1 x

coef

em

1

am 2 x

expon

em

2

...

link

Representation of Node

a0 x

e0
Example
14

a

3x
a

3 14
14

b

8x
b

2x

8

8 14

1
1 0 null

2 8
10

3x

10x

-3 10

6

10 6 null
Polynomial Operation
1.
2.
3.
4.

Addition
Subtraction
Multiplication
Scalar Division
Polynomial Addition
3 14
a
8

14

b

2

8

1

0

-3 10

10

6

11 14
d

3 14
8

14

11 14

a->expon == b->expon
2

8

a

-3 10

b
-3 10
d

1

0

10

6

a->expon < b->expon
Polynomial Addition
3 14

2 8
a

8 14

-3 10

11 14

-3 10

a->expon > b->expon

1 0
10 6

b
2 8
d
Polynomial Addition (cont’d)
3

2

8

11 14

14

14

-3 10

-3 10

8

2

8

a->expon > b->expon

1 0

10 6
10 6

a

b

1 0

d
Polynomial Subtraction
3 14
a
8

14

b

2

8

1

0

-3 10

10

6

-5 14
d

3 14
8

14

-5 14

a->expon == b->expon
2

8

a

-3 10

b
3 10
d

1

0

10

6

a->expon < b->expon
Polynomial Subtraction (cont’d)
3 14

2 8
a

8 14

-3 10

-5 14

3 10

a->expon > b->expon

1 0
10 6

b
2 8
d
Polynomial Addition (cont’d)
3

2

8

-5 14

14

14

-3 10

3 10

8

2

8

a->expon > b->expon

1 0

10 6
-10 6

a

b

1 0

d
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

0 2 5

3 2 8 Null

0 0 5 0
A

2 7 0 9
0 0 0 0
0 3 8 0

4 4

--
Linked list representation
Sparse Matrix

ROWLINK
i

j
aij
COLLINK
Linked list representation
Sparse Matrix
0

0 0 5 0
A

2

5

1

0

2

1

3

1

7

2 7 0 9
0 0 0 0
0 3 8 0

2

3

2

8

3

9
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.
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
63
Two-Way List
FIRST
A
Forward Pointer
Node A

LAST
B
Backward Pointer
Node A
64
Two-Way List
FIRST

LAST
A
LOCA

B
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
B

A

C

LOCB
LOCB->BACK->FORW

= LOCB->FORW
67
Deletion in Two-Way List
DELETE NODE B
FIRST

LAST
B

A

C

LOCB
LOCB->FORW->BACK

= LOCB->BACK
68
Insertion in Two-Way List
INSERT NODE NEW
LOCA->FORW->BACK = NEW
NEW->FORW = LOCA->FORW
FIRST

LOCA

NEW

A

B

LAST

C

LOCA->FORW = NEW
NEW->BACK = LOCA
69
?
70

Weitere ähnliche Inhalte

Was ist angesagt?

linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Linked list
Linked listLinked list
Linked listVONI
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5Kumar
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queueskalyanineve
 
Linked list
Linked listLinked list
Linked listakshat360
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Data structure
Data  structureData  structure
Data structureArvind Kumar
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure conceptsAkila Krishnamoorthy
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashingKrish_ver2
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing AlgorithmHayi Nukman
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Hash table
Hash tableHash table
Hash tableRajendran
 

Was ist angesagt? (18)

6.queue
6.queue6.queue
6.queue
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Linked list
Linked listLinked list
Linked list
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
Linklist
LinklistLinklist
Linklist
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Linked list
Linked listLinked list
Linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Data structure
Data  structureData  structure
Data structure
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Linked list
Linked listLinked list
Linked list
 
Hash table
Hash tableHash table
Hash table
 

Andere mochten auch

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
12111 data structure
12111 data structure12111 data structure
12111 data structureGaurang Thakar
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Listas Lineares - Parte 1
Listas Lineares - Parte 1Listas Lineares - Parte 1
Listas Lineares - Parte 1Artur Barreto
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Aula 01 -_pilhas_e_filas_com_vetores-oop
Aula 01 -_pilhas_e_filas_com_vetores-oopAula 01 -_pilhas_e_filas_com_vetores-oop
Aula 01 -_pilhas_e_filas_com_vetores-oopJean Martina
 
Aula01 - estrutura de dados
Aula01 - estrutura de dadosAula01 - estrutura de dados
Aula01 - estrutura de dadosAbner Lima
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Estrutura de Dados - Listas Encadeadas
Estrutura de Dados - Listas EncadeadasEstrutura de Dados - Listas Encadeadas
Estrutura de Dados - Listas EncadeadasAdriano Teixeira de Souza
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
Estrutura de Dados - Conceitos fundamentais
Estrutura de Dados - Conceitos fundamentaisEstrutura de Dados - Conceitos fundamentais
Estrutura de Dados - Conceitos fundamentaisFabrĂ­cio Lopes Sanchez
 

Andere mochten auch (20)

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
12111 data structure
12111 data structure12111 data structure
12111 data structure
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Listas Lineares - Parte 1
Listas Lineares - Parte 1Listas Lineares - Parte 1
Listas Lineares - Parte 1
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Aula 01 -_pilhas_e_filas_com_vetores-oop
Aula 01 -_pilhas_e_filas_com_vetores-oopAula 01 -_pilhas_e_filas_com_vetores-oop
Aula 01 -_pilhas_e_filas_com_vetores-oop
 
Aula01 - estrutura de dados
Aula01 - estrutura de dadosAula01 - estrutura de dados
Aula01 - estrutura de dados
 
Pilhas e Filas
Pilhas e FilasPilhas e Filas
Pilhas e Filas
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Estrutura de Dados - Listas Encadeadas
Estrutura de Dados - Listas EncadeadasEstrutura de Dados - Listas Encadeadas
Estrutura de Dados - Listas Encadeadas
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Estrutura de dados - Filas
Estrutura de dados - FilasEstrutura de dados - Filas
Estrutura de dados - Filas
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Estrutura de dados - Pilhas
Estrutura de dados - PilhasEstrutura de dados - Pilhas
Estrutura de dados - Pilhas
 
Estrutura de Dados - Conceitos fundamentais
Estrutura de Dados - Conceitos fundamentaisEstrutura de Dados - Conceitos fundamentais
Estrutura de Dados - Conceitos fundamentais
 

Ähnlich wie Lecture 5 data structures and algorithms

4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)Chandan Singh
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.pptAlliVinay1
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked listsstudent
 
Singly link list
Singly link listSingly link list
Singly link listRojin Khadka
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list Mandeep Singh
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptxnikhilcse1
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiSowmya Jyothi
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfAxmedcarb
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptssuser9dd05f
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdfKaynattariq1
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptxmsohail37
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked listNirmalPandey23
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notesDreamers6
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdfISHAN194169
 

Ähnlich wie Lecture 5 data structures and algorithms (20)

4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Singly link list
Singly link listSingly link list
Singly link list
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdf
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.ppt
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
 
Lect 11-12 Zaheer Abbas
Lect 11-12 Zaheer AbbasLect 11-12 Zaheer Abbas
Lect 11-12 Zaheer Abbas
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 

KĂźrzlich hochgeladen

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

KĂźrzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Lecture 5 data structures and algorithms

  • 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
  • 12. Insertion to Beginning Head PTR Data Link New Node PTR->Link = Head 12
  • 13. Insertion to Beginning Head PTR Data Link New Node 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 PTR Data Link New Node 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 LOC A PTR B New Node 19
  • 20. Insertion at a Given Node Head Data Link LOC A PTR B New Node PTR->Link = LOC->Link 20
  • 21. Insertion at a Given Node Head Data Link LOC B A PTR New Node 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 3500 3000 4000 5000 3000 < 3500 < 4000 23
  • 24. Insertion into a Sorted Linked List To Insert Between Node A and B We have to Remember the Pointer to Node A which is the predecessor Node of B 1000 2000 3000 4000 A 5000 B 3000 < 3500 < 4000 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 To delete a Node [Node B] We have to Remember the Pointer to its predecessor [Node A] 1000 2000 3000 4000 A 5000 B 4000 31
  • 32. Deletion of an ITEM Algorithm 1. 2. 3. Set PTR=Head and TEMP = Head Repeat step 3 while PTR NULL 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. 2. 3. 4. 5. Set PTR = Head->Link; Repeat Steps 3 and 4 while PTR Print PTR->Data Set PTR = PTR ->Link Exit Head 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 PTR Head ITEM and 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 Header Node Trailer Node 44
  • 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 22 0 0 0 0 11 0 66
  • 46. Polynomials A( x) am 1 x coef em 1 am 2 x expon em 2 ... link Representation of Node a0 x e0
  • 47. Example 14 a 3x a 3 14 14 b 8x b 2x 8 8 14 1 1 0 null 2 8 10 3x 10x -3 10 6 10 6 null
  • 49. Polynomial Addition 3 14 a 8 14 b 2 8 1 0 -3 10 10 6 11 14 d 3 14 8 14 11 14 a->expon == b->expon 2 8 a -3 10 b -3 10 d 1 0 10 6 a->expon < b->expon
  • 50. Polynomial Addition 3 14 2 8 a 8 14 -3 10 11 14 -3 10 a->expon > b->expon 1 0 10 6 b 2 8 d
  • 51. Polynomial Addition (cont’d) 3 2 8 11 14 14 14 -3 10 -3 10 8 2 8 a->expon > b->expon 1 0 10 6 10 6 a b 1 0 d
  • 52. Polynomial Subtraction 3 14 a 8 14 b 2 8 1 0 -3 10 10 6 -5 14 d 3 14 8 14 -5 14 a->expon == b->expon 2 8 a -3 10 b 3 10 d 1 0 10 6 a->expon < b->expon
  • 53. Polynomial Subtraction (cont’d) 3 14 2 8 a 8 14 -3 10 -5 14 3 10 a->expon > b->expon 1 0 10 6 b 2 8 d
  • 54. Polynomial Addition (cont’d) 3 2 8 -5 14 14 14 -3 10 3 10 8 2 8 a->expon > b->expon 1 0 10 6 -10 6 a b 1 0 d
  • 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 0 2 5 3 2 8 Null 0 0 5 0 A 2 7 0 9 0 0 0 0 0 3 8 0 4 4 --
  • 57. Linked list representation Sparse Matrix ROWLINK i j aij COLLINK
  • 58. Linked list representation Sparse Matrix 0 0 0 5 0 A 2 5 1 0 2 1 3 1 7 2 7 0 9 0 0 0 0 0 3 8 0 2 3 2 8 3 9
  • 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 INFO field BACK pointer FORW pointer 61
  • 63. Two-Way List 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 63
  • 64. Two-Way List FIRST A Forward Pointer Node A LAST B Backward Pointer Node A 64
  • 65. Two-Way List FIRST LAST A LOCA B LOCB LOCA->FORW = LOCB if and only if LOCB->BACK = LOCA 65
  • 66. Operation in two-way list • • • • Traversing Searching Deleting Inserting 66
  • 67. Deletion in Two-Way List DELETE NODE B FIRST LAST B A C LOCB LOCB->BACK->FORW = LOCB->FORW 67
  • 68. Deletion in Two-Way List DELETE NODE B FIRST LAST B A C LOCB LOCB->FORW->BACK = LOCB->BACK 68
  • 69. Insertion in Two-Way List INSERT NODE NEW LOCA->FORW->BACK = NEW NEW->FORW = LOCA->FORW FIRST LOCA NEW A B LAST C LOCA->FORW = NEW NEW->BACK = LOCA 69
  • 70. ? 70