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

PPT
Mi 103 linked list
PPTX
Data Structures - Lecture 7 [Linked List]
PPT
Linkedlist
PPT
17 linkedlist (1)
PPTX
Linked lists in Data Structure
PPTX
Linked list and its operations - Traversal
PDF
Linked list
PPT
Algo>ADT list & linked list
Mi 103 linked list
Data Structures - Lecture 7 [Linked List]
Linkedlist
17 linkedlist (1)
Linked lists in Data Structure
Linked list and its operations - Traversal
Linked list
Algo>ADT list & linked list

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

PPTX
linkedlist-130914084342-phpapp02.pptx
PPT
Linked list1.ppt
PPTX
LEC_4,5_linked_list.pptx for single and double linked list
PPTX
LEC_4,5_linked_list.pptx this is Good for data structure
PPTX
Linked lists a
PPT
linked-list.ppt
PDF
Lec-4_Linked-List (1).pdf
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPT
Link list part 1
DOCX
Linked list.docx
PPTX
Lec3-Linked list.pptx
PPTX
singlelinkedlistasdfghzxcvbnmqwertyuiopa
PPTX
PPTX
UNIT -4 Singly Linked List-kiruthika.pptx
PPT
Operations on linked list
PPTX
CIRCULAR LINKED LIST _
PPT
Data Structures with C Linked List
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PDF
Singly linked list
PPT
Savitch ch 13
linkedlist-130914084342-phpapp02.pptx
Linked list1.ppt
LEC_4,5_linked_list.pptx for single and double linked list
LEC_4,5_linked_list.pptx this is Good for data structure
Linked lists a
linked-list.ppt
Lec-4_Linked-List (1).pdf
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
Link list part 1
Linked list.docx
Lec3-Linked list.pptx
singlelinkedlistasdfghzxcvbnmqwertyuiopa
UNIT -4 Singly Linked List-kiruthika.pptx
Operations on linked list
CIRCULAR LINKED LIST _
Data Structures with C Linked List
Unit II Data Structure 2hr topic - List - Operations.pptx
Singly linked list
Savitch ch 13
Ad

Recently uploaded (20)

PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PPT
introduction to datamining and warehousing
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT - 3 Total quality Management .pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
Total quality management ppt for engineering students
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
introduction to high performance computing
Fundamentals of Mechanical Engineering.pptx
III.4.1.2_The_Space_Environment.p pdffdf
Exploratory_Data_Analysis_Fundamentals.pdf
Soil Improvement Techniques Note - Rabbi
Fundamentals of safety and accident prevention -final (1).pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Information Storage and Retrieval Techniques Unit III
Automation-in-Manufacturing-Chapter-Introduction.pdf
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
introduction to datamining and warehousing
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
R24 SURVEYING LAB MANUAL for civil enggi
UNIT - 3 Total quality Management .pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Total quality management ppt for engineering students
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
introduction to high performance computing
Ad

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