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

PPTX
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 
PPTX
Data structure
Nida Ahmed
 
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
PPTX
RPT_03_A_Linked List presentation for FE
AshishFamt
 
PPTX
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
PDF
DS Module 03.pdf
SonaPathak5
 
PDF
Linked list (introduction) 1
DrSudeshna
 
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 
Data structure
Nida Ahmed
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
RPT_03_A_Linked List presentation for FE
AshishFamt
 
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
DS Module 03.pdf
SonaPathak5
 
Linked list (introduction) 1
DrSudeshna
 

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

PPT
Link list part 1
Anaya Zafar
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
Linked List
CHANDAN KUMAR
 
PPTX
Data Structures_Linked List
ThenmozhiK5
 
PPT
Operations on linked list
Sumathi Kv
 
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
PPTX
linked_list.pptx
Koteswari Kasireddy
 
PPT
C1320prespost
FALLEE31188
 
PPT
linked_lists.ppt linked_lists linked_lists
AmsaAzeem
 
PPT
Linked list1.ppt
KasthuriKAssistantPr
 
PPT
ANOITO2341988888888888888888888885555.ppt
robertobula2
 
PPTX
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
PPT
Fundamentals of data structures
Niraj Agarwal
 
PPTX
Lec3-Linked list.pptx
FaheemMahmood2
 
PPTX
UNIT 3a.pptx
jack881
 
PPTX
Linked list
Md. Afif Al Mamun
 
PPTX
Address, Pointers, Arrays, and Structures.pptx
Dr. Amna Mohamed
 
PPTX
1.3 Linked List.pptx
ssuserd2f031
 
PPT
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Link list part 1
Anaya Zafar
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Linked List
CHANDAN KUMAR
 
Data Structures_Linked List
ThenmozhiK5
 
Operations on linked list
Sumathi Kv
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
linked_list.pptx
Koteswari Kasireddy
 
C1320prespost
FALLEE31188
 
linked_lists.ppt linked_lists linked_lists
AmsaAzeem
 
Linked list1.ppt
KasthuriKAssistantPr
 
ANOITO2341988888888888888888888885555.ppt
robertobula2
 
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
Fundamentals of data structures
Niraj Agarwal
 
Lec3-Linked list.pptx
FaheemMahmood2
 
UNIT 3a.pptx
jack881
 
Linked list
Md. Afif Al Mamun
 
Address, Pointers, Arrays, and Structures.pptx
Dr. Amna Mohamed
 
1.3 Linked List.pptx
ssuserd2f031
 
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Ad

Recently uploaded (20)

DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
CDH. pptx
AneetaSharma15
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
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.