SlideShare a Scribd company logo
Singly Linked List
A node is a data structure that contains a
data item and one or more links. A link is
a reference to a node
An arrow of each node indicates that the reference of the next node. The
reference of the first node is stored at head. The null in the link part
of the last node indicates that no node follows the last node.
Each node in a linked list can be implemented as a class with two instance
variables data and link, placed inside a linked list class
class Node {
public:
int data;
Node* next;
// Default constructor
Node()
{
data = 0;
next = NULL;
}
// Parameterised Constructor
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
class Linkedlist {
Node* head;
};
Operations:
Traversal
nodeRef walks down the list, referencing each of
the list nodes in turn. The value of nodeRef is
null when the traversal is finished.
current = head
while current is not null:
print current.data
current = current.next
Line 1: Node newNode = new Node(5, null);
Line 2: newNode.next = head;
Line 3: head = newNode;
Line 1 creates a new node with data 5 and link null.
Line 2 makes the link part of the new node refers to the node
that has been pointed by head.
With line 3, now head refers to the new node.
Operations: Insertion at
Front
Operations: Insertion
after
First indicate where the new node will be
added. Let’s say we want to add a new node
after the node referred by p.
newNode.next = p.next;
p.next = newNode;
Operations: Insertion last
if (!head) {
head =
newNode;
return;
}
Node* current
= head;
while
(current->next) {
current =
current->next;
}
current->next
= newNode;
}
deleteFirst():
if head is NU
print "Li
else:
temp = he
head = he
delete te
Delete from beginning
Delete at middle
• Traverse to element before the element to
be deleted
• Change next pointers to exclude the node
from the chain
deleteMiddle(position):
if head is NULL:
print "List is empty"
else if position == 1:
deleteFirst()
else:
current = head
prev = NULL
for i from 1 to position -
prev = current
current = current.next
if current is NULL:
print "Position ou
bounds"
return
prev.next = current.next
delete current
Delete at End
Traverse to second last element
Change its next pointer to null
deleteLast():
if head is NULL:
print "List is
empty"
else if head.next is
NULL:
delete head
head = NULL
else:
current = head
while
current.next.next is not NULL
current =
current.next
delete
current.next
current.next =
NULL
Singly Linked List_Operations-Traversal.pptx

More Related Content

Similar to Singly Linked List_Operations-Traversal.pptx

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
DikkySuryadiSKomMKom
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Link list part 1
Link list part 1Link list part 1
Link list part 1
Anaya Zafar
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
EmilyMengich
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
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
Mani .S (Specialization in Semantic Web)
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
JAGDEEPKUMAR23
 
Linked list
Linked listLinked list
Linked list
maamir farooq
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
Estiak Khan
 

Similar to Singly Linked List_Operations-Traversal.pptx (20)

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Link list part 1
Link list part 1Link list part 1
Link list part 1
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on 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
 
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
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Savitch ch 13
Savitch ch 13Savitch ch 13
Savitch ch 13
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
Linked list
Linked listLinked list
Linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Linked list
Linked listLinked list
Linked list
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 

Recently uploaded

A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
Kamal Acharya
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
VishalDeshpande27
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Md. Shahidul Islam Prodhan
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdfDanfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
NurvisNavarroSanchez
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
Emre Günaydın
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
884710SadaqatAli
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
Kamal Acharya
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
gettygaming1
 

Recently uploaded (20)

A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdfDanfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 

Singly Linked List_Operations-Traversal.pptx

  • 2. A node is a data structure that contains a data item and one or more links. A link is a reference to a node An arrow of each node indicates that the reference of the next node. The reference of the first node is stored at head. The null in the link part of the last node indicates that no node follows the last node. Each node in a linked list can be implemented as a class with two instance variables data and link, placed inside a linked list class
  • 3. class Node { public: int data; Node* next; // Default constructor Node() { data = 0; next = NULL; } // Parameterised Constructor Node(int data) { this->data = data; this->next = NULL; } }; class Linkedlist { Node* head; };
  • 4. Operations: Traversal nodeRef walks down the list, referencing each of the list nodes in turn. The value of nodeRef is null when the traversal is finished. current = head while current is not null: print current.data current = current.next
  • 5. Line 1: Node newNode = new Node(5, null); Line 2: newNode.next = head; Line 3: head = newNode; Line 1 creates a new node with data 5 and link null. Line 2 makes the link part of the new node refers to the node that has been pointed by head. With line 3, now head refers to the new node. Operations: Insertion at Front
  • 6. Operations: Insertion after First indicate where the new node will be added. Let’s say we want to add a new node after the node referred by p. newNode.next = p.next; p.next = newNode;
  • 7. Operations: Insertion last if (!head) { head = newNode; return; } Node* current = head; while (current->next) { current = current->next; } current->next = newNode; }
  • 8. deleteFirst(): if head is NU print "Li else: temp = he head = he delete te Delete from beginning
  • 9. Delete at middle • Traverse to element before the element to be deleted • Change next pointers to exclude the node from the chain deleteMiddle(position): if head is NULL: print "List is empty" else if position == 1: deleteFirst() else: current = head prev = NULL for i from 1 to position - prev = current current = current.next if current is NULL: print "Position ou bounds" return prev.next = current.next delete current
  • 10. Delete at End Traverse to second last element Change its next pointer to null deleteLast(): if head is NULL: print "List is empty" else if head.next is NULL: delete head head = NULL else: current = head while current.next.next is not NULL current = current.next delete current.next current.next = NULL