SlideShare a Scribd company logo
1 of 29
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
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

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptxjack881
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptxssuserd2f031
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memoryecomputernotes
 
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 searchEstiak Khan
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
 

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

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
linked_list.pptx
linked_list.pptxlinked_list.pptx
linked_list.pptx
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
Linked list
Linked listLinked list
Linked list
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Linked List
Linked ListLinked List
Linked List
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memory
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
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
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

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
  • 26.
  • 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.