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

What's hot (20)

Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Array
ArrayArray
Array
 
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
 
Linked List
Linked ListLinked List
Linked List
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
linked list
linked list linked list
linked list
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Queues
QueuesQueues
Queues
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked list
Linked listLinked list
Linked list
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Linked List
Linked ListLinked List
Linked List
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 

Similar to Singly linked list

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 

Similar to Singly linked list (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
 
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
 
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
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 

More from Amar Jukuntla

Linux Directory System: Introduction
Linux Directory System: IntroductionLinux Directory System: Introduction
Linux Directory System: Introduction
Amar Jukuntla
 

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

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Recently uploaded (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 

Singly linked list

  • 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