SlideShare a Scribd company logo
1 of 31
Powerpoint Templates
Page 1
Presentation Topic:
Singly Linked List &
Doubly Linked List
Group Members:
Mohaimin Rahat
Abdul Kahir
Shamim Sarkar
Md.Kamrul Hasan
Powerpoint Templates
Page 2
c
What are Linked Lists
• A linked list is a linear
data structure.
• Nodes make up linked
lists.
• Nodes are structures
made up of data and a
pointer to another node.
• Usually the pointer is
called next.
Powerpoint Templates
Page 3
c
Types of lists
• There are two basic types of linked list
• Singly Linked list
• Doubly linked list
Powerpoint Templates
Page 4
c
Singly Linked List
• Each node has only one link part
• Each link part contains the address of the
next node in the list
• Link part of the last node contains NULL
value which signifies the end of the node
Powerpoint Templates
Page 5
c
 Here is a singly-linked list (SLL):
a b c d
myList
• Each node contains a value(data) and a
pointer to the next node in the list
• MyList is the header pointer which
points at the first node in the list
Powerpoint Templates
Page 6
d
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Powerpoint Templates
Page 7
Inserting the node in a SLL
There are 3 cases here:-
Insertion at the beginning
Insertion at the end
Insertion after a particular node
Powerpoint Templates
Page 8
d
Insertion at the beginning
There are two steps to be followed:-
a) Make the next pointer of the node point towards
the first node of the list
b) Make the start pointer point towards this new
node
 If the list is empty simply make the start pointer
point towards the new node;
Powerpoint Templates
Page 9
a
Powerpoint Templates
Page 10
a
Inserting at the end
Here we simply need to make the next pointer
of the last node point to the new node
Powerpoint Templates
Page 11
a
Inserting after an element
Here we again need to do 2 steps :-
 Make the next pointer of the node to be inserted
point to the next node of the node after which
you want to insert the node
 Make the next pointer of the node after which
the node is to be inserted, point to the node to
be inserted
Powerpoint Templates
Page 12
a
Powerpoint Templates
Page 13
a
Deleting a node in SLL
Here also we have three cases:-
 Deleting the first node
 Deleting the last node
 Deleting the intermediate node
Powerpoint Templates
Page 14
a
Deleting the first node
Here we apply 2 steps:-
 Making the start pointer point towards the 2nd node
 Deleting the first node using delete keyword
threetwoone
start
Powerpoint Templates
Page 15
a
Deleting the last node
Here we apply 2 steps:-
 Making the second last node’s next pointer point
to NULL
 Deleting the last node via delete keyword
node3node2node1
start
Powerpoint Templates
Page 16
a
Deleting a particular node
Here we make the next pointer of the node
previous to the node being deleted ,point to the
successor node of the node to be deleted and
then delete the node using delete keyword
node1 node2 node3
To be deleted
Powerpoint Templates
Page 17
d
Linked lists vs. Doubly linked lists
• Linked lists
– Simple implementation
– Require less memory
• Doubly linked lists
– More complex implementation
– Require more memory
– Accessing elements is easier, since accessing
from the “head” (i.e., leftmost element) costs as
much as accessing from the “tail” (i.e., rightmost
element)
Powerpoint Templates
Page 19
Definition
Doubly is a type of linked list that
allows us to go in both directions
and in a linked list.
Powerpoint Templates
Page 20
A node in a doubly linked list
stores two references:
- a next link, which points to the
next node in the list,
- a prev link, which points to the
previous node in the list.
Powerpoint Templates
Page 21
Such lists allow for :
a great variety of quick update
operations, including insertion and
removal at both ends, and in the
middle.
Powerpoint Templates
Page 22
Doubly linked lists
• A doubly linked list is a list that can be
traversed both from left to right and from right
to left
• It requires to store two head pointers:
– The first one points to the first element in the list
– The second one points to the last element in the
list
first
last
Powerpoint Templates
Page 23
Node definition
• Each node contains:
– The data
– A pointer to the previous element (left-side)
– A pointer to the next element (right-side)
previousPtr data nextPtr
Powerpoint Templates
Page 24
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
.Data .nextprevious.
inf
Powerpoint Templates
Page 25
NODE
A B
C
A doubly linked list contain three fields: an integer
value, the link to the next node, and the link to
the previous node.
previous data next
NULL 11 786
786200 400
200 656 400 786 777 NULL
Powerpoint Templates
Page 26
second last
Creating a doubly linked list
• We are going to create a doubly linked
list by hand:
first
Powerpoint Templates
Page 27
Doubly Linked Lists
Figure 3.14: A doubly linked list with sentinels, header and trailer,
marking the ends of the list. An empty list would have these sentinels
pointing to each other. We do not show the null prev pointer for the
header nor do we show the null next pointer for the trailer.
Powerpoint Templates
Page 28
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
Insert at middle O(n) O(n)
Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle O(n):
O(1) access followed by O(n)
shift
O(n):
O(n) search, followed by O(1) delete
Search O(n) linear search
O(log n) Binary search
O(n)
Indexing: What is
the element at a
given position k?
O(1) O(n)
COMPLEXITY OF VARIOUS
OPERATIONS IN ARRAYS AND SLL
Deletion
Figure 3.15: Removing the node at the end of a a doubly linked list with
header and trailer sentinels: (a) before deleting at the tail; (b) deleting at
the tail; (c) after the deletion.
Insertion
Figure 3.16: Adding an element at the front: (a)
during; (b) after.
Powerpoint Templates
Page 31

More Related Content

What's hot (20)

Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Queues
QueuesQueues
Queues
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
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
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked list
Linked listLinked list
Linked list
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Linked list
Linked listLinked list
Linked list
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Hashing
HashingHashing
Hashing
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Queues
QueuesQueues
Queues
 

Viewers also liked

Linked list without animation
Linked list without animationLinked list without animation
Linked list without animationLovelyn Rose
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structureMarcus Biel
 
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
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Dynamic data structures
Dynamic data structuresDynamic data structures
Dynamic data structures9020303098
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Link list CSE ( Data structure ) .
Link list CSE  ( Data structure ) .Link list CSE  ( Data structure ) .
Link list CSE ( Data structure ) .Nirjhor003
 
Car Parking System (Singly linked list )
Car Parking System (Singly linked list ) Car Parking System (Singly linked list )
Car Parking System (Singly linked list ) S. M. Shakib Limon
 

Viewers also liked (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Linked list without animation
Linked list without animationLinked list without animation
Linked list without animation
 
linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Link List
Link ListLink List
Link List
 
Link list
Link listLink list
Link list
 
Linked list
Linked listLinked list
Linked list
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 
Linked lists
Linked listsLinked lists
Linked lists
 
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
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Dynamic data structures
Dynamic data structuresDynamic data structures
Dynamic data structures
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Data structures
Data structuresData structures
Data structures
 
Link list CSE ( Data structure ) .
Link list CSE  ( Data structure ) .Link list CSE  ( Data structure ) .
Link list CSE ( Data structure ) .
 
Linked List
Linked ListLinked List
Linked List
 
Singly linked lists
Singly linked listsSingly linked lists
Singly linked lists
 
Car Parking System (Singly linked list )
Car Parking System (Singly linked list ) Car Parking System (Singly linked list )
Car Parking System (Singly linked list )
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 

Similar to linked list

Similar to linked list (20)

Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Linked list
Linked listLinked list
Linked list
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
linked list using c
linked list using clinked list using c
linked list using c
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Linked list
Linked listLinked list
Linked list
 
Double link list
Double link listDouble link list
Double link list
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked list
Linked listLinked list
Linked list
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)
 
LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptx
 
Linked List
Linked ListLinked List
Linked List
 

Recently uploaded

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
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
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Recently uploaded (20)

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
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
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

linked list

  • 1. Powerpoint Templates Page 1 Presentation Topic: Singly Linked List & Doubly Linked List Group Members: Mohaimin Rahat Abdul Kahir Shamim Sarkar Md.Kamrul Hasan
  • 2. Powerpoint Templates Page 2 c What are Linked Lists • A linked list is a linear data structure. • Nodes make up linked lists. • Nodes are structures made up of data and a pointer to another node. • Usually the pointer is called next.
  • 3. Powerpoint Templates Page 3 c Types of lists • There are two basic types of linked list • Singly Linked list • Doubly linked list
  • 4. Powerpoint Templates Page 4 c Singly Linked List • Each node has only one link part • Each link part contains the address of the next node in the list • Link part of the last node contains NULL value which signifies the end of the node
  • 5. Powerpoint Templates Page 5 c  Here is a singly-linked list (SLL): a b c d myList • Each node contains a value(data) and a pointer to the next node in the list • MyList is the header pointer which points at the first node in the list
  • 6. Powerpoint Templates Page 6 d Basic Operations on a list • Creating a List • Inserting an element in a list • Deleting an element from a list • Searching a list • Reversing a list
  • 7. Powerpoint Templates Page 7 Inserting the node in a SLL There are 3 cases here:- Insertion at the beginning Insertion at the end Insertion after a particular node
  • 8. Powerpoint Templates Page 8 d Insertion at the beginning There are two steps to be followed:- a) Make the next pointer of the node point towards the first node of the list b) Make the start pointer point towards this new node  If the list is empty simply make the start pointer point towards the new node;
  • 10. Powerpoint Templates Page 10 a Inserting at the end Here we simply need to make the next pointer of the last node point to the new node
  • 11. Powerpoint Templates Page 11 a Inserting after an element Here we again need to do 2 steps :-  Make the next pointer of the node to be inserted point to the next node of the node after which you want to insert the node  Make the next pointer of the node after which the node is to be inserted, point to the node to be inserted
  • 13. Powerpoint Templates Page 13 a Deleting a node in SLL Here also we have three cases:-  Deleting the first node  Deleting the last node  Deleting the intermediate node
  • 14. Powerpoint Templates Page 14 a Deleting the first node Here we apply 2 steps:-  Making the start pointer point towards the 2nd node  Deleting the first node using delete keyword threetwoone start
  • 15. Powerpoint Templates Page 15 a Deleting the last node Here we apply 2 steps:-  Making the second last node’s next pointer point to NULL  Deleting the last node via delete keyword node3node2node1 start
  • 16. Powerpoint Templates Page 16 a Deleting a particular node Here we make the next pointer of the node previous to the node being deleted ,point to the successor node of the node to be deleted and then delete the node using delete keyword node1 node2 node3 To be deleted
  • 17. Powerpoint Templates Page 17 d Linked lists vs. Doubly linked lists • Linked lists – Simple implementation – Require less memory • Doubly linked lists – More complex implementation – Require more memory – Accessing elements is easier, since accessing from the “head” (i.e., leftmost element) costs as much as accessing from the “tail” (i.e., rightmost element)
  • 18.
  • 19. Powerpoint Templates Page 19 Definition Doubly is a type of linked list that allows us to go in both directions and in a linked list.
  • 20. Powerpoint Templates Page 20 A node in a doubly linked list stores two references: - a next link, which points to the next node in the list, - a prev link, which points to the previous node in the list.
  • 21. Powerpoint Templates Page 21 Such lists allow for : a great variety of quick update operations, including insertion and removal at both ends, and in the middle.
  • 22. Powerpoint Templates Page 22 Doubly linked lists • A doubly linked list is a list that can be traversed both from left to right and from right to left • It requires to store two head pointers: – The first one points to the first element in the list – The second one points to the last element in the list first last
  • 23. Powerpoint Templates Page 23 Node definition • Each node contains: – The data – A pointer to the previous element (left-side) – A pointer to the next element (right-side) previousPtr data nextPtr
  • 24. Powerpoint Templates Page 24 Structure of DLL struct node { int data; node*next; node*previous; //holds the address of previous node }; .Data .nextprevious. inf
  • 25. Powerpoint Templates Page 25 NODE A B C A doubly linked list contain three fields: an integer value, the link to the next node, and the link to the previous node. previous data next NULL 11 786 786200 400 200 656 400 786 777 NULL
  • 26. Powerpoint Templates Page 26 second last Creating a doubly linked list • We are going to create a doubly linked list by hand: first
  • 27. Powerpoint Templates Page 27 Doubly Linked Lists Figure 3.14: A doubly linked list with sentinels, header and trailer, marking the ends of the list. An empty list would have these sentinels pointing to each other. We do not show the null prev pointer for the header nor do we show the null next pointer for the trailer.
  • 28. Powerpoint Templates Page 28 Operation ID-Array Complexity Singly-linked list Complexity Insert at beginning O(n) O(1) Insert at end O(1) O(1) if the list has tail reference O(n) if the list has no tail reference Insert at middle O(n) O(n) Delete at beginning O(n) O(1) Delete at end O(1) O(n) Delete at middle O(n): O(1) access followed by O(n) shift O(n): O(n) search, followed by O(1) delete Search O(n) linear search O(log n) Binary search O(n) Indexing: What is the element at a given position k? O(1) O(n) COMPLEXITY OF VARIOUS OPERATIONS IN ARRAYS AND SLL
  • 29. Deletion Figure 3.15: Removing the node at the end of a a doubly linked list with header and trailer sentinels: (a) before deleting at the tail; (b) deleting at the tail; (c) after the deletion.
  • 30. Insertion Figure 3.16: Adding an element at the front: (a) during; (b) after.