SlideShare a Scribd company logo
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Linked Lists
5 7 3 1 NULL
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Linked List
A linked list is a linear collection of data elements (called nodes),
where the linear order is given by means of pointers.
Each node is divided into 2 parts:
 1st
part contains the information of the element.
 2nd
part is called the link field or next pointer field which
contains the address of the next node in the list.
struct node {
int value;
struct node *next;
}
5 7 3 1
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Basic Operations
Insert: Add a new node in the first, last or interior of
the list.
Delete: Delete a node from the first, last or interior of
the list.
Search: Search a node containing particular value in
the linked list.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insertion to a Linear Linked list
Add a new node at the first, last or interior of a linked list.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert First
To add a new node to the head of the linear linked list, we need to
construct a new node that is pointed by pointer newitem.
Assume there is a global variable head which points to the first
node in the list.
The new node points to the first node in the list. The head is then
set to point to the new node.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert First (Cont.)
Step 1. Create a new node that is pointed by pointer newItem.
Step 2. Link the new node to the first node of the linked list.
Step 3. Set the pointer head to the new node.
NULL
HEADnewItem
NULL
HEADnewItem
NULL
HEAD
newItem
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert First (Cont.)
struct node{
int value;
struct node *next;
};
struct node *head;
void insertHead(){
//create a new node
struct node *newItem = new node();
newItem->value = 10;
newItem->next = NULL;
//insert the new node at the head
newItem->next = head;
head = newItem;
}
NULL
HEADnewItem
NULL
HEADnewItem
NULL
HEAD
newItem
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert Last
To add a new node to the tail of the linear linked list, we
need to construct a new node and set it's link field to
"null".
Assume the list is not empty, locate the last node and
change it's link field to point to the new node.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert Last (Cont.)
Step1. Create the new node.
Step2. Set a temporary pointer prev to point to the last node.
Step3. Set prev to point to the new node and new node as last node.
NULL
HEAD newItem
NULL
HEAD prev
newItem
HEAD prev
newItem
NULL
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert Last (Cont.)
struct node{
int value;
struct node *next;
};
struct node *head;
void insertTail(){
//create a new node to be inserted
struct node *newItem = new node();
newItem->value = 10;
newItem->next = NULL;
// set prev to point to the last node of the list
struct node *prev = head;
while (prev->next != NULL){
prev = prev->next;
}
newItem->next = prev->next;
prev->next = newItem;
}
NULL
HEAD
NULL
HEAD prev
newItemHEAD prev
NULL
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert Middle (after a desired node)
struct node{
int value;
struct node *next;
};
struct node *head;
void insertMiddle(int num){
//create a new node to be inserted
struct node *newItem = new node();
newItem->value = 10;
newItem->next = NULL;
// set prev to point to the desired node of the list
struct node *prev = head;
while (prev->value != num){
prev = prev->next;
}
newItem->next = prev->next;
prev->next = newItem;
}
HEAD
HEAD prev
newItemHEAD prev
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Printing List
void printList()
{
if (head == NULL) // no list at all
return;
struct node *cur = head;
while (cur != NULL)
{
printf("%d t", cur->value);
cur = cur->next;
}
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Deletion from a Linear Linked list
Deletion can be done
At the first node of linked list.
At the end of a linked list.
Within the linked list.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete First
To delete the first node of the linked list, we not only want to
advance the pointer head to the second node, but we also want
to release the memory occupied by the abandoned node.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete First (Cont.)
Step1. Initialize the pointer cur point to the first node of the list.
Step2. Move the pointer head to the second node of the list.
Step3. Remove the node that is pointed by the pointer cur.
Step 1
Step 2 Step 3
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete First (Cont.)
void deleteHead()
{
struct node *cur;
if (head = = NULL) //list empty
return;
cur = head; // save head pointer
head = head->next; //advance head
delete cur;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Last
To delete the last node in a linked list, we use a local variable,
cur, to point to the last node. We also use another variable,
prev, to point to the second last node in the linked list.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Last (Cont.)
Step1. Initialize pointer cur to point to the first node of the list, while the
pointer prev has a value of null.
Step2. Traverse the entire list until the pointer cur points to the last node of
the list.
Step3. Set NULL to next field of the node pointed by the pointer prev.
Step4. Remove the last node that is pointed by the pointer cur.
Step1
Step3
Step2
Step4
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Last (Cont.)
void deleteTail(){
if (head = = NULL) //list empty
return;
struct node *cur = head;
struct node *prev = NULL;
while (cur->next != NULL){
prev = cur;
cur=cur->next;
}
if (prev != NULL)
prev->next = cur->next; //NULL;
delete cur;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Any
To delete a node that contains a particular value x in a linked
list, we use a local variable, cur, to point to this node, and
another variable, prev, to hold the previous node.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Any (Cont.)
Step1. Initialize pointer cur to point to the first node of the list, while the
pointer prev has a value of null.
Step2. Traverse the entire list until the pointer cur points to the node that
contains value of x, and prev points to the previous node.
……..
Step 1
Step 2
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Delete Any (Cont.)
…….
Step3. Link the node pointed by pointer prev to the node after the
cur’s node.
Step4. Remove the node pointed by cur.
Step 3
Step 4
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
void deleteAny(int x){
if (head = = NULL) //list empty
return;
struct node *cur = head;
struct node *prev = NULL;
while (cur->value != x){
prev = cur;
cur=cur->next;
}
prev->next = cur->next;
delete cur;
}
Delete Any (Cont.)
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Introduction to Doubly Linked List
We have discussed the details of linear linked list. In the linear linked
list, we can only traverse the linked list in one direction.
But sometimes, it is very desirable to traverse a linked list in either a
forward or reverse manner.
This property of a linked list implies that each node must contain two
link fields instead of one. The links are used to denote the predecessor
and successor of a node. The link denoting the predecessor of a node
is called the left link, and that denoting its successor its right link
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Basic Operation of Doubly Linked List
Insert: Add a new node in the first, last or interior of the
list.
Delete: Delete a node from the first, last or interior of the
list.
Search: Search a node containing particular value in the
linked list.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert First or Insert Last into a Doubly Linked List
Insertion is to add a new node into a linked list. It can take
place anywhere -- the first, last, or interior of the linked list.
To add a new node to the head and tail of a double linked list is
similar to the linear linked list.
First, we need to construct a new node that is pointed by pointer
newItem.
Then the newItem is linked to the left-most node (or right-most
node) in the list. Finally, the Left (or Right) is set to point to the
new node.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert Interior of Doubly Linked List
Step1. Create a new node that is pointed by newItem
Step2. Set the pointer prev to point to the left node of the node pointed by cur.
Step3. Set the left link of the new node to point to the node pointed by prev, and the
right link of the new node to point to the node pointed by cur.
Step4. Set the right link of the node pointed by prev and the left link of the node
pointed by cur to point to the new node.
Step1
Step3
Step2
Step4
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert into a Doubly Linked List
struct node
{
struct node *prev;
int value;
struct node *next;
}*head, *last;
void insert_begning(int data){
struct node *newItem,*temp;
newItem=(struct node *)malloc(sizeof(struct node));
newItem->value=data;
if(head==NULL){
head=newItem; head->prev=NULL;
head->next=NULL; last=head;
}
else{
temp=newItem;
temp->prev=NULL; temp->next=head;
head->prev=temp; head=temp;
}
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert into a Doubly Linked List
void insert_end(int data){
struct node *newItem,*temp;
newItem=(struct node *)malloc(sizeof(struct node));
newItem->value=data;
if(head==NULL){
head=newItem; head->prev=NULL;
head->next=NULL; last=head;
}
else{
last=head;
while(last!=NULL){
temp=last;
last=last->next;
}
last=newItem; temp->next=last;
last->prev=temp; last->next=NULL;
}
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Insert into a Doubly Linked List
int insert_after(int data, int loc){
struct node *temp,*newItem,*temp1;
newItem=(struct node *)malloc(sizeof(struct node));
newItem->value=data;
if(head==NULL){
head=newItem; head->prev=NULL; head->next=NULL; }
else{
temp=head;
while(temp!=NULL && temp->value!=loc)
temp=temp->next;
if (temp==NULL)
printf("n%d is not present in list ",loc);
else{
temp1=temp->next; temp->next=newItem; newItem->prev=temp;
newItem->next=temp1; temp1->prev=newItem; }
}
last=head;
while(last->next!=NULL)
last=last->next;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Deletion of Doubly Linked List
Deletion is to remove a node from a list. It can also take place
anywhere -- the first, last, or interior of a linked list.
To delete a node from a double linked list is easier than to delete
a node from a linear linked list.
For deletion of a node in a single linked list, we have to search
and find the predecessor of the discarded node. But in the double
linked list, no such search is required.
Given the address of the node that is to be deleted, the
predecessor and successor nodes are immediately known.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Deletion of Doubly Linked List (Cont.)
Step1. Set pointer prev to point to the left node of old and pointer cur to
point to the node on the right of old.
Step2. Set the right link of prev to cur, and the left link of cur to prev.
Step3. Discard the node pointed by old.
step1
step3
step2
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Deletion of Doubly Linked List (Cont.)
struct dnode {
  struct dnode *prev;
int value;
  struct dnode *next;
  };
 
void deleteNode (struct dnode *old) {
  if(head == old) /* If node to be deleted is head node */
    head = old->next;
 
  /* Change next only if node to be deleted is NOT the last node */
  if(old->next != NULL)
    old->next->prev = old->prev;
 
  /* Change prev only if node to be deleted is NOT the first node */
  if(old->prev != NULL)
    old->prev->next = old->next;
 
  free(old); /* Finally, free the memory occupied by old*/
  return;
}

More Related Content

What's hot

Linked list
Linked listLinked list
Linked list
akshat360
 
linked list
linked listlinked list
linked list
Abbott
 
Linked lists
Linked listsLinked lists
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
Akila Krishnamoorthy
 
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
Shubham Sharma
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
Adam Mukharil Bachtiar
 
linked list using c
linked list using clinked list using c
linked list using c
Venkat Reddy
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
linked list
linked listlinked list
linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
 
11 15 (doubly linked list)
11 15 (doubly linked list)11 15 (doubly linked list)
11 15 (doubly linked list)
 
Linked list
Linked listLinked list
Linked list
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
 
linked list using c
linked list using clinked list using c
linked list using c
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 

Viewers also liked

超強力青色レーザーポインター
超強力青色レーザーポインター超強力青色レーザーポインター
超強力青色レーザーポインター
onlinelaserjp
 
SGC 2014 - Mathematical Sciences Tutorials
SGC 2014 - Mathematical Sciences TutorialsSGC 2014 - Mathematical Sciences Tutorials
SGC 2014 - Mathematical Sciences TutorialsDaniel Ogburn
 
Thao rửa bể nước tại %G
Thao rửa bể nước tại %GThao rửa bể nước tại %G
Thao rửa bể nước tại %Gmanhtan342
 
Ustav
UstavUstav
δείγματα Xαϊκού
δείγματα Xαϊκούδείγματα Xαϊκού
δείγματα Xαϊκούagpapado
 
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_htmlHttp --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
onlinelaserjp
 
ADL Anywhere Delivery and Logistics Presentation
ADL Anywhere Delivery and Logistics PresentationADL Anywhere Delivery and Logistics Presentation
ADL Anywhere Delivery and Logistics Presentation
Borislava Georgieva
 
Recommendation Borislava Georgieva
Recommendation Borislava GeorgievaRecommendation Borislava Georgieva
Recommendation Borislava GeorgievaBorislava Georgieva
 

Viewers also liked (13)

超強力青色レーザーポインター
超強力青色レーザーポインター超強力青色レーザーポインター
超強力青色レーザーポインター
 
SGC 2014 - Mathematical Sciences Tutorials
SGC 2014 - Mathematical Sciences TutorialsSGC 2014 - Mathematical Sciences Tutorials
SGC 2014 - Mathematical Sciences Tutorials
 
Harishkumar
HarishkumarHarishkumar
Harishkumar
 
Thao rửa bể nước tại %G
Thao rửa bể nước tại %GThao rửa bể nước tại %G
Thao rửa bể nước tại %G
 
Ustav
UstavUstav
Ustav
 
δείγματα Xαϊκού
δείγματα Xαϊκούδείγματα Xαϊκού
δείγματα Xαϊκού
 
ICCP sponsorship overview
ICCP sponsorship overviewICCP sponsorship overview
ICCP sponsorship overview
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_htmlHttp --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
Http --www onlinelaserjp-com-50000mw-voilet-laser-pointer_html
 
Neerav Modi
Neerav ModiNeerav Modi
Neerav Modi
 
Human rightsresourceeng pdf-rev
Human rightsresourceeng pdf-revHuman rightsresourceeng pdf-rev
Human rightsresourceeng pdf-rev
 
ADL Anywhere Delivery and Logistics Presentation
ADL Anywhere Delivery and Logistics PresentationADL Anywhere Delivery and Logistics Presentation
ADL Anywhere Delivery and Logistics Presentation
 
Recommendation Borislava Georgieva
Recommendation Borislava GeorgievaRecommendation Borislava Georgieva
Recommendation Borislava Georgieva
 

Similar to Linked list

Doubly Linked List
Doubly Linked ListDoubly Linked List
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
EmilyMengich
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
ssusera965f6
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
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 lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
Linked List
Linked ListLinked List
Linked List
Md gulam sarwar
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
sauravmanwanicp
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
Kaynattariq1
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
Linklist
LinklistLinklist
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
ISHAN194169
 

Similar to Linked list (20)

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
 
single linked list
single linked listsingle linked list
single linked list
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Team 10
Team 10Team 10
Team 10
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked List
Linked ListLinked List
Linked List
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
Linklist
LinklistLinklist
Linklist
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
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
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
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
 
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
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
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
 
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)
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
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
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
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
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
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
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
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
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
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
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
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
 
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
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
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
 
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...
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
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
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
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
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
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
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
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
 

Linked list

  • 1. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Linked Lists 5 7 3 1 NULL
  • 2. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Linked List A linked list is a linear collection of data elements (called nodes), where the linear order is given by means of pointers. Each node is divided into 2 parts:  1st part contains the information of the element.  2nd part is called the link field or next pointer field which contains the address of the next node in the list. struct node { int value; struct node *next; } 5 7 3 1
  • 3. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Basic Operations Insert: Add a new node in the first, last or interior of the list. Delete: Delete a node from the first, last or interior of the list. Search: Search a node containing particular value in the linked list.
  • 4. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insertion to a Linear Linked list Add a new node at the first, last or interior of a linked list.
  • 5. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert First To add a new node to the head of the linear linked list, we need to construct a new node that is pointed by pointer newitem. Assume there is a global variable head which points to the first node in the list. The new node points to the first node in the list. The head is then set to point to the new node.
  • 6. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert First (Cont.) Step 1. Create a new node that is pointed by pointer newItem. Step 2. Link the new node to the first node of the linked list. Step 3. Set the pointer head to the new node. NULL HEADnewItem NULL HEADnewItem NULL HEAD newItem
  • 7. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert First (Cont.) struct node{ int value; struct node *next; }; struct node *head; void insertHead(){ //create a new node struct node *newItem = new node(); newItem->value = 10; newItem->next = NULL; //insert the new node at the head newItem->next = head; head = newItem; } NULL HEADnewItem NULL HEADnewItem NULL HEAD newItem
  • 8. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert Last To add a new node to the tail of the linear linked list, we need to construct a new node and set it's link field to "null". Assume the list is not empty, locate the last node and change it's link field to point to the new node.
  • 9. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert Last (Cont.) Step1. Create the new node. Step2. Set a temporary pointer prev to point to the last node. Step3. Set prev to point to the new node and new node as last node. NULL HEAD newItem NULL HEAD prev newItem HEAD prev newItem NULL
  • 10. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert Last (Cont.) struct node{ int value; struct node *next; }; struct node *head; void insertTail(){ //create a new node to be inserted struct node *newItem = new node(); newItem->value = 10; newItem->next = NULL; // set prev to point to the last node of the list struct node *prev = head; while (prev->next != NULL){ prev = prev->next; } newItem->next = prev->next; prev->next = newItem; } NULL HEAD NULL HEAD prev newItemHEAD prev NULL
  • 11. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert Middle (after a desired node) struct node{ int value; struct node *next; }; struct node *head; void insertMiddle(int num){ //create a new node to be inserted struct node *newItem = new node(); newItem->value = 10; newItem->next = NULL; // set prev to point to the desired node of the list struct node *prev = head; while (prev->value != num){ prev = prev->next; } newItem->next = prev->next; prev->next = newItem; } HEAD HEAD prev newItemHEAD prev
  • 12. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Printing List void printList() { if (head == NULL) // no list at all return; struct node *cur = head; while (cur != NULL) { printf("%d t", cur->value); cur = cur->next; } }
  • 13. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Deletion from a Linear Linked list Deletion can be done At the first node of linked list. At the end of a linked list. Within the linked list.
  • 14. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete First To delete the first node of the linked list, we not only want to advance the pointer head to the second node, but we also want to release the memory occupied by the abandoned node.
  • 15. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete First (Cont.) Step1. Initialize the pointer cur point to the first node of the list. Step2. Move the pointer head to the second node of the list. Step3. Remove the node that is pointed by the pointer cur. Step 1 Step 2 Step 3
  • 16. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete First (Cont.) void deleteHead() { struct node *cur; if (head = = NULL) //list empty return; cur = head; // save head pointer head = head->next; //advance head delete cur; }
  • 17. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Last To delete the last node in a linked list, we use a local variable, cur, to point to the last node. We also use another variable, prev, to point to the second last node in the linked list.
  • 18. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Last (Cont.) Step1. Initialize pointer cur to point to the first node of the list, while the pointer prev has a value of null. Step2. Traverse the entire list until the pointer cur points to the last node of the list. Step3. Set NULL to next field of the node pointed by the pointer prev. Step4. Remove the last node that is pointed by the pointer cur. Step1 Step3 Step2 Step4
  • 19. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Last (Cont.) void deleteTail(){ if (head = = NULL) //list empty return; struct node *cur = head; struct node *prev = NULL; while (cur->next != NULL){ prev = cur; cur=cur->next; } if (prev != NULL) prev->next = cur->next; //NULL; delete cur; }
  • 20. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Any To delete a node that contains a particular value x in a linked list, we use a local variable, cur, to point to this node, and another variable, prev, to hold the previous node.
  • 21. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Any (Cont.) Step1. Initialize pointer cur to point to the first node of the list, while the pointer prev has a value of null. Step2. Traverse the entire list until the pointer cur points to the node that contains value of x, and prev points to the previous node. …….. Step 1 Step 2
  • 22. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Delete Any (Cont.) ……. Step3. Link the node pointed by pointer prev to the node after the cur’s node. Step4. Remove the node pointed by cur. Step 3 Step 4
  • 23. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET void deleteAny(int x){ if (head = = NULL) //list empty return; struct node *cur = head; struct node *prev = NULL; while (cur->value != x){ prev = cur; cur=cur->next; } prev->next = cur->next; delete cur; } Delete Any (Cont.)
  • 24. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Introduction to Doubly Linked List We have discussed the details of linear linked list. In the linear linked list, we can only traverse the linked list in one direction. But sometimes, it is very desirable to traverse a linked list in either a forward or reverse manner. This property of a linked list implies that each node must contain two link fields instead of one. The links are used to denote the predecessor and successor of a node. The link denoting the predecessor of a node is called the left link, and that denoting its successor its right link
  • 25. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Basic Operation of Doubly Linked List Insert: Add a new node in the first, last or interior of the list. Delete: Delete a node from the first, last or interior of the list. Search: Search a node containing particular value in the linked list.
  • 26. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert First or Insert Last into a Doubly Linked List Insertion is to add a new node into a linked list. It can take place anywhere -- the first, last, or interior of the linked list. To add a new node to the head and tail of a double linked list is similar to the linear linked list. First, we need to construct a new node that is pointed by pointer newItem. Then the newItem is linked to the left-most node (or right-most node) in the list. Finally, the Left (or Right) is set to point to the new node.
  • 27. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert Interior of Doubly Linked List Step1. Create a new node that is pointed by newItem Step2. Set the pointer prev to point to the left node of the node pointed by cur. Step3. Set the left link of the new node to point to the node pointed by prev, and the right link of the new node to point to the node pointed by cur. Step4. Set the right link of the node pointed by prev and the left link of the node pointed by cur to point to the new node. Step1 Step3 Step2 Step4
  • 28. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert into a Doubly Linked List struct node { struct node *prev; int value; struct node *next; }*head, *last; void insert_begning(int data){ struct node *newItem,*temp; newItem=(struct node *)malloc(sizeof(struct node)); newItem->value=data; if(head==NULL){ head=newItem; head->prev=NULL; head->next=NULL; last=head; } else{ temp=newItem; temp->prev=NULL; temp->next=head; head->prev=temp; head=temp; } }
  • 29. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert into a Doubly Linked List void insert_end(int data){ struct node *newItem,*temp; newItem=(struct node *)malloc(sizeof(struct node)); newItem->value=data; if(head==NULL){ head=newItem; head->prev=NULL; head->next=NULL; last=head; } else{ last=head; while(last!=NULL){ temp=last; last=last->next; } last=newItem; temp->next=last; last->prev=temp; last->next=NULL; } }
  • 30. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Insert into a Doubly Linked List int insert_after(int data, int loc){ struct node *temp,*newItem,*temp1; newItem=(struct node *)malloc(sizeof(struct node)); newItem->value=data; if(head==NULL){ head=newItem; head->prev=NULL; head->next=NULL; } else{ temp=head; while(temp!=NULL && temp->value!=loc) temp=temp->next; if (temp==NULL) printf("n%d is not present in list ",loc); else{ temp1=temp->next; temp->next=newItem; newItem->prev=temp; newItem->next=temp1; temp1->prev=newItem; } } last=head; while(last->next!=NULL) last=last->next; }
  • 31. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Deletion of Doubly Linked List Deletion is to remove a node from a list. It can also take place anywhere -- the first, last, or interior of a linked list. To delete a node from a double linked list is easier than to delete a node from a linear linked list. For deletion of a node in a single linked list, we have to search and find the predecessor of the discarded node. But in the double linked list, no such search is required. Given the address of the node that is to be deleted, the predecessor and successor nodes are immediately known.
  • 32. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Deletion of Doubly Linked List (Cont.) Step1. Set pointer prev to point to the left node of old and pointer cur to point to the node on the right of old. Step2. Set the right link of prev to cur, and the left link of cur to prev. Step3. Discard the node pointed by old. step1 step3 step2
  • 33. Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET Deletion of Doubly Linked List (Cont.) struct dnode {   struct dnode *prev; int value;   struct dnode *next;   };   void deleteNode (struct dnode *old) {   if(head == old) /* If node to be deleted is head node */     head = old->next;     /* Change next only if node to be deleted is NOT the last node */   if(old->next != NULL)     old->next->prev = old->prev;     /* Change prev only if node to be deleted is NOT the first node */   if(old->prev != NULL)     old->prev->next = old->next;     free(old); /* Finally, free the memory occupied by old*/   return; }