SlideShare a Scribd company logo
1 of 32
Download to read offline
LINKED LIST
➢ A linked list is a series of connected nodes,
where each node is a data structure.
➢ A linked list can grow or shrink in size as the
program runs
Example
Bradley Lord
Kristine
Anthony
Alleyah
Alleyah Anthony Bradley Kristine Lord
Moniquee
Moniquee
Ways to maintain a List in a Memory
Two ways
Array
1 2 3 4 5
Linked List
Data Addr Data Addr
Types of Linked List
➢Single Linked List - navigation is forward only.
➢Doubly Linked List - forward and backward navigation
is possible.
➢Circular Linked List - the last element is linked to the
first element
What is a Single Linked List?
➢A single link list is a list made up of nodes that consists of
two parts.
✓Data
✓Link
Data Link
contains the actual data
contains the address of the next node of the list.
Presentation of Single Linked List
Suppose we want to store a list of numbers 20, 35, 50, 70
20 35 50 70
1000
2000 3000 4000
20 2000 35 3000 50 4000 70 NULL
The Address must be store on each node
1000
Head
Creating the Node of a Single Linked List
20 2000 35 3000 50 NULL
1000
struct node{
int data;
struct node *link;
};
20 NULL
1000
#include<stdio.h>
struct node{
int data;
struct node *link;
};
Int main(){
struct node*head = malloc(sizeof(struct node));
head ->data = 20;
head ->link = NULL;
struct node*head = malloc(sizeof(struct node));
head ->data = 35;
head ->link = NULL;
return 0;
}
35 NULL
2000
New head
Head
Doubly Linked List
➢forward and backward navigation is possible.
20 2000 35 3000 50 Null
1000 Head pointer
1000 2000 3000
data
next
0 data next previ
ous
data next previ
ous
data next
head
Struct node
Struct node*prev;
Int data;
Struct node*next;
}
Int main(){
struct node*head = malloc(sizeof(struct
node));
head ->=NULL
head ->data = data;
head ->link = NULL;
return 0;
}
Circular Linked List
Two types
❑Circular Singly Linked List
❑Circular Doubly Linked List
❑Circular Singly Linked List is a similar to singly linked list except that the
last node of the circular singly linked list points to the first node.
20 2000 35 3000 50 NULL
❑ Circular Doubly Linked List – This is similar to a doubly linked list except that the last
node of the circular doubly linked list points to the first node and the first node point
to the last node.
0 data next previ
ous
data next previ
ous
data next
Struct node
Int data;
Struct node*next;
};
int main(){
int data = ______;
struct node*tail;
tail = circularSingly(data);
cout<<tail ->data;
return 0;
}
struct node*circularSingly(int data)
{
struct node*temp = malloc(sizeof(struct node));
temp ->data=data
temp->next= temp;
return temp;
}
20 1000
1000
1000
Circular Singly Linked List
Circular Doubly Linked List
Struct node
Struct node*prev;
Int data;
Struct node*next;
}
int main(){
int data = ______;
struct node*tail;
tail = circularDoubly(data);
cout<<tail ->data;
return 0;
}
struct node*circularDoubly(int data)
{
struct node*temp = malloc(sizeof(struct node));
temp ->data=data
temp->next= temp;
temp->prev= temp;
return temp;
}
100 2 100
100
100
Linked List Operations
We will use the following class declaration, which is stored in FloatList.h.
class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
float value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
FloatList(void) // Constructor
{ head = NULL; }
void appendNode(float);
void insertNode(float);
void deleteNode(float);
void displayList(void);
};
Appending a Node to the List
To append a node to a linked list means to add the
node to the end of the list.
Create a new node.
Store data in the new node.
If there are no nodes in the list
Make the new node the first node.
Else
Traverse the List to Find the last node.
Add the new node to the end of the list.
End If.
Program
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
Sample Program
// This program demonstrates a simple append
// operation on a linked list.
#include <iostream.h>
#include "FloatList.h”
void main(void)
{
FloatList List;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
}
➢ The displayList member function traverses the list,
displaying the value member of each node
Assign List head to node pointer.
While node pointer is not NULL
Display the value member of the node pointed to
by node pointer.
Assign node pointer to its own next member.
End While.
Traversing the List
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
The pseudocode that represents the algorithm
Sample Program 2
// This program calls the displayList member function.
// The funcion traverses the linked list displaying
// the value stored in each node.
#include <iostream.h>
#include "FloatList.h"
void main(void)
{
FloatList List;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
list.displayList();
}
Inserting a Node
Create a new node.
Store data in the new node.
If there are no nodes in the list
Make the new node the first node.
Else
Find the first node whose value is greater than or
equal the new value, or the end of the list (whichever is
first).
Insert the new node before the found node, or at the end of
the list if no node was found.
End If.
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
Algorithm for InsertNode
insertNode function
void FloatList::insertNode(float num)
{
ListNode *newNode, *nodePtr, *previousNode;
// Allocate a new node & store Num
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode.
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new mode is to be the 1st in the list, // insert it before all other nodes.
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else{
previousNode->next = newNode;
newNode->next = nodePtr;
} }
}
Sample Program 3
#include <iostream.h>
#include "FloatList.h”
void main(void)
{
FloatList list;
// Build the list
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
// Insert a node in the middle
// of the list.
list.insertNode(10.5);
// Dispay the list
list.displayList();
}
Sample Program 3 Output
2.5
7.9
10.5
12.6
The deleteNode function
void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the
one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
Deleting Node
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
Sample Program 4
// This program demonstrates the deleteNode member
function
#include <iostream.h>
#include "FloatList.h“
void main(void)
{
FloatList list;
// Build the list
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
cout << "Here are the initial values:n";
list.displayList();
cout << endl;
cout << "Now deleting the node in the
middle.n";
cout << "Here are the nodes left.n";
list.deleteNode(7.9);
list.displayList();
cout << endl;
cout << "Now deleting the last node.n";
cout << "Here are the nodes left.n";
list.deleteNode(12.6);
list.displayList();
cout << endl;
cout << "Now deleting the only remaining
node.n";
cout << "Here are the nodes left.n";
list.deleteNode(2.5);
list.displayList();
}
Program Output
Here are the initial values:
2.5
7.9
12.6
Now deleting the node in the middle.
Here are the nodes left.
2.5
12.6
Now deleting the last node.
Here are the nodes left.
2.5
Now deleting the only remaining node.
Here are the nodes left.

More Related Content

Similar to Lec-4_Linked-List (1).pdf

In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfarjunstores123
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data StructureMuhazzab Chouhadry
 
Write a program to implement below operations with both singly and d.pdf
Write a program to implement below operations with both singly and d.pdfWrite a program to implement below operations with both singly and d.pdf
Write a program to implement below operations with both singly and d.pdfthangarajarivukadal
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.pptWaf1231
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
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
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 

Similar to Lec-4_Linked-List (1).pdf (20)

In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Write a program to implement below operations with both singly and d.pdf
Write a program to implement below operations with both singly and d.pdfWrite a program to implement below operations with both singly and d.pdf
Write a program to implement below operations with both singly and d.pdf
 
Linked list
Linked listLinked list
Linked list
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Linked list
Linked listLinked list
Linked list
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
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...
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Lec-4_Linked-List (1).pdf

  • 1. LINKED LIST ➢ A linked list is a series of connected nodes, where each node is a data structure. ➢ A linked list can grow or shrink in size as the program runs
  • 2. Example Bradley Lord Kristine Anthony Alleyah Alleyah Anthony Bradley Kristine Lord Moniquee Moniquee
  • 3. Ways to maintain a List in a Memory Two ways Array 1 2 3 4 5 Linked List Data Addr Data Addr
  • 4. Types of Linked List ➢Single Linked List - navigation is forward only. ➢Doubly Linked List - forward and backward navigation is possible. ➢Circular Linked List - the last element is linked to the first element
  • 5. What is a Single Linked List? ➢A single link list is a list made up of nodes that consists of two parts. ✓Data ✓Link Data Link contains the actual data contains the address of the next node of the list.
  • 6. Presentation of Single Linked List Suppose we want to store a list of numbers 20, 35, 50, 70 20 35 50 70 1000 2000 3000 4000
  • 7. 20 2000 35 3000 50 4000 70 NULL The Address must be store on each node 1000 Head
  • 8. Creating the Node of a Single Linked List 20 2000 35 3000 50 NULL 1000 struct node{ int data; struct node *link; };
  • 9. 20 NULL 1000 #include<stdio.h> struct node{ int data; struct node *link; }; Int main(){ struct node*head = malloc(sizeof(struct node)); head ->data = 20; head ->link = NULL; struct node*head = malloc(sizeof(struct node)); head ->data = 35; head ->link = NULL; return 0; } 35 NULL 2000 New head Head
  • 10. Doubly Linked List ➢forward and backward navigation is possible. 20 2000 35 3000 50 Null 1000 Head pointer 1000 2000 3000 data next
  • 11. 0 data next previ ous data next previ ous data next head Struct node Struct node*prev; Int data; Struct node*next; } Int main(){ struct node*head = malloc(sizeof(struct node)); head ->=NULL head ->data = data; head ->link = NULL; return 0; }
  • 12. Circular Linked List Two types ❑Circular Singly Linked List ❑Circular Doubly Linked List ❑Circular Singly Linked List is a similar to singly linked list except that the last node of the circular singly linked list points to the first node. 20 2000 35 3000 50 NULL
  • 13. ❑ Circular Doubly Linked List – This is similar to a doubly linked list except that the last node of the circular doubly linked list points to the first node and the first node point to the last node. 0 data next previ ous data next previ ous data next
  • 14. Struct node Int data; Struct node*next; }; int main(){ int data = ______; struct node*tail; tail = circularSingly(data); cout<<tail ->data; return 0; } struct node*circularSingly(int data) { struct node*temp = malloc(sizeof(struct node)); temp ->data=data temp->next= temp; return temp; } 20 1000 1000 1000 Circular Singly Linked List
  • 15. Circular Doubly Linked List Struct node Struct node*prev; Int data; Struct node*next; } int main(){ int data = ______; struct node*tail; tail = circularDoubly(data); cout<<tail ->data; return 0; } struct node*circularDoubly(int data) { struct node*temp = malloc(sizeof(struct node)); temp ->data=data temp->next= temp; temp->prev= temp; return temp; } 100 2 100 100 100
  • 16. Linked List Operations We will use the following class declaration, which is stored in FloatList.h. class FloatList { private: // Declare a structure for the list struct ListNode { float value; struct ListNode *next; }; ListNode *head; // List head pointer public: FloatList(void) // Constructor { head = NULL; } void appendNode(float); void insertNode(float); void deleteNode(float); void displayList(void); };
  • 17. Appending a Node to the List To append a node to a linked list means to add the node to the end of the list. Create a new node. Store data in the new node. If there are no nodes in the list Make the new node the first node. Else Traverse the List to Find the last node. Add the new node to the end of the list. End If.
  • 18. Program void FloatList::appendNode(float num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node if (!head) head = newNode; else // Otherwise, insert newNode at end { // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } }
  • 19. Sample Program // This program demonstrates a simple append // operation on a linked list. #include <iostream.h> #include "FloatList.h” void main(void) { FloatList List; list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6); }
  • 20. ➢ The displayList member function traverses the list, displaying the value member of each node Assign List head to node pointer. While node pointer is not NULL Display the value member of the node pointed to by node pointer. Assign node pointer to its own next member. End While. Traversing the List
  • 21. void FloatList::displayList(void) { ListNode *nodePtr; nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } } The pseudocode that represents the algorithm
  • 22. Sample Program 2 // This program calls the displayList member function. // The funcion traverses the linked list displaying // the value stored in each node. #include <iostream.h> #include "FloatList.h" void main(void) { FloatList List; list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6); list.displayList(); }
  • 23. Inserting a Node Create a new node. Store data in the new node. If there are no nodes in the list Make the new node the first node. Else Find the first node whose value is greater than or equal the new value, or the end of the list (whichever is first). Insert the new node before the found node, or at the end of the list if no node was found. End If.
  • 24. // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is less // than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } Algorithm for InsertNode
  • 25. insertNode function void FloatList::insertNode(float num) { ListNode *newNode, *nodePtr, *previousNode; // Allocate a new node & store Num newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; }
  • 26. else // Otherwise, insert newNode. { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is less // than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new mode is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else{ previousNode->next = newNode; newNode->next = nodePtr; } } }
  • 27. Sample Program 3 #include <iostream.h> #include "FloatList.h” void main(void) { FloatList list; // Build the list list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6); // Insert a node in the middle // of the list. list.insertNode(10.5); // Dispay the list list.displayList(); } Sample Program 3 Output 2.5 7.9 10.5 12.6
  • 28. The deleteNode function void FloatList::deleteNode(float num) { ListNode *nodePtr, *previousNode; // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } Deleting Node
  • 29. else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // Link the previous node to the node after // nodePtr, then delete nodePtr. previousNode->next = nodePtr->next; delete nodePtr; } }
  • 30. Sample Program 4 // This program demonstrates the deleteNode member function #include <iostream.h> #include "FloatList.h“ void main(void) { FloatList list; // Build the list list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6); cout << "Here are the initial values:n"; list.displayList(); cout << endl;
  • 31. cout << "Now deleting the node in the middle.n"; cout << "Here are the nodes left.n"; list.deleteNode(7.9); list.displayList(); cout << endl; cout << "Now deleting the last node.n"; cout << "Here are the nodes left.n"; list.deleteNode(12.6); list.displayList(); cout << endl; cout << "Now deleting the only remaining node.n"; cout << "Here are the nodes left.n"; list.deleteNode(2.5); list.displayList(); }
  • 32. Program Output Here are the initial values: 2.5 7.9 12.6 Now deleting the node in the middle. Here are the nodes left. 2.5 12.6 Now deleting the last node. Here are the nodes left. 2.5 Now deleting the only remaining node. Here are the nodes left.