SlideShare a Scribd company logo
Chapter 2.
Linked List
(08 periods)
Dayanand Science College Department Of Computer Science 1
Content:
1. Introduction to Linked list,
2. Representation of linked list in memory,
3. Traversing,
4. Searching in Unsorted linked list,
5. Overflow and Underflow,
6. Inserting at the beginning of a list,
7. deleting node following a given Node.
Dayanand Science College Department Of Computer Science 2
Introduction to Linked list
• Like arrays, Linked List is a linear data structure.
• Unlike arrays, linked list elements are not stored at a contiguous location;
• the elements are linked using pointers.
101 110 114 117
110 114 117
Dayanand Science College Department Of Computer Science 3
Why Linked List?
Arrays can be used to store linear data of similar types, but
arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has to
be created for the new elements and to create room existing elements have to be shifted.
Dayanand Science College Department Of Computer Science 4
For example
• in a system, if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
• And if we want to insert a new ID 1005, then to maintain the sorted order,
we have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques
are used. For example, to delete 1010 in id[], everything after 1010 has to be
moved.
Dayanand Science College Department Of Computer Science 5
Representation:
• A linked list is represented by a pointer to the first node of the linked list.
The first node is called the head. If the linked list is empty, then the value of
the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures. Below is an example of a
linked list node with integer data.
Dayanand Science College Department Of Computer Science 6
Representation:
• Let LIST be a linked list. Then LIST will be maintained in memory specified
as follows. First of all, LIST requires two linear arrays we will call them here
INFO and LINK- such that INFO[K] and LINK[K] contain, respectively,
the information part and the nextpointer field of a node LIST. As noted above
LIST also requires a variable name- such as START. START contains the
location of the beginning of the list, and a nextpointer sentinel -denoted by
NULL- which indicate the end of the list. Since the subscripts of the array
INFO and LINK are usually positive, we will choose NULL=0.
Dayanand Science College Department Of Computer Science 7
• The following example of linked list indicate that the nodes of a list need
not occupy adjacent elements in the array INFO and LINK, and that more
than one list may be maintained in the same linear array INFO and LINK.
However, each list must have its own pointer variable giving the location of
its first node.
Dayanand Science College Department Of Computer Science 8
Dayanand Science College Department Of Computer Science 9
Representation
• Above picture is of linked list in memory where each node of the list contains a single character. We can obtain the actual list of characters as
follows.
• Algorithm
• START= 9, so INFO[9]=N is the first character.
• LINK[9]=3, so INFO[3]=O is the second character.
• LINK[3]=6, so INFO[6]= (Blank) is the third character.
• LINK[6]=11, so INFO[11]=E is the fourth character.
• LINK[11]=7, so INFO[7]=X is the fifth character.
• LINK[7]=10, so INFO[10]=I is the sixth character.
• LINK[10]=4, so INFO[4]=T is the seventh character.
• LINK[4]=0, the NULL value, so the List has ended.
• In other words, NO EXIT is the character string.
Dayanand Science College Department Of Computer Science 10
Types of Linked List
• Following are the various types of linked list.
• Simple Linked List − Item navigation is forward only.
• Doubly Linked List − Items can be navigated forward and backward.
• Circular Linked List − Last item contains link of the first element as next
and the first element has a link to the last element as previous.
Dayanand Science College Department Of Computer Science 11
Simple Linked List
101 110 114 117
110 114 117
Dayanand Science College Department Of Computer Science 12
Doubly Linked List
A 15 11 B 17 15 C 19 17 D
11 15 17 19
START
Dayanand Science College Department Of Computer Science 13
Circular Linked List (Simple)
101 110 114 117
110 114 117 101
Dayanand Science College Department Of Computer Science 14
Circular Linked List (Double)
A 15 11 B 17 15 C 19 17 D 11
11 15 17 19
START
Dayanand Science College Department Of Computer Science 15
2.3 Traversing
A linked list is a linear data structure that needs to be traversed starting from the head node until the
end of the list. Unlike arrays, where random access is possible, linked list requires access to its nodes
through sequential traversal. Traversing a linked list is important in many applications. For example,
we may want to print a list or search for a specific node in the list. Or we may want to perform an
advanced operation on the list as we traverse the list. The algorithm for traversing a list is fairly
trivial.
a. Start with the head of the list. Access the content of the head node if it is not null.
b. Then go to the next node(if exists) and access the node information
c. Continue until no more nodes (that is, you have reached the last node)
Let LIST be a linked list in memory stored in linear array INFO and LINK with START pointing to
the first element and NULL indicating the end of LIST. Suppose we want to traverse LIST in order
to Process each node exactly once. This section presents an algorithm that does so and then uses the
algorithm in some applications.
Dayanand Science College Department Of Computer Science 16
101
101 112 120 125
A B C D
112 120 125
Dayanand Science College Department Of Computer Science 17
Algorithm: 1. Set PTR:=START.[Initializes pointer PTR]
2. Repeat Step 3 and 4 while PTR≠ NULL.
3. Apply PROCESS to INFO[PTR].
4. Set PTR:= LINK[PTR]. [PTR now points to the next node.]
[End of Step 2 loop.]
5. Exit.
Dayanand Science College Department Of Computer Science 18
Algorithm details
Initialize PTR or START.
Then process INFO[PTR], the information at the first node.
Update PTR by the assignment PTR:=LINK[PTR], so that
PTR points to the second node.
Then Process INFO[PTR], the information at the second node.
Again update PTR by the assignment operator
PTR:=LINK[PTR], and then process INFO[PTR], the
information at the third node.
And so on. Continue until PTR=NULL, which signals the end
of the list.
Dayanand Science College Department Of Computer Science 19
Traversing Linear Array
• LA- Array, Lower Bound= LB, Upper Bound=UB, S-> Process
each element
• Algorithm: 1) Initialize Counter I=LB
2) Loop (While I<=UB)
3) Apply S to LA[I]
4) I=I+1
5) End Loop
6) Exit
1
2
3
4
LB
UB
5
1
2
3
4
5
I=LB LA[i] *2 I=I+1
1 1*2=2 I=1+1=2
2 2*2=4 I=2+1=3
3 3*2=6 I=3+1=4
4 4*2=8 I=4+1=5
5 5*2=10 I=5+1=6
Dayanand Science College Department Of Computer Science 20
#include <stdio.h>
#include<conio.h>
void main()
{ int LA[] = {2,4,6,8,9};
int i, n = 5;
printf("The array elements are:n");
for(i = 0; i < n; i++)
{
printf("LA[%d] = %d n", i, LA[i]);
}
getch();
}
Command Prompt
The array elements
are:
LA[0] = 2
LA[1] = 4
LA[2] = 6
LA[3] = 8
LA[4] = 9
Dayanand Science College Department Of Computer Science 21
2.4 Searching Unsorted linked list
• Let LIST be a linked list in memory which is not sorted, then one searches for
ITEM in LIST by traversing through the list using a pointer variable PTR and
comparing ITEM with the contents INFO[PTR] of each node, one by one, of
LIST. Before we update the pointer PTR by PTR:=LINK[PTR]
• We require two tests. First we have to check to see whether we have reached the
end of list i.e.
• PTR= NULL
• Then we check to see whether
• INFO[PTR]=ITEM
Dayanand Science College Department Of Computer Science 22
Algorithm:
SEARCH (INFO, LINK, START, ITEM, LOC)
LIST is a linked list in memory. This algorithm finds the location LOC of the node where
ITEM first appear in LIST, or set LOC=NULL.
set PTR:=START.
Repeat step 3 while PTR ≠ NULL:
If ITEM =INFO[PTR], then:
Set LOC:=PTR, and Exit.
Else:
Set PTR:=LINK[PTR].[PTR now points to the next node.]
[End of IF structure]
[End of step 2 loop]
[Search is unsuccessful.] set LOC:=NULL.
Exit.
Dayanand Science College Department Of Computer Science 23
• Want to Search For Shree then
101 110 114 117
110 114 117
Amar RAJ Neha Shree
HEAD
Dayanand Science College Department Of Computer Science 24
Overflow
• Sometimes new data are to be inserted into a data structure but
there is no available space, i.e. the free-storage list is empty. This
situation is usually called overflow.
• The programmer may handle overflow by printing the message
OVERFLOW.
• In such a case, the programmer may then modify the program by
adding space to the underlying arrays.
• Observe that overflow will occur with our linked lists when
AVAIL=NULL and there is an insertion.
Dayanand Science College Department Of Computer Science 25
UNDERFLOW
• The term underflow refers to the situation where one
wants to delete data from a data structure that is empty.
• The programmer may handle underflow by printing the
message UNDERFLOW.
• Observe that underflow will occur with our linked when
START = NULL and there is a deletion.
Dayanand Science College Department Of Computer Science 26
Inserting at the beginning of a list,
Dayanand Science College Department Of Computer Science 27
101
105
203
203
105
203 105
Dayanand Science College Department Of Computer Science 28
Deleting Node following a given Node.
203
105
105
Dayanand Science College Department Of Computer Science 29
Dayanand Science College Department Of Computer Science 30
Question Bank
1. What is Linked list? Explain it with suitable example.
2. Explain the representation of linked list in memory.
3. Explain Traversing of linked list with suitable example.
4. Explain searching in Unsorted linked list.
5. Write a short note on overflow and underflow.
6. Write a procedure for inserting element in a linked list
7. Explain deleting node in a linked list.
Dayanand Science College Department Of Computer Science 31

More Related Content

Similar to unit 2- PPT.pdf

Linked List
Linked ListLinked List
Linked List
BHARATH KUMAR
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
KamranAli649587
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
rajveersingh643731
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
NirmalPandey23
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
nikhilcse1
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
Rajendran
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
Dreamers6
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
Kumar
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptx
jotaro11
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
Jazz Jinia Bhowmik
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
Akila Krishnamoorthy
 
Data Structure
Data StructureData Structure
Data Structure
HarshGupta663
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
Chandan Singh
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
Aakash deep Singhal
 

Similar to unit 2- PPT.pdf (20)

Linked List
Linked ListLinked List
Linked List
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
Dounly linked list
Dounly linked listDounly linked list
Dounly 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
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptx
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Data Structure
Data StructureData Structure
Data Structure
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 

Recently uploaded

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 

Recently uploaded (20)

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 

unit 2- PPT.pdf

  • 1. Chapter 2. Linked List (08 periods) Dayanand Science College Department Of Computer Science 1
  • 2. Content: 1. Introduction to Linked list, 2. Representation of linked list in memory, 3. Traversing, 4. Searching in Unsorted linked list, 5. Overflow and Underflow, 6. Inserting at the beginning of a list, 7. deleting node following a given Node. Dayanand Science College Department Of Computer Science 2
  • 3. Introduction to Linked list • Like arrays, Linked List is a linear data structure. • Unlike arrays, linked list elements are not stored at a contiguous location; • the elements are linked using pointers. 101 110 114 117 110 114 117 Dayanand Science College Department Of Computer Science 3
  • 4. Why Linked List? Arrays can be used to store linear data of similar types, but arrays have the following limitations. 1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create room existing elements have to be shifted. Dayanand Science College Department Of Computer Science 4
  • 5. For example • in a system, if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040]. • And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. Dayanand Science College Department Of Computer Science 5
  • 6. Representation: • A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node In C, we can represent a node using structures. Below is an example of a linked list node with integer data. Dayanand Science College Department Of Computer Science 6
  • 7. Representation: • Let LIST be a linked list. Then LIST will be maintained in memory specified as follows. First of all, LIST requires two linear arrays we will call them here INFO and LINK- such that INFO[K] and LINK[K] contain, respectively, the information part and the nextpointer field of a node LIST. As noted above LIST also requires a variable name- such as START. START contains the location of the beginning of the list, and a nextpointer sentinel -denoted by NULL- which indicate the end of the list. Since the subscripts of the array INFO and LINK are usually positive, we will choose NULL=0. Dayanand Science College Department Of Computer Science 7
  • 8. • The following example of linked list indicate that the nodes of a list need not occupy adjacent elements in the array INFO and LINK, and that more than one list may be maintained in the same linear array INFO and LINK. However, each list must have its own pointer variable giving the location of its first node. Dayanand Science College Department Of Computer Science 8
  • 9. Dayanand Science College Department Of Computer Science 9
  • 10. Representation • Above picture is of linked list in memory where each node of the list contains a single character. We can obtain the actual list of characters as follows. • Algorithm • START= 9, so INFO[9]=N is the first character. • LINK[9]=3, so INFO[3]=O is the second character. • LINK[3]=6, so INFO[6]= (Blank) is the third character. • LINK[6]=11, so INFO[11]=E is the fourth character. • LINK[11]=7, so INFO[7]=X is the fifth character. • LINK[7]=10, so INFO[10]=I is the sixth character. • LINK[10]=4, so INFO[4]=T is the seventh character. • LINK[4]=0, the NULL value, so the List has ended. • In other words, NO EXIT is the character string. Dayanand Science College Department Of Computer Science 10
  • 11. Types of Linked List • Following are the various types of linked list. • Simple Linked List − Item navigation is forward only. • Doubly Linked List − Items can be navigated forward and backward. • Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Dayanand Science College Department Of Computer Science 11
  • 12. Simple Linked List 101 110 114 117 110 114 117 Dayanand Science College Department Of Computer Science 12
  • 13. Doubly Linked List A 15 11 B 17 15 C 19 17 D 11 15 17 19 START Dayanand Science College Department Of Computer Science 13
  • 14. Circular Linked List (Simple) 101 110 114 117 110 114 117 101 Dayanand Science College Department Of Computer Science 14
  • 15. Circular Linked List (Double) A 15 11 B 17 15 C 19 17 D 11 11 15 17 19 START Dayanand Science College Department Of Computer Science 15
  • 16. 2.3 Traversing A linked list is a linear data structure that needs to be traversed starting from the head node until the end of the list. Unlike arrays, where random access is possible, linked list requires access to its nodes through sequential traversal. Traversing a linked list is important in many applications. For example, we may want to print a list or search for a specific node in the list. Or we may want to perform an advanced operation on the list as we traverse the list. The algorithm for traversing a list is fairly trivial. a. Start with the head of the list. Access the content of the head node if it is not null. b. Then go to the next node(if exists) and access the node information c. Continue until no more nodes (that is, you have reached the last node) Let LIST be a linked list in memory stored in linear array INFO and LINK with START pointing to the first element and NULL indicating the end of LIST. Suppose we want to traverse LIST in order to Process each node exactly once. This section presents an algorithm that does so and then uses the algorithm in some applications. Dayanand Science College Department Of Computer Science 16
  • 17. 101 101 112 120 125 A B C D 112 120 125 Dayanand Science College Department Of Computer Science 17
  • 18. Algorithm: 1. Set PTR:=START.[Initializes pointer PTR] 2. Repeat Step 3 and 4 while PTR≠ NULL. 3. Apply PROCESS to INFO[PTR]. 4. Set PTR:= LINK[PTR]. [PTR now points to the next node.] [End of Step 2 loop.] 5. Exit. Dayanand Science College Department Of Computer Science 18
  • 19. Algorithm details Initialize PTR or START. Then process INFO[PTR], the information at the first node. Update PTR by the assignment PTR:=LINK[PTR], so that PTR points to the second node. Then Process INFO[PTR], the information at the second node. Again update PTR by the assignment operator PTR:=LINK[PTR], and then process INFO[PTR], the information at the third node. And so on. Continue until PTR=NULL, which signals the end of the list. Dayanand Science College Department Of Computer Science 19
  • 20. Traversing Linear Array • LA- Array, Lower Bound= LB, Upper Bound=UB, S-> Process each element • Algorithm: 1) Initialize Counter I=LB 2) Loop (While I<=UB) 3) Apply S to LA[I] 4) I=I+1 5) End Loop 6) Exit 1 2 3 4 LB UB 5 1 2 3 4 5 I=LB LA[i] *2 I=I+1 1 1*2=2 I=1+1=2 2 2*2=4 I=2+1=3 3 3*2=6 I=3+1=4 4 4*2=8 I=4+1=5 5 5*2=10 I=5+1=6 Dayanand Science College Department Of Computer Science 20
  • 21. #include <stdio.h> #include<conio.h> void main() { int LA[] = {2,4,6,8,9}; int i, n = 5; printf("The array elements are:n"); for(i = 0; i < n; i++) { printf("LA[%d] = %d n", i, LA[i]); } getch(); } Command Prompt The array elements are: LA[0] = 2 LA[1] = 4 LA[2] = 6 LA[3] = 8 LA[4] = 9 Dayanand Science College Department Of Computer Science 21
  • 22. 2.4 Searching Unsorted linked list • Let LIST be a linked list in memory which is not sorted, then one searches for ITEM in LIST by traversing through the list using a pointer variable PTR and comparing ITEM with the contents INFO[PTR] of each node, one by one, of LIST. Before we update the pointer PTR by PTR:=LINK[PTR] • We require two tests. First we have to check to see whether we have reached the end of list i.e. • PTR= NULL • Then we check to see whether • INFO[PTR]=ITEM Dayanand Science College Department Of Computer Science 22
  • 23. Algorithm: SEARCH (INFO, LINK, START, ITEM, LOC) LIST is a linked list in memory. This algorithm finds the location LOC of the node where ITEM first appear in LIST, or set LOC=NULL. set PTR:=START. Repeat step 3 while PTR ≠ NULL: If ITEM =INFO[PTR], then: Set LOC:=PTR, and Exit. Else: Set PTR:=LINK[PTR].[PTR now points to the next node.] [End of IF structure] [End of step 2 loop] [Search is unsuccessful.] set LOC:=NULL. Exit. Dayanand Science College Department Of Computer Science 23
  • 24. • Want to Search For Shree then 101 110 114 117 110 114 117 Amar RAJ Neha Shree HEAD Dayanand Science College Department Of Computer Science 24
  • 25. Overflow • Sometimes new data are to be inserted into a data structure but there is no available space, i.e. the free-storage list is empty. This situation is usually called overflow. • The programmer may handle overflow by printing the message OVERFLOW. • In such a case, the programmer may then modify the program by adding space to the underlying arrays. • Observe that overflow will occur with our linked lists when AVAIL=NULL and there is an insertion. Dayanand Science College Department Of Computer Science 25
  • 26. UNDERFLOW • The term underflow refers to the situation where one wants to delete data from a data structure that is empty. • The programmer may handle underflow by printing the message UNDERFLOW. • Observe that underflow will occur with our linked when START = NULL and there is a deletion. Dayanand Science College Department Of Computer Science 26
  • 27. Inserting at the beginning of a list, Dayanand Science College Department Of Computer Science 27
  • 28. 101 105 203 203 105 203 105 Dayanand Science College Department Of Computer Science 28
  • 29. Deleting Node following a given Node. 203 105 105 Dayanand Science College Department Of Computer Science 29
  • 30. Dayanand Science College Department Of Computer Science 30
  • 31. Question Bank 1. What is Linked list? Explain it with suitable example. 2. Explain the representation of linked list in memory. 3. Explain Traversing of linked list with suitable example. 4. Explain searching in Unsorted linked list. 5. Write a short note on overflow and underflow. 6. Write a procedure for inserting element in a linked list 7. Explain deleting node in a linked list. Dayanand Science College Department Of Computer Science 31