SlideShare a Scribd company logo
2
Most read
3
Most read
11
Most read
LINKED LISTS
Mrs.V. JAYAVANI
Assistant Professor
Department of Computer Science
E.M.G. Yadava Women’s College
INTRODUCTION
• A linked list is a linear collection of data elements called
nodes in which linear representation is given by links from
one node to the next node.
• Linked list is a data structure which in turn can be used to
implement other data structures. Thus, it acts as building
block to implement data structures like stacks, queues and
their variations.
• A linked list can be perceived as a train or a sequence of
nodes in which each node contains one or more data fields
and a pointer to the next node.
SIMPLE LINKED LIST
1 2 3 4 5 6 7 X
START
• In the above linked list, every node contains two parts
- one integer and the other a pointer to the next node.
• The left part of the node which contains data may include a
simple data type, an array or a structure.
• The right part of the node contains a pointer to the next
node (or address of the next node in sequence).
• The last node will have no next node connected to it, so
it will store a special value called NULL.
TRAVERSING LINKED LISTS
• We can traverse the entire linked list using a single
pointer variable called START.
• The START node contains the address of the first node; the
next part of the first node in turn stores the address
of its succeeding node.
• Using this technique the individual nodes of the list will
form a chain of nodes.
• If START = NULL, this means that the linked list is
empty and contains no nodes.
• In C, we can implement a linked list using the following
code: struct node
{
int data;
struct node *next;
};
START Pointer
1 DATA
NEXT
H 4
E 7
L 8
L 10
O -1
1
2
3
4
5
6
7
8
9
10
START
START pointing to
the first element of
the linked list in
memory
9
AVAIL
SINGLY LINKED LISTS
• A singly linked list is the simplest type of linked list in
which every node contains some data and a pointer to
the next node of the same data type.
1 2 3 4 5 6 7 X
START
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
SEARCHING A LINKED LIST
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
SEARCHING FOR VAL 4 IN LINKED LIST
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 4 2 6 5 X
3
PTR
1 7 3 4 2 6 5 X
PTR
INSERTING A NODE AT THE BEGINNING
1 7 3 4 2 6 5 X
START
START
9 1 7 3 4 2 6 5 X
ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT
INSERTING A NODE AT THE END
1 7 3 4 2 6 5 X
START, PTR
1 7 3 4 2 6 5 9 X
START
ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
INSERTING A NODE AFTER NODE THAT
HAS VALUE NUM
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
DELETING THE FIRST NODE
Algorithm to delete the first node from the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
Step 5: EXIT
1 7 3 4 2 6 5 X
7 3 4 2 6 5 X
START
START
DELETING THE LAST NODE
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
DELETING THE NODE AFTER A
GIVEN NODE
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR->NEXT
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
SINGLY LINKED LIST
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 5 X
PREPTR PTR
START
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X

More Related Content

PPT
UNIT-2_ LINKEDLIST.ppt for digital sysytem design
PPT
cp264_lecture13_14_linkedlist.ppt
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
PPTX
Ch-17-Data Structures.ppt.pptx
PPTX
DSModule2.pptx
PPT
ds 4Linked lists.ppt
PPT
Singly link list
PPTX
Implementation of linked list unit 2.pptx
UNIT-2_ LINKEDLIST.ppt for digital sysytem design
cp264_lecture13_14_linkedlist.ppt
358 33 powerpoint-slides_8-linked-lists_chapter-8
Ch-17-Data Structures.ppt.pptx
DSModule2.pptx
ds 4Linked lists.ppt
Singly link list
Implementation of linked list unit 2.pptx

Similar to Introduction Linked Lists - Singly Linked List, (20)

PPT
lecture four of data structures :Linked List-ds.ppt
PDF
DS Module 03.pdf
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPTX
VCE Unit 02 (1).pptx
PPTX
RPT_03_A_Linked List presentation for FE
PPTX
Implemention of Linked list concept in Data Structures
PPTX
data structures lists operation of lists
PPTX
Linked List
PDF
Document on Linked List as a presentation
PDF
Document on Linked List as a presentation
PDF
Bca data structures linked list mrs.sowmya jyothi
PDF
Chapter 3 Linkedlist Data Structure .pdf
PPT
Unit ii(dsc++)
PPTX
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
PPT
DS Unit 2.ppt
PPTX
Data Structures_Linked List
PPTX
Linked list
DOCX
Link list assi
PPT
linked list1.ppt linked list ppts and notes
PPT
Data Structures with C Linked List
lecture four of data structures :Linked List-ds.ppt
DS Module 03.pdf
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
VCE Unit 02 (1).pptx
RPT_03_A_Linked List presentation for FE
Implemention of Linked list concept in Data Structures
data structures lists operation of lists
Linked List
Document on Linked List as a presentation
Document on Linked List as a presentation
Bca data structures linked list mrs.sowmya jyothi
Chapter 3 Linkedlist Data Structure .pdf
Unit ii(dsc++)
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
DS Unit 2.ppt
Data Structures_Linked List
Linked list
Link list assi
linked list1.ppt linked list ppts and notes
Data Structures with C Linked List
Ad

More from JayaKamal (12)

PPT
Introduction to Machine Learning Concepts
PPTX
Trees, Basic Terminology and Binary Trees
PPT
To learn Basic Excel - Data Entry, Formulas and Functions
PPT
Introduction - Data Structures and Algorithms.ppt
PPTX
What is an Operating Systems?
PPTX
PPTX
Software Engineering
PPTX
Software Engineering
PPTX
Software Engineering
PPTX
Software Engineering
PPTX
Software Engineering
PPTX
Software Engineering
Introduction to Machine Learning Concepts
Trees, Basic Terminology and Binary Trees
To learn Basic Excel - Data Entry, Formulas and Functions
Introduction - Data Structures and Algorithms.ppt
What is an Operating Systems?
Software Engineering
Software Engineering
Software Engineering
Software Engineering
Software Engineering
Software Engineering
Ad

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Classroom Observation Tools for Teachers
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
01-Introduction-to-Information-Management.pdf
Institutional Correction lecture only . . .
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Classroom Observation Tools for Teachers
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
01-Introduction-to-Information-Management.pdf

Introduction Linked Lists - Singly Linked List,

  • 1. LINKED LISTS Mrs.V. JAYAVANI Assistant Professor Department of Computer Science E.M.G. Yadava Women’s College
  • 2. INTRODUCTION • A linked list is a linear collection of data elements called nodes in which linear representation is given by links from one node to the next node. • Linked list is a data structure which in turn can be used to implement other data structures. Thus, it acts as building block to implement data structures like stacks, queues and their variations. • A linked list can be perceived as a train or a sequence of nodes in which each node contains one or more data fields and a pointer to the next node.
  • 3. SIMPLE LINKED LIST 1 2 3 4 5 6 7 X START • In the above linked list, every node contains two parts - one integer and the other a pointer to the next node. • The left part of the node which contains data may include a simple data type, an array or a structure. • The right part of the node contains a pointer to the next node (or address of the next node in sequence). • The last node will have no next node connected to it, so it will store a special value called NULL.
  • 4. TRAVERSING LINKED LISTS • We can traverse the entire linked list using a single pointer variable called START. • The START node contains the address of the first node; the next part of the first node in turn stores the address of its succeeding node. • Using this technique the individual nodes of the list will form a chain of nodes. • If START = NULL, this means that the linked list is empty and contains no nodes. • In C, we can implement a linked list using the following code: struct node { int data; struct node *next; };
  • 5. START Pointer 1 DATA NEXT H 4 E 7 L 8 L 10 O -1 1 2 3 4 5 6 7 8 9 10 START START pointing to the first element of the linked list in memory 9 AVAIL
  • 6. SINGLY LINKED LISTS • A singly linked list is the simplest type of linked list in which every node contains some data and a pointer to the next node of the same data type. 1 2 3 4 5 6 7 X START ALGORITHM FOR TRAVERSING A LINKED LIST Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Steps 3 and 4 while PTR != NULL Step 3: Apply Process to PTR->DATA Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: EXIT
  • 7. SEARCHING A LINKED LIST ALGORITHM TO SEARCH A LINKED LIST Step 1: [INITIALIZE] SET PTR = START Step 2: Repeat Step 3 while PTR != NULL Step 3: IF VAL = PTR->DATA SET POS = PTR Go To Step 5 ELSE SET PTR = PTR->NEXT [END OF IF] [END OF LOOP] Step 4: SET POS = NULL Step 5: EXIT
  • 8. SEARCHING FOR VAL 4 IN LINKED LIST 1 7 3 4 2 6 5 X PTR 1 7 3 4 2 6 5 X PTR 1 7 4 2 6 5 X 3 PTR 1 7 3 4 2 6 5 X PTR
  • 9. INSERTING A NODE AT THE BEGINNING 1 7 3 4 2 6 5 X START START 9 1 7 3 4 2 6 5 X ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 7 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = START Step 6: SET START = New_Node Step 7: EXIT
  • 10. INSERTING A NODE AT THE END 1 7 3 4 2 6 5 X START, PTR 1 7 3 4 2 6 5 9 X START ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 10 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET New_Node->Next = NULL Step 6: SET PTR = START Step 7: Repeat Step 8 while PTR->NEXT != NULL Step 8: SET PTR = PTR ->NEXT [END OF LOOP] Step 9: SET PTR->NEXT = New_Node Step 10: EXIT
  • 11. INSERTING A NODE AFTER NODE THAT HAS VALUE NUM ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM Step 1: IF AVAIL = NULL, then Write OVERFLOW Go to Step 12 [END OF IF] Step 2: SET New_Node = AVAIL Step 3: SET AVAIL = AVAIL->NEXT Step 4: SET New_Node->DATA = VAL Step 5: SET PTR = START Step 6: SET PREPTR = PTR Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM Step 8: SET PREPTR = PTR Step 9: SET PTR = PTR->NEXT [END OF LOOP] Step 10: PREPTR->NEXT = New_Node Step 11: SET New_Node->NEXT = PTR Step 12: EXIT
  • 12. DELETING THE FIRST NODE Algorithm to delete the first node from the linked list Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 5 [END OF IF] Step 2: SET PTR = START Step 3: SET START = START->NEXT Step 4: FREE PTR Step 5: EXIT 1 7 3 4 2 6 5 X 7 3 4 2 6 5 X START START
  • 13. DELETING THE LAST NODE ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR->NEXT [END OF LOOP] Step 6: SET PREPTR->NEXT = NULL Step 7: FREE PTR Step 8: EXIT 1 7 3 4 2 6 5 X START, PREPTR, PTR 1 7 3 4 2 6 X 5 X PREPTR PTR START
  • 14. DELETING THE NODE AFTER A GIVEN NODE ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST Step 1: IF START = NULL, then Write UNDERFLOW Go to Step 10 [END OF IF] Step 2: SET PTR = START Step 3: SET PREPTR = PTR Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM Step 5: SET PREPTR = PTR Step 6: SET PTR = PTR->NEXT [END OF LOOP] Step7: SET TEMP = PTR->NEXT Step 8: SET PREPTR->NEXT = TEMP->NEXT Step 9: FREE TEMP Step 10: EXIT
  • 15. SINGLY LINKED LIST 1 7 3 4 2 6 5 X START, PREPTR, PTR 1 7 3 4 2 6 5 X PREPTR PTR START 1 7 3 4 2 6 5 X START 1 7 3 4 6 5 X