SlideShare a Scribd company logo
1 of 75
Download to read offline

Linked List
Data Structures & Algorithms
Syed Mudassar Alam
Linked Lists
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
A linked list is a series of connected nodes (or links)
where each node is a data structure.
Dynamically allocated data structures can be linked
together to form a chain.
A linked list can grow or shrink in size as the program
runs.This is possible because the nodes in a linked list are
dynamically allocated.
Defination:
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Lists and arrays
Lists:
Lists and arrays
Arrays:
{Size of the following array is = 4}
Index 0 1 2 3
Value 44 5 96 3
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Nodes and pointers
 Each nodes contains the Data
Field and a reference field (Pointers)
that points to next node.
 The last nodes contains the null value in its second
part because there is no node after it.
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Single linked lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Double Linked Lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Circular Lists
Introduction
 Definitions
 Lists and arrays
 Nodes and pointers
 Single Linked Lists
 Double Linked Lists
 Circular Lists
 Advantages
Advantages
 Linked lists are more complex to code and manage
than arrays, but they have some distinct advantages.
a)A linked list can easily grow and shrink in size -
◦ The programmer doesn’t need to know how
many nodes will be in the list. They are created in
memory as needed.
b)Speed of insertion or deletion from the list -
◦ Inserting and deleting elements into and out of
arrays requires moving elements of the array. When
a node is inserted, or deleted from a linked list,
none of the other nodes have to be moved.
Basic Operations of a Linked List
 As an abstract data type,
A list is a finite sequence (possibly empty) of elements with basic
operations that vary from one application to another.
Basic operations commonly include:
 Construction: Allocate and initialize a list object (usually empty)
 Empty: Check if list is empty
Basic Operations of a Linked
List
 Traverse: Visit each element of the list in order stored
 Insert: Add an item to the list (at any point)
 Delete: Remove an item from the list (at any point)
Basic Operations of a Linked List
 Construction: first = null_value;
 Empty: return (first == null_value)
 Traverse:
ptr = first;
while (ptr != null_value)
{ Process data part of node pointed to by ptr;
ptr = address of next element
// contained in next part of
// node pointed to by ptr;
}
Basic Operations of a Linked
List
 Insert & Delete: Both operations necessitate the
changing of links.
The address of the predecessor of the node to be
added or removed needs to be known.
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle (any) of the list
Insertion at the top
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
Insertion Description
 Follow the previous steps and we get
48 17 142
head //
head 93
Step 1 Step 2
Step 3
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion at the end
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
Insertion Description
 Follow the previous steps and we get
48 17 142
head //
Step 1 Step 2
Step 3
Insertion Description
 Insertion at the top of the list
 Insertion at the end of the list
 Insertion in the middle of the list
Insertion in the middle
Steps:
 Create a Node
 Set the node data Values
 Break pointer connection
 Re-connect the pointers
Insertion Description
Step 1 Step 2
Step 3
Step 4
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the top
Steps
 Break the pointer connection
 Re-connect the nodes
 Delete the node
Deletion Description
4 17
head
42
6
4 17
head
42
6
4 17
head
42
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the end
Steps
 Break the pointer connection
 Set previous node pointer to NULL
 Delete the node
Deletion Description
4 17
head
42
6
4 17
head
42
6
4 17
6
head
Deletion Description
 Deleting from the top of the list
 Deleting from the end of the list
 Deleting from the middle of the list
Deleting from the Middle
Steps
 Set previous Node pointer to next node
 Break Node pointer connection
 Delete the node
Deletion Description
4 17 42
head
4 17
head
42
4
head
42
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Basic Node Implementation
The following code is written in C++:
Struct Node
{
int data; //any type of data could be another struct
Node *next; //this is an important piece of code
“pointer”
};
Outline
 Introduction
 Insertion Description
 Deletion Description
 Basic Node Implementation
 Conclusion
Adding a Node
There are four steps to add a node to a linked list:
 Allocate memory for the new node.
 Determine the insertion point (you need to know only the
new node’s predecessor (pPre)
 Point the new node to its successor.
 Point the predecessor to the new node.
Pointer to the predecessor (pPre) can be in one of two
states:
 it can contain the address of a node (i.e. you are
adding somewhere after the first node – in the
middle or at the end)
 it can be NULL (i.e. you are adding either to an
empty list or at the beginning of the list)
Adding Nodes to an Empty Linked List
pNew -> next = pHead; // set link to NULL
pHead = pNew;// point list to first node
Before: After:
Page 51
51
39
pNew
pHead
pPre
39
pNew
pHead
pPre
Adding a Node to the Beginning of a
Linked List
39
pNew
pHead
pPre
75 124
39
pNew
pHead
pPre
75 124
pNew -> next = pHead;
pHead = pNew;// point list to first node
Adding a Node to the End of a Linked List
Before:
pNew -> next = NULL;
pPre ->next = pNew;
After:
144
pNew
pPre
55 24
144
pNew
pPre
55 24
Adding a Node to the Middle of a Linked List
64
pNew
pPre
55 124
Before:
pNew -> next = pPre -> next;
pPre -> next = pNew;
After: 64
pNew
55 124
pPre
Deleting a Node from a
Linked List
 Deleting a node requires that we logically remove the
node from the list by changing various links and then
physically deleting the node from the list (i.e., return it
to the heap).
 Any node in the list can be deleted. Note that if the
only node in the list is to be deleted, an empty list will
result. In this case the head pointer will be set to NULL.
 To logically delete a node:
 First locate the node itself (pCur) and its logical
predecessor (pPre).
 Change the predecessor’s link field to point to the
deleted node’s successor (located at pCur ->
next).
 Recycle the node
Deleting the First Node from a Linked List
Before:
pHead = pCur -> next;
delete(pCur);
After:
Page 57
pHead
pPre
75 124
pCur
pHead
pPre
Recycle
d
124
pCur
Deleting a Node from a Linked List
– General Case
 Before:
 After:
75 124
96
pPre pCur
75 124
Recycle
d
pPre pCur
Code:
pPre -> next = pCur -> next;
delete(pCur);
Doubly Link List
 It contains the data value
it contains two pointer
 Next, Prev
Insert at Rear End (Last)
Insert at Rear
Insert at rear
newnode->prev= tail
tail->next=newnode
tail=Newnode
List is Empty
Newnode->prev=tail //(if tail=null)
head=newnode
tail=newnode
Newnode->next=null
Insert at First
Newnode->next=head
head->prev=newnode
head=newnode
Insert at middle (some point)
 newnode->prev=current
 newnode->next=current->next
 current->next->prev=newnode
 current->next=newnode
Delete at rear end (last)
lNode* delNode=tail
tail=delNode->prev
tail->next=null
delete delnode
Delete only One node
 lNode* delNode=tail
 tail=delNode->prev
 head=null // (tail==null)
 delete delNode
Delete from Front
 lNode* delNode=head
 head= delnode->next
 head->prev=null
 delete delnode
Delete From Middle (Any)
 curr->prev->next=curr->next
 curr->next-prev=curr->prev
 delete curr
Reading Material CS
 Schaum’s Outlines: Chapter # 5
 D. S. Malik: Chapter # 5
 Mark A. Weiss: Chapter # 3
 Chapter 6, ADT, Data Structure and Problem solving
with C++ by Larry Nyhoff.
 Chapter 5, Nell Dale 3rd Edition
 Chapter 8, C++ an introduction to Data Structures by
Larry Nyhoff

More Related Content

Similar to Linked Lists.pdf

ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.pptAlliVinay1
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)Chandan Singh
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked ListThenmozhiK5
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfAxmedcarb
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptxnikhilcse1
 

Similar to Linked Lists.pdf (20)

Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
4.linked list(contd.)
4.linked list(contd.)4.linked list(contd.)
4.linked list(contd.)
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdf
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
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
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
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
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

Linked Lists.pdf