SlideShare a Scribd company logo
1 of 39
Download to read offline
Linked List
Amar Jukuntla, Assistant Professor,
Department of CSE, VFSTR University
1
Index
• Introduction
• Linked List implementation
• Node Creation
• Insertion at Beginning
• Insertion at End of the List
• Insertion at given Position
• Deletion at Beginning
• Deletion at End of the List
• Deletion at given Position
2
Introduction
• A linked list is a data structure that consists of sequence of nodes. Each node
is composed of two fields: data field and reference field which is
a pointer that points to the next node in the sequence.
3
Continue…
• Each node in the list is also called an element. The reference field that
contains a pointer which points to the next node is called next
pointer or next link.
• A head pointer is used to track the first element in the linked list,
therefore, it always points to the first element.
• The following picture illustrates a linked list.
4
Continue…
• The linked list data structure is designed to be efficient for
insertion or removal of elements from any position in the
list.
• However other operations such as getting the last element or finding
an element that stores specific data requires scanning most or all the
elements in the list.
• A linked list is also used to implement other data structures like stack
and queue.
5
Linked List implementation
• We can model a node of the linked list using a structure as follows:
• The node structure has two members:
• data stores the information
• next pointer holds the address of the next node.
1
2
3
4
typedef struct node{
int data;
struct node* next;
};
6
Add a node at the beginning of the linked list
• First, we declare a head pointer that always points to the first node of
the list.
• To add a node at the beginning of the list:
• First, we need to create a new node. We will need to create a new node
each time we want to insert a new node into the list so we can develop
a function that creates a new node and return it.
1node* head;
7
Node Creation
8
node* create(int data, node* next)
{
node* new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
printf("Error creating a new node.n");
exit(0);
}
new_node->data = data;
new_node->next = next;
return new_node;
} 9
Adding a node at beginning
10
Adding a node at beginning
•Steps to insert node at the beginning of singly
linked list
• Create a new node, say newNode points to the newly
created node.
• Link the newly created node with the head node, i.e.
the newNode will now point to head node.
• Make the new node as the head node, i.e. now head node
will point to newNode
11
12
13
14
Continue…
newNode->data = data; // Link data part
newNode->next = head; // Link address part
head = newNode;
15
Insert node at Last / End Position in Singly
Linked List
•Inserting node at start in the Single Linked List
(Steps):
• Create New Node
• Fill Data into “Data Field“
• Make it’s “Pointer” or “Next Field” as NULL
• Node is to be inserted at Last Position so we need to
traverse SLL upto Last Node.
• Make link between last node and newnode
16
17
18
Insert Node at given Position
19
Continue…
• Check if the given prev_node is NULL
• Allocate new node
• Put in the data
• Make next of new node as next of prev_node.
• Move the next of prev_node as new_node
20
21
Deletion of a Node at Beginning
22
Continue…
•Steps to delete first node from Singly Linked List
• Copy the address of first node i.e. head node to some temp
variable say toDelete.
• Move the head to the second node of the linked list
i.e. head = head->next.
• Disconnect the connection of first node to second node.
• Free the memory occupied by the first node.
23
Step 1: Copy the address of first node
24
Step 2: Move the head to the second node
25
Step 3: Disconnect the connection of first
node
26
Step 4: Free the memory
27
28
delNode= head;
head=head->next;
free(delNode);
Deletion of a Node at End of the List
• Steps to delete last node of a Singly Linked List
• Traverse to the last node of the linked list keeping track of the second last node
in some temp variable say secondLastNode.
• If the last node is the head node then make the head node as NULL else
disconnect the second last node with the last node i.e. secondLastNode->next
= NULL.
• Free the memory occupied by the last node.
29
Step 1: Traverse to the last node of the linked
list keeping track of the second last node
30
Step 2: secondLastNode->next = NULL.
31
Step 3: Free the memory occupied by the last
node
32
33
void deletionAtE()
{
struct node *temp, *temp1;
if(head== NULL) {
printf("Node Already Nulln");
}else{
temp=head;
temp1=NULL;
while(temp->next !=NULL){
temp1=temp;
temp=temp->next;
}
if(temp1!=NULL){
temp1->next=NULL;
}
if(temp==head)
head=NULL;
free(temp);
printf("Deletedn");
}
}
Deletion at given Position
•Steps to delete middle node of Singly Linked List
•Traverse to the nth node of the singly linked list and also
keep reference of n-1th node in some temp variable
say prevnode.
34
Continue…
• Reconnect the n-1th node with the n+1th node i.e. prevNode->next =
toDelete->next (Where prevNode is n-1th node and toDelete node is
the nth node and toDelete->next is the n+1th node).
35
Continue…
• Free the memory occupied by the nth node i.e. toDelete node.
36
Continue…
// Find previous node of the node to be deleted
for (int i=0; prev!=NULL && i<pos-1; i++)
prev = prev->next;
for (int i=0; next!=NULL && i<pos+1; i++)
next = next->next;
prev->next=next;
37
Reverse of a List
struct node *current, *prev,*next;
current=head;
prev=NULL;
while(current!=NULL) {
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
38
Next Session
• Doubly Linked List
• Circular Linked List
39

More Related Content

What's hot

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)Huba Akhtar
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy TutorialAfzal Badshah
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 

What's hot (20)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Linked lists
Linked listsLinked lists
Linked lists
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
linked list
linked list linked list
linked list
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Binary tree
Binary tree Binary tree
Binary tree
 
Selection sort
Selection sortSelection sort
Selection sort
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 

Similar to Linked List Data Structure Explained in 40 Characters

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxskilljiolms
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of listsmuskans14
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdfSonaPathak5
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsAdithaBuwaneka
 
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 List Data Structure Explained in 40 Characters (20)

Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
5.Linked list
5.Linked list 5.Linked list
5.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
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Linked list
Linked listLinked list
Linked list
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Linked list
Linked listLinked list
Linked list
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.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 list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 

More from Amar Jukuntla (19)

Types of files
Types of filesTypes of files
Types of files
 
Hashing
HashingHashing
Hashing
 
Planning
Planning Planning
Planning
 
Unit 2
Unit 2Unit 2
Unit 2
 
Problem Solving
Problem Solving Problem Solving
Problem Solving
 
Intelligent Agents
Intelligent Agents Intelligent Agents
Intelligent Agents
 
Introduction
IntroductionIntroduction
Introduction
 
DFS
DFSDFS
DFS
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Nature of open source
Nature of open sourceNature of open source
Nature of open source
 
Linux Directory System: Introduction
Linux Directory System: IntroductionLinux Directory System: Introduction
Linux Directory System: Introduction
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
 
Learning
LearningLearning
Learning
 
First Order Logic resolution
First Order Logic resolutionFirst Order Logic resolution
First Order Logic resolution
 
First Order Logic
First Order LogicFirst Order Logic
First Order Logic
 
A*
A*A*
A*
 
Agents1
Agents1Agents1
Agents1
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

Linked List Data Structure Explained in 40 Characters

  • 1. Linked List Amar Jukuntla, Assistant Professor, Department of CSE, VFSTR University 1
  • 2. Index • Introduction • Linked List implementation • Node Creation • Insertion at Beginning • Insertion at End of the List • Insertion at given Position • Deletion at Beginning • Deletion at End of the List • Deletion at given Position 2
  • 3. Introduction • A linked list is a data structure that consists of sequence of nodes. Each node is composed of two fields: data field and reference field which is a pointer that points to the next node in the sequence. 3
  • 4. Continue… • Each node in the list is also called an element. The reference field that contains a pointer which points to the next node is called next pointer or next link. • A head pointer is used to track the first element in the linked list, therefore, it always points to the first element. • The following picture illustrates a linked list. 4
  • 5. Continue… • The linked list data structure is designed to be efficient for insertion or removal of elements from any position in the list. • However other operations such as getting the last element or finding an element that stores specific data requires scanning most or all the elements in the list. • A linked list is also used to implement other data structures like stack and queue. 5
  • 6. Linked List implementation • We can model a node of the linked list using a structure as follows: • The node structure has two members: • data stores the information • next pointer holds the address of the next node. 1 2 3 4 typedef struct node{ int data; struct node* next; }; 6
  • 7. Add a node at the beginning of the linked list • First, we declare a head pointer that always points to the first node of the list. • To add a node at the beginning of the list: • First, we need to create a new node. We will need to create a new node each time we want to insert a new node into the list so we can develop a function that creates a new node and return it. 1node* head; 7
  • 9. node* create(int data, node* next) { node* new_node = (node*)malloc(sizeof(node)); if(new_node == NULL) { printf("Error creating a new node.n"); exit(0); } new_node->data = data; new_node->next = next; return new_node; } 9
  • 10. Adding a node at beginning 10
  • 11. Adding a node at beginning •Steps to insert node at the beginning of singly linked list • Create a new node, say newNode points to the newly created node. • Link the newly created node with the head node, i.e. the newNode will now point to head node. • Make the new node as the head node, i.e. now head node will point to newNode 11
  • 12. 12
  • 13. 13
  • 14. 14
  • 15. Continue… newNode->data = data; // Link data part newNode->next = head; // Link address part head = newNode; 15
  • 16. Insert node at Last / End Position in Singly Linked List •Inserting node at start in the Single Linked List (Steps): • Create New Node • Fill Data into “Data Field“ • Make it’s “Pointer” or “Next Field” as NULL • Node is to be inserted at Last Position so we need to traverse SLL upto Last Node. • Make link between last node and newnode 16
  • 17. 17
  • 18. 18
  • 19. Insert Node at given Position 19
  • 20. Continue… • Check if the given prev_node is NULL • Allocate new node • Put in the data • Make next of new node as next of prev_node. • Move the next of prev_node as new_node 20
  • 21. 21
  • 22. Deletion of a Node at Beginning 22
  • 23. Continue… •Steps to delete first node from Singly Linked List • Copy the address of first node i.e. head node to some temp variable say toDelete. • Move the head to the second node of the linked list i.e. head = head->next. • Disconnect the connection of first node to second node. • Free the memory occupied by the first node. 23
  • 24. Step 1: Copy the address of first node 24
  • 25. Step 2: Move the head to the second node 25
  • 26. Step 3: Disconnect the connection of first node 26
  • 27. Step 4: Free the memory 27
  • 29. Deletion of a Node at End of the List • Steps to delete last node of a Singly Linked List • Traverse to the last node of the linked list keeping track of the second last node in some temp variable say secondLastNode. • If the last node is the head node then make the head node as NULL else disconnect the second last node with the last node i.e. secondLastNode->next = NULL. • Free the memory occupied by the last node. 29
  • 30. Step 1: Traverse to the last node of the linked list keeping track of the second last node 30
  • 32. Step 3: Free the memory occupied by the last node 32
  • 33. 33 void deletionAtE() { struct node *temp, *temp1; if(head== NULL) { printf("Node Already Nulln"); }else{ temp=head; temp1=NULL; while(temp->next !=NULL){ temp1=temp; temp=temp->next; } if(temp1!=NULL){ temp1->next=NULL; } if(temp==head) head=NULL; free(temp); printf("Deletedn"); } }
  • 34. Deletion at given Position •Steps to delete middle node of Singly Linked List •Traverse to the nth node of the singly linked list and also keep reference of n-1th node in some temp variable say prevnode. 34
  • 35. Continue… • Reconnect the n-1th node with the n+1th node i.e. prevNode->next = toDelete->next (Where prevNode is n-1th node and toDelete node is the nth node and toDelete->next is the n+1th node). 35
  • 36. Continue… • Free the memory occupied by the nth node i.e. toDelete node. 36
  • 37. Continue… // Find previous node of the node to be deleted for (int i=0; prev!=NULL && i<pos-1; i++) prev = prev->next; for (int i=0; next!=NULL && i<pos+1; i++) next = next->next; prev->next=next; 37
  • 38. Reverse of a List struct node *current, *prev,*next; current=head; prev=NULL; while(current!=NULL) { next=current->next; current->next=prev; prev=current; current=next; } head=prev; 38
  • 39. Next Session • Doubly Linked List • Circular Linked List 39