SlideShare a Scribd company logo
1 of 26
1
Linked List
2
Need of Dynamic Data Structures
• Linked Lists are dynamic data structures: They can grow
or shrink during the execution of a program
• Efficient memory utilization: Memory is allocated
whenever it is required and deallocated when it is no
longer needed
• Insertion and Deletion are easier & efficient: It provides
flexibility in inserting a data item at a specified position
and deletion of a data item from the given position
• Many complex applications can be easily carried out with
linked list
3
Continuous Implementation of Lists
• It can be done with help of continuous memory
allocation data structure i.e. Arrays
• For the purpose of implementing lists using arrays we
are required to create a structure of two elements.
1. Info
2. Next
 And then create the array (variable) of structure.
e.g. struct node
{ int info,next;};
struct node n[100];
4
A linked list node
• Each node contains a data item and a pointer to the next
node
• The last node has no next node
• An external pointer gives access to the first node
data next next
first
5
Linked list notation
. . . . . .
p
node(p) Predecessor node(p) node(p) Successor
info(p) next(p)
6
Accessing nodes in a linked list
• No direct access to individual nodes
• First node accessed via external pointer
• Other nodes accessed via pointer in previous
node
• It is a sequential access data structure
first
7
Operation on Lists
1. Traversing
2. Searching
3. Inserting
4. Deleting
8
TRAVERSING A LINKED LIST
1. Set PTR = START
2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR=LINK[PTR]
5. Exit
9
Types of Linked Lists
1. Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
2. Circular, singly linked
• Pointer in the last node points back to the first node
3. Doubly linked list
• Two “start pointers” – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
4. Circular, doubly linked list
• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
10
Singly Linked List
• A singly linked list is a concrete
data structure consisting of a
sequence of nodes
• Each node stores
– element
– link to the next node
next
elem node
A B C D

11
Adding an item
• Three steps:
– find node after which to add
– Allocate space for a node and store data in it
– Link the new node into the existing linked list
• Two cases for linking the node into the existing
linked list
– Adding an item at the beginning of the list
– Adding an item after an existing node
12
Inserting at the Beginning
NULLfirst 'A' 'B'
p
Initial List
1. Create a new node.
'C'p
2. Fill the new node with data.
'A' 'B'
3. Make the new node point to the first node.
4. Make first point to the new node .
'A' 'B'
first
'C'p
'C'p
NULL
NULL
first
13
Inserting at the Beginning (Algo.)
INSERT(INFO,LINK,START,AVAIL,ITEM)
1. IF AVAIL=NULL then Write OVERFLOW and EXIT.
2. NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. LINK[NEW]=START
5. Set START=NEW
6. EXIT.
14
Inserting after a given NodeINSERTLOC(INFO,LINK,START,AVAIL,ITEM)
1. IF AVAIL=NULL then Write OVERFLOW and Exit
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. If LOC=NULL then
Set LINK[NEW]= START and START=NEW
Else
Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW
5. Exit.
15
Inserting into Sorted List
FIND(INFO, LINK, START, ITEM, LOC)
1. If START =NULL then Set LOC=NULL and Return.
2. If ITEM<INFO[START] then Set LOC=NULL and Return
3. Set SAVE=START and PTR=LINK[START].
4. Repeat Step 5 AND 6 While PTR!= NULL.
5. If ITEM<INFO[PTR] then Set LOC=SAVE and Return.
6. Set SAVE=PTR and PTR = LINK[PTR]
7. Set LOC= SAVE.
8. Return.
INSERT(INFO,LINK,START,AVAIL,ITEM)
1. Call FIND(INFO, LINK, START. ITEM, LOC)
2. Call INSERTLOC(INFO,LINK,START,AVAIL,ITEM)
16
Deleting the Node Following a given Node
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. IF LOCP=NULL, THEN, Set START=LINK[START]
ELSE Set LINK[LOCP]=LINK[LOC]
2. Set LINK[LOC]=AVAIL and AVAIL=LOC
3. Exit.
Here, LOCP is Predecessor of Location.
17
Header linked lists
• Header linked list is a linked list which always
contains a special node called the Header Node, at
the beginning of the list.
Header linked list
Grounded Header linked list Circular Header linked list
18
• Grounded Header linked lists
– The last node contains null pointer.
A
Head
B C X
19
• Circular linked lists
– The last node points to the first node of the
list
– How do we know when we have finished
traversing the list?
A
Head
B C
20
Comparison between Array & Dynamic
Implementation of Linked List
ARRAY LINKED LIST
1. Linear collection of data
elements
1. Linear collection of nodes
2. Consecutive memory
allocation
2. Not Consecutive memory
allocation
3. Allow Sequential & Random
Access of data
3. Allow only Sequential Access
of data
4. Insertions / Deletions are not
efficient
4. Insertions / Deletions are
efficient with a constant time
5. Can’t add any number of
elements at run time
5. Can add any number of
elements in the list at run time
6. No extra cost of saving
address of next element
6. Extra cost of saving address
of next element in each node
21
Circular Lists
• The last node contains pointer to the first
node of the list
• We can begin at any node and traverse the list
until we reach the same node where we
started
A
START
B C
22
Inserting an element in the beginning of a
Circular List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while LINK[PTR]!=START
6. PTR=LINK[PTR]
7. Set LINK[NEW]=START
8. Set LINK[PTR]=NEW
9. Set START=NEW
10. Exit
23
Inserting an element at the end of a Circular
List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set LINK[NEW]=START
5. Set PTR=START
6. Repeat Step 7 while LINK[PTR]!=START
7. PTR=LINK[PTR]
8. Set LINK[PTR]=NEW
9. Exit
24
Deleting the First node from Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set PTR=LINK[PTR]
5. Set LINK[PTR]=LINK[START]
6. LINK[START]=AVAIL and AVAIL=START
7. START=LINK[PTR]
8. Exit
25
Deleting the Last node from Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set SAVE=PTR and PTR=LINK[PTR]
5. Set LINK[SAVE]=START
6. LINK[PTR]=AVAIL and AVAIL=PTR
7. Exit
26
Doubly Linked List
• A Doubly linked list is a two way linked list, where
each node N is divided into three parts:
1. An information field INFO which contains the data
of N
2. A pointer FORW which contains the location of the
next node in the list
3. A pointer field BACK which conatins the location of
the preceding node in the list
4. The BACK pointer of first node and the FORW
pointer of the last node will contain NULL

More Related Content

What's hot (20)

Link list
Link listLink list
Link list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
List data structure
List data structure List data structure
List data structure
 
Linked list
Linked listLinked list
Linked list
 
Linked List
Linked ListLinked List
Linked List
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Linklist
LinklistLinklist
Linklist
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked list
Linked listLinked list
Linked list
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
single linked list
single linked listsingle linked list
single linked list
 
Doubly circular linked list
Doubly circular linked listDoubly circular linked list
Doubly circular linked list
 
Python lists
Python listsPython lists
Python lists
 
Link list
Link listLink list
Link list
 
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
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 

Similar to 5.Linked list

Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link listSukhdeep Kaur
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.pptAlliVinay1
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptssuser9dd05f
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptxmsohail37
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxskilljiolms
 

Similar to 5.Linked list (20)

Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
cp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.pptcp264_lecture13_14_linkedlist.ppt
cp264_lecture13_14_linkedlist.ppt
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 

More from Mandeep Singh

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & SearchingMandeep Singh
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data StructureMandeep Singh
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introductionMandeep Singh
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingMandeep Singh
 

More from Mandeep Singh (11)

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
7. Spanning trees
7. Spanning trees7. Spanning trees
7. Spanning trees
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Ip6 tables in linux
Ip6 tables in linuxIp6 tables in linux
Ip6 tables in linux
 
Iptables in linux
Iptables in linuxIptables in linux
Iptables in linux
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

5.Linked list

  • 2. 2 Need of Dynamic Data Structures • Linked Lists are dynamic data structures: They can grow or shrink during the execution of a program • Efficient memory utilization: Memory is allocated whenever it is required and deallocated when it is no longer needed • Insertion and Deletion are easier & efficient: It provides flexibility in inserting a data item at a specified position and deletion of a data item from the given position • Many complex applications can be easily carried out with linked list
  • 3. 3 Continuous Implementation of Lists • It can be done with help of continuous memory allocation data structure i.e. Arrays • For the purpose of implementing lists using arrays we are required to create a structure of two elements. 1. Info 2. Next  And then create the array (variable) of structure. e.g. struct node { int info,next;}; struct node n[100];
  • 4. 4 A linked list node • Each node contains a data item and a pointer to the next node • The last node has no next node • An external pointer gives access to the first node data next next first
  • 5. 5 Linked list notation . . . . . . p node(p) Predecessor node(p) node(p) Successor info(p) next(p)
  • 6. 6 Accessing nodes in a linked list • No direct access to individual nodes • First node accessed via external pointer • Other nodes accessed via pointer in previous node • It is a sequential access data structure first
  • 7. 7 Operation on Lists 1. Traversing 2. Searching 3. Inserting 4. Deleting
  • 8. 8 TRAVERSING A LINKED LIST 1. Set PTR = START 2. Repeat steps 3 and 4 while PTR!=NULL 3. Apply PROCESS to INFO[PTR] 4. Set PTR=LINK[PTR] 5. Exit
  • 9. 9 Types of Linked Lists 1. Singly linked list • Begins with a pointer to the first node • Terminates with a null pointer • Only traversed in one direction 2. Circular, singly linked • Pointer in the last node points back to the first node 3. Doubly linked list • Two “start pointers” – first element and last element • Each node has a forward pointer and a backward pointer • Allows traversals both forwards and backwards 4. Circular, doubly linked list • Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
  • 10. 10 Singly Linked List • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores – element – link to the next node next elem node A B C D 
  • 11. 11 Adding an item • Three steps: – find node after which to add – Allocate space for a node and store data in it – Link the new node into the existing linked list • Two cases for linking the node into the existing linked list – Adding an item at the beginning of the list – Adding an item after an existing node
  • 12. 12 Inserting at the Beginning NULLfirst 'A' 'B' p Initial List 1. Create a new node. 'C'p 2. Fill the new node with data. 'A' 'B' 3. Make the new node point to the first node. 4. Make first point to the new node . 'A' 'B' first 'C'p 'C'p NULL NULL first
  • 13. 13 Inserting at the Beginning (Algo.) INSERT(INFO,LINK,START,AVAIL,ITEM) 1. IF AVAIL=NULL then Write OVERFLOW and EXIT. 2. NEW=AVAIL and AVAIL=LINK[AVAIL] 3. INFO[NEW]=ITEM 4. LINK[NEW]=START 5. Set START=NEW 6. EXIT.
  • 14. 14 Inserting after a given NodeINSERTLOC(INFO,LINK,START,AVAIL,ITEM) 1. IF AVAIL=NULL then Write OVERFLOW and Exit 2. Set NEW=AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]=ITEM 4. If LOC=NULL then Set LINK[NEW]= START and START=NEW Else Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW 5. Exit.
  • 15. 15 Inserting into Sorted List FIND(INFO, LINK, START, ITEM, LOC) 1. If START =NULL then Set LOC=NULL and Return. 2. If ITEM<INFO[START] then Set LOC=NULL and Return 3. Set SAVE=START and PTR=LINK[START]. 4. Repeat Step 5 AND 6 While PTR!= NULL. 5. If ITEM<INFO[PTR] then Set LOC=SAVE and Return. 6. Set SAVE=PTR and PTR = LINK[PTR] 7. Set LOC= SAVE. 8. Return. INSERT(INFO,LINK,START,AVAIL,ITEM) 1. Call FIND(INFO, LINK, START. ITEM, LOC) 2. Call INSERTLOC(INFO,LINK,START,AVAIL,ITEM)
  • 16. 16 Deleting the Node Following a given Node DEL(INFO,LINK,START,AVAIL,LOC,LOCP) 1. IF LOCP=NULL, THEN, Set START=LINK[START] ELSE Set LINK[LOCP]=LINK[LOC] 2. Set LINK[LOC]=AVAIL and AVAIL=LOC 3. Exit. Here, LOCP is Predecessor of Location.
  • 17. 17 Header linked lists • Header linked list is a linked list which always contains a special node called the Header Node, at the beginning of the list. Header linked list Grounded Header linked list Circular Header linked list
  • 18. 18 • Grounded Header linked lists – The last node contains null pointer. A Head B C X
  • 19. 19 • Circular linked lists – The last node points to the first node of the list – How do we know when we have finished traversing the list? A Head B C
  • 20. 20 Comparison between Array & Dynamic Implementation of Linked List ARRAY LINKED LIST 1. Linear collection of data elements 1. Linear collection of nodes 2. Consecutive memory allocation 2. Not Consecutive memory allocation 3. Allow Sequential & Random Access of data 3. Allow only Sequential Access of data 4. Insertions / Deletions are not efficient 4. Insertions / Deletions are efficient with a constant time 5. Can’t add any number of elements at run time 5. Can add any number of elements in the list at run time 6. No extra cost of saving address of next element 6. Extra cost of saving address of next element in each node
  • 21. 21 Circular Lists • The last node contains pointer to the first node of the list • We can begin at any node and traverse the list until we reach the same node where we started A START B C
  • 22. 22 Inserting an element in the beginning of a Circular List 1. If AVAIL==NULL then Write Overflow and Return 2. Set NEW=AVAIL and AVAIL=LINK[AVAIL] 3. INFO[NEW]=ITEM 4. Set PTR=START 5. Repeat Step 6 while LINK[PTR]!=START 6. PTR=LINK[PTR] 7. Set LINK[NEW]=START 8. Set LINK[PTR]=NEW 9. Set START=NEW 10. Exit
  • 23. 23 Inserting an element at the end of a Circular List 1. If AVAIL==NULL then Write Overflow and Return 2. Set NEW=AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]=ITEM 4. Set LINK[NEW]=START 5. Set PTR=START 6. Repeat Step 7 while LINK[PTR]!=START 7. PTR=LINK[PTR] 8. Set LINK[PTR]=NEW 9. Exit
  • 24. 24 Deleting the First node from Circular List 1. If START==NULL then Write Underflow and Return 2. Set PTR=START 3. Repeat STEP 4 while LINK[PTR]!=START 4. Set PTR=LINK[PTR] 5. Set LINK[PTR]=LINK[START] 6. LINK[START]=AVAIL and AVAIL=START 7. START=LINK[PTR] 8. Exit
  • 25. 25 Deleting the Last node from Circular List 1. If START==NULL then Write Underflow and Return 2. Set PTR=START 3. Repeat STEP 4 while LINK[PTR]!=START 4. Set SAVE=PTR and PTR=LINK[PTR] 5. Set LINK[SAVE]=START 6. LINK[PTR]=AVAIL and AVAIL=PTR 7. Exit
  • 26. 26 Doubly Linked List • A Doubly linked list is a two way linked list, where each node N is divided into three parts: 1. An information field INFO which contains the data of N 2. A pointer FORW which contains the location of the next node in the list 3. A pointer field BACK which conatins the location of the preceding node in the list 4. The BACK pointer of first node and the FORW pointer of the last node will contain NULL