SlideShare a Scribd company logo
Chapter 3
Linked Lists
Review on pointer
• Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
• A pointer is a variable used to store the
address of a memory cell.
Cont…
• Declaration of Pointer variables
type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)
• The "address of " operator (&) gives the memory address of the
variable
– Usage: &variable_name
#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a <<
endl;
cout << "The address of b is: " << &b <<
endl;
Result is:
The address of a is: 1020
The address of b is: 1024
Cont..
We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
int a = 100;
int *p = &a;
cout << a << endl;
cout << &a << endl;
cout << p << " " << *p << endl;
cout << &p << endl;
Result is:
100
1024
1024 100
1032
Review on Structures
Structures are aggregate data types built using
elements of primitive data types.
The struct keyword creates a new user defined
data type that is used to declare variables of an
aggregate data type.
Structure variables are declared like variables of
other types.
• Syntax: struct <structure tag> <variable name>;
E.g. struct Time timeObject,
struct Time *timeptr;
Cont…
Accessing Members of Structure Variables
• The Dot operator (.): to access data members of
structure variables.
• The Arrow operator (->): to access data members
of pointer variables pointing to the structure.
• E.g. Print member hour of timeObject and
timeptr.
cout<< timeObject.hour; or
cout<<timeptr->hour;
TIP: timeptr->hour is the same as (*timeptr).hour.
What is linked list?
Linked list is a data structure used for storing
collections of data.
linked list have the following properties:
1. successive elements are connected by pointers
2. Last elements points to Null
3. Can grow or shrink in size during program
execution
4. Can be made just as long as required(until
system memory exhausts)
Cont…
A linked list is made up of a chain of nodes.
Each node contains:
• the data item- holds actual elements on the
list
• a pointer to the next node- address of the
next/prev node in the list
Cont…
Array vs. linked list
Arrays
-simple and easy to use
-faster access to the elements
-fixed size
-complex position based insertion
Linked list
- Dynamic memory allocation
- Easy position based insertion
Dynamic memory allocation
Dynamic memory allocation is when an
executing program requests that the operating
system give it a block of main memory.
The program then uses this memory for some
purpose. Usually the purpose is to add a node to
a data structure.
In object oriented languages, dynamic memory
allocation is used to get the memory for a new
object.
Types of linked lists
1.singly linked list
2. Doubly linked list
Singly linked list
A singly linked list can be represented by a
diagram like shown blow:
Cont…
Start (Head): Special pointer that points to the
first node of a linked list, so that we can keep
track of the linked list.
The last node should points to NULL to show
that it is the last link in the chain (in the linked
list).
Creating Linked Lists in C++
• A linked list is a data structure that is built
from structures and pointers.
• It forms a chain of "nodes" with pointers
representing the links of the chain and holding
the entire thing together.
• A linked list can be represented by a diagram
like this one:
Cont…
According to the above example in the figure, it is
the singly linked list which has four nodes in it, each
with a link to the next node in the series (in the
linked list).
Defining the data structure for a
linked list
The key part of a linked list is a structure,
which holds the data for each node (the
name, address, age or whatever for the
items in the list), and, most importantly,
a pointer to the next node.
Cont …
Here we have given the structure of a typical
node:
struct node {
char name[20]; // Name of up to 20 letters
int age;
float height; // In metres
node *nxt;// Pointer to next node
};struct node *start_ptr = NULL;
Adding a node to the list
Steps
1. Allocate a new node
2. Set the node data values and make new node
point to Null
3. Make old last node’s next pointer point to
the new node
4. *make the new last node’s prev pointer point
to the old last node.(this is only for DLL)
Cont…
• Insert at the front
Steps
 allocate a new node
Insert new element values
Make the next pointer of the new node point
to old head(start)
Update head to point to the new node
Example
cont…
• Insert at the end
Steps
Allocate a new node
Set the node data values and make the next
pointer of the new node point to null
Make old last node’s next pointer point to the
new node
Update end to point to the new node
Cont …
• Insertion in the middle
Steps
Create a new node
Set the node data values
Break pointer connection
Reconnect the pointers
Deleting the 1st node in SLL
Data structure and algorithms chapter three LINKED LIST
Double linked list(DLL)
• A doubly linked list is one where there are
links from each node in both directions:
• each node in the list has two pointers, one to
the next node and one to the previous one -
again, the ends of the list are defined by NULL
pointers.
DLL…
Defining DLL

More Related Content

Similar to Data structure and algorithms chapter three LINKED LIST (20)

Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
DS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
linked list.pptx
linked list.pptx
DarbhalaPavanKumar
 
Data structure
Data structure
Shahariar limon
 
Linked lists a
Linked lists a
Khuram Shahzad
 
linked list
linked list
Ayesha Sajjad
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
Linked List.pptx
Linked List.pptx
PoonamPatil120
 
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Linked list (introduction) 1
Linked list (introduction) 1
DrSudeshna
 
Linked lists linked lists vs Arrays.pptx
Linked lists linked lists vs Arrays.pptx
Sahar160629
 
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
vaibhavkore8
 
module 3-.pptx
module 3-.pptx
kumarkaushal17
 
Linked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
linkedlist.pptx
linkedlist.pptx
MeghaKulkarni27
 
Data Structures Introduction & Linear DS
Data Structures Introduction & Linear DS
sailaja156145
 
Linked list
Linked list
akshat360
 
Linked lists
Linked lists
Eleonora Ciceri
 
The study underscores the importance of collaborative efforts and technologic...
The study underscores the importance of collaborative efforts and technologic...
DaudPhiri
 
dynamicList.ppt
dynamicList.ppt
ssuser0be977
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
DS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Linked list (introduction) 1
Linked list (introduction) 1
DrSudeshna
 
Linked lists linked lists vs Arrays.pptx
Linked lists linked lists vs Arrays.pptx
Sahar160629
 
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
vaibhavkore8
 
Linked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Data Structures Introduction & Linear DS
Data Structures Introduction & Linear DS
sailaja156145
 
The study underscores the importance of collaborative efforts and technologic...
The study underscores the importance of collaborative efforts and technologic...
DaudPhiri
 

Recently uploaded (20)

SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Ad

Data structure and algorithms chapter three LINKED LIST

  • 2. Review on pointer • Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there • A pointer is a variable used to store the address of a memory cell.
  • 3. Cont… • Declaration of Pointer variables type* pointer_name; //or type *pointer_name; where type is the type of data pointed to (e.g. int, char, double)
  • 4. • The "address of " operator (&) gives the memory address of the variable – Usage: &variable_name #include <iostream> using namespace std; void main(){ int a, b; a = 88; b = 100; cout << "The address of a is: " << &a << endl; cout << "The address of b is: " << &b << endl; Result is: The address of a is: 1020 The address of b is: 1024
  • 5. Cont.. We can access to the value stored in the variable pointed to by using the dereferencing operator (*), int a = 100; int *p = &a; cout << a << endl; cout << &a << endl; cout << p << " " << *p << endl; cout << &p << endl; Result is: 100 1024 1024 100 1032
  • 6. Review on Structures Structures are aggregate data types built using elements of primitive data types. The struct keyword creates a new user defined data type that is used to declare variables of an aggregate data type. Structure variables are declared like variables of other types. • Syntax: struct <structure tag> <variable name>; E.g. struct Time timeObject, struct Time *timeptr;
  • 7. Cont… Accessing Members of Structure Variables • The Dot operator (.): to access data members of structure variables. • The Arrow operator (->): to access data members of pointer variables pointing to the structure. • E.g. Print member hour of timeObject and timeptr. cout<< timeObject.hour; or cout<<timeptr->hour; TIP: timeptr->hour is the same as (*timeptr).hour.
  • 8. What is linked list? Linked list is a data structure used for storing collections of data. linked list have the following properties: 1. successive elements are connected by pointers 2. Last elements points to Null 3. Can grow or shrink in size during program execution 4. Can be made just as long as required(until system memory exhausts)
  • 9. Cont… A linked list is made up of a chain of nodes. Each node contains: • the data item- holds actual elements on the list • a pointer to the next node- address of the next/prev node in the list
  • 11. Array vs. linked list Arrays -simple and easy to use -faster access to the elements -fixed size -complex position based insertion Linked list - Dynamic memory allocation - Easy position based insertion
  • 12. Dynamic memory allocation Dynamic memory allocation is when an executing program requests that the operating system give it a block of main memory. The program then uses this memory for some purpose. Usually the purpose is to add a node to a data structure. In object oriented languages, dynamic memory allocation is used to get the memory for a new object.
  • 13. Types of linked lists 1.singly linked list 2. Doubly linked list
  • 14. Singly linked list A singly linked list can be represented by a diagram like shown blow:
  • 15. Cont… Start (Head): Special pointer that points to the first node of a linked list, so that we can keep track of the linked list. The last node should points to NULL to show that it is the last link in the chain (in the linked list).
  • 16. Creating Linked Lists in C++ • A linked list is a data structure that is built from structures and pointers. • It forms a chain of "nodes" with pointers representing the links of the chain and holding the entire thing together. • A linked list can be represented by a diagram like this one:
  • 17. Cont… According to the above example in the figure, it is the singly linked list which has four nodes in it, each with a link to the next node in the series (in the linked list).
  • 18. Defining the data structure for a linked list The key part of a linked list is a structure, which holds the data for each node (the name, address, age or whatever for the items in the list), and, most importantly, a pointer to the next node.
  • 19. Cont … Here we have given the structure of a typical node: struct node { char name[20]; // Name of up to 20 letters int age; float height; // In metres node *nxt;// Pointer to next node };struct node *start_ptr = NULL;
  • 20. Adding a node to the list Steps 1. Allocate a new node 2. Set the node data values and make new node point to Null 3. Make old last node’s next pointer point to the new node 4. *make the new last node’s prev pointer point to the old last node.(this is only for DLL)
  • 21. Cont… • Insert at the front Steps  allocate a new node Insert new element values Make the next pointer of the new node point to old head(start) Update head to point to the new node
  • 23. cont… • Insert at the end Steps Allocate a new node Set the node data values and make the next pointer of the new node point to null Make old last node’s next pointer point to the new node Update end to point to the new node
  • 24. Cont … • Insertion in the middle Steps Create a new node Set the node data values Break pointer connection Reconnect the pointers
  • 25. Deleting the 1st node in SLL
  • 27. Double linked list(DLL) • A doubly linked list is one where there are links from each node in both directions: • each node in the list has two pointers, one to the next node and one to the previous one - again, the ends of the list are defined by NULL pointers.