SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Linked Lists
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
2
Introduction to the 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
3
Advantages of Linked Lists over Arrays and
Vectors
• A linked list can easily grow or shrink in size.
• Insertion and deletion of nodes is quicker with linked lists than with
vectors.
The Composition of a Linked List
• Each node in a linked list contains one or more members that
represent data.
• In addition to the data, each node contains a pointer, which can point
to another node.
4
The composition of a Linked List
• A linked list is called "linked" because each node in the series has a
pointer that points to the next node in the list.
5
Declarations
• First you must declare a data structure that will be used for the
nodes. For example, the following struct could be used to create a
list where each node holds a float:
6
struct ListNode
{
float value;
struct ListNode *next;
};
Declarations
• The next step is to declare a pointer to serve as the list head, as
shown below.
•Once you have declared a node data structure and have created a
NULL head pointer, you have an empty linked list.
•The next step is to implement operations with the list.
7
ListNode *head;
Linked List Operations
• Different operations can be performed on list such as:
• Append
• Insert
• Delete
• Display / Traversing
• We will use the following class declaration (on the next slide), which is
stored in FloatList.h.
8
9
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; }
~FloatList(void); // Destructor
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.
• The pseudocode is shown below. The C++ code follows.
10
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.
11
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==null
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->next!=null
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
Code Segment appendNode
12
Program: 1
// 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);
}
(This program displays no output.)
Stepping Through the Program
• The head pointer is declared as a global variable. head is
automatically initialized to 0 (NULL), which indicates that the list is
empty.
• The first call to appendNode passes 2.5 as the argument. In the
following statements, a new node is allocated in memory, 2.5 is
copied into its value member, and NULL is assigned to the node's
next pointer.
13
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
14
The next statement to execute is the following if statement.
if (!head)
head = newNode;
There are no more statements to execute, so control returns to function main.
In the second call to appendNode, 7.9 is passed as the argument. Once again, the first
three statements in the function create a new node, store the argument in the node's
value member, and assign its next pointer to NULL.
SECOND FUNCTION CALL
15
Since head no longer points to NULL, the else part of the if statement executes:
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;
}
• nodePtr is already at the end of the list, so the while loop
immediately terminates.
• The last statement, nodePtr->next = newNode; causes
nodePtr->next to point to the new node.
• This inserts newNode at the end of the list.
16
The third time appendNode is called, 12.6 is passed as the argument. Once again, the
first three statements create a node with the argument stored in the value member.
THIRD FUNCTION CALL
next, the else part of the if statement executes. As before, nodePtr is made to
point to the same node as head.
17
Since nodePtr->next is not NULL, the
while loop will execute. After its first iteration,
nodePtr will point to the second node in the
list.
The while loop's conditional test will fail after
the first iteration because nodePtr->next
now points to NULL. The last statement,
nodePtr->next = newNode; causes
nodePtr->next to point to the new node.
This inserts newNode at the end of the list
The figure above depicts the final state of the linked list.
Traversing the List
• The displayList member function traverses the list, displaying the value
member of each node. The following pseudocode represents the algorithm. The
C++ code for the member function follows on the next slide.
18
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.
Code Segment displayList
19
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
Program: 2
20
// 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();
}
2.5
7.9
12.6
Output:
21
Inserting a Node
• Using the listNode structure again, the pseudocode on the next slide
shows an algorithm for finding a new node’s proper position in the list and
inserting there.
• The algorithm assumes the nodes in the list are already in order.
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.
22
The code for the traversal algorithm is shown below. (As before, num holds the value being
inserted into the list.)
// 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;
}
The entire insertNode function begins on the next slide.
23
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;
}
Continued on next slide…
24
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == NULL)
{
head = newNode;
newNode-> = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
Continued from previous slide.
Program: 3
25
// This program calls the displayList member function.
// The function traverses the linked list displaying the value stored in each node.
#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();
}
2.5
7.9
10.5
12.6
Output:
26
In insertNode, a new node is created and the function
argument is copied to its value member. Since the list
already has nodes stored in it, the else part of the if
statement will execute. It begins by assigning nodePtr to
head.
Stepping Through the Program
Since nodePtr is not NULL and nodePtr->value is less
than num, the while loop will iterate. During the iteration,
previousNode will be made to point to the node that
nodePtr is pointing to. nodePtr will then be advanced to
point to the next node.
27
Once again, the loop performs its test. Since nodePtr is not NULL
and nodePtr->value is less than num, the loop will iterate a
second time. During the second iteration, both previousNode and
nodePtr are advanced by one node in the list.
This time, the loop's test will fail because nodePtr is not less than num. The statements after the loop will
execute, which cause previousNode->next to point to newNode, and newNode->next to point to
nodePtr.
If you follow the links, from the head pointer to
the NULL, you will see that the nodes are stored
in the order of their value members.
28
Deleting a Node
• Deleting a node from a linked list requires two steps:
• Remove the node from the list without breaking the links created by the next
pointers
• Deleting the node from memory
• The deleteNode function begins on the next slide.
29
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;
}
Continued on next slide…
30
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;
}
}
Continued from previous slide.
Program: 4
31
// 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;
Continued on next slide…
32
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();
}
Continued from previous slide.
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.
33
Look at the else part of the second if statement. This is where the function will perform
its action since the list is not empty, and the first node does not contain the value 7.9. Just
like insertNode, this function uses nodePtr and previousNode to traverse the list.
The while loop terminates when the value 7.9 is located. At this point, the list and the other
pointers will be in the state depicted in the figure below.
Stepping Through the Program
34
next, the following statement executes.
previousNode->next = nodePtr->next;
The statement above causes the links in the list to bypass the node that nodePtr points to.
Although the node still exists in memory, this removes it from the list.
The last statement uses the delete operator to complete the total deletion of the node.
35
Destroying the List
• The class's destructor should release all the memory used by the list.
• It does so by stepping through the list, deleting each node one-by-
one.
FloatList::~FloatList(void)
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
Notice the use of nextNode instead of previousNode. The nextNode pointer is used to hold the position
of the next node in the list, so it will be available after the node pointed to by nodePtr is deleted.
Assignment: Variations of the Linked List
36
The Doubly-Linked List
The Circular Linked List
• Implementation of these two variations in
C++ using OOP
• Submit softcopy of your codes on
m.hammad.wasim@gmail.com before
Wednesday, Jan, 07. 3:00 PM

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Linked list
Linked listLinked list
Linked list
 
Singly link list
Singly link listSingly link list
Singly link list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked List
Linked ListLinked List
Linked List
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly 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...
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
 

Andere mochten auch (7)

Student record
Student recordStudent record
Student record
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Ähnlich wie Data Structures - Lecture 7 [Linked List]

Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 

Ähnlich wie Data Structures - Lecture 7 [Linked List] (20)

Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Ch17
Ch17Ch17
Ch17
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Savitch ch 13
Savitch ch 13Savitch ch 13
Savitch ch 13
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
 
Link list part 1
Link list part 1Link list part 1
Link list part 1
 
Linked list
Linked listLinked list
Linked list
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 

Mehr von Muhammad Hammad Waseem

Mehr von Muhammad Hammad Waseem (20)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Kürzlich hochgeladen (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Data Structures - Lecture 7 [Linked List]

  • 1. Linked Lists Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  • 2. 2 Introduction to the 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
  • 3. 3 Advantages of Linked Lists over Arrays and Vectors • A linked list can easily grow or shrink in size. • Insertion and deletion of nodes is quicker with linked lists than with vectors.
  • 4. The Composition of a Linked List • Each node in a linked list contains one or more members that represent data. • In addition to the data, each node contains a pointer, which can point to another node. 4
  • 5. The composition of a Linked List • A linked list is called "linked" because each node in the series has a pointer that points to the next node in the list. 5
  • 6. Declarations • First you must declare a data structure that will be used for the nodes. For example, the following struct could be used to create a list where each node holds a float: 6 struct ListNode { float value; struct ListNode *next; };
  • 7. Declarations • The next step is to declare a pointer to serve as the list head, as shown below. •Once you have declared a node data structure and have created a NULL head pointer, you have an empty linked list. •The next step is to implement operations with the list. 7 ListNode *head;
  • 8. Linked List Operations • Different operations can be performed on list such as: • Append • Insert • Delete • Display / Traversing • We will use the following class declaration (on the next slide), which is stored in FloatList.h. 8
  • 9. 9 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; } ~FloatList(void); // Destructor void appendNode(float); void insertNode(float); void deleteNode(float); void displayList(void); };
  • 10. 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. • The pseudocode is shown below. The C++ code follows. 10 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.
  • 11. 11 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==null 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->next!=null nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } } Code Segment appendNode
  • 12. 12 Program: 1 // 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); } (This program displays no output.)
  • 13. Stepping Through the Program • The head pointer is declared as a global variable. head is automatically initialized to 0 (NULL), which indicates that the list is empty. • The first call to appendNode passes 2.5 as the argument. In the following statements, a new node is allocated in memory, 2.5 is copied into its value member, and NULL is assigned to the node's next pointer. 13 newNode = new ListNode; newNode->value = num; newNode->next = NULL;
  • 14. 14 The next statement to execute is the following if statement. if (!head) head = newNode; There are no more statements to execute, so control returns to function main. In the second call to appendNode, 7.9 is passed as the argument. Once again, the first three statements in the function create a new node, store the argument in the node's value member, and assign its next pointer to NULL. SECOND FUNCTION CALL
  • 15. 15 Since head no longer points to NULL, the else part of the if statement executes: 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; } • nodePtr is already at the end of the list, so the while loop immediately terminates. • The last statement, nodePtr->next = newNode; causes nodePtr->next to point to the new node. • This inserts newNode at the end of the list.
  • 16. 16 The third time appendNode is called, 12.6 is passed as the argument. Once again, the first three statements create a node with the argument stored in the value member. THIRD FUNCTION CALL next, the else part of the if statement executes. As before, nodePtr is made to point to the same node as head.
  • 17. 17 Since nodePtr->next is not NULL, the while loop will execute. After its first iteration, nodePtr will point to the second node in the list. The while loop's conditional test will fail after the first iteration because nodePtr->next now points to NULL. The last statement, nodePtr->next = newNode; causes nodePtr->next to point to the new node. This inserts newNode at the end of the list The figure above depicts the final state of the linked list.
  • 18. Traversing the List • The displayList member function traverses the list, displaying the value member of each node. The following pseudocode represents the algorithm. The C++ code for the member function follows on the next slide. 18 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.
  • 19. Code Segment displayList 19 void FloatList::displayList(void) { ListNode *nodePtr; nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } }
  • 20. Program: 2 20 // 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(); } 2.5 7.9 12.6 Output:
  • 21. 21 Inserting a Node • Using the listNode structure again, the pseudocode on the next slide shows an algorithm for finding a new node’s proper position in the list and inserting there. • The algorithm assumes the nodes in the list are already in order. 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.
  • 22. 22 The code for the traversal algorithm is shown below. (As before, num holds the value being inserted into the list.) // 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; } The entire insertNode function begins on the next slide.
  • 23. 23 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; } Continued on next slide…
  • 24. 24 // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode-> = nodePtr; } else { previousNode->next = newNode; newNode->next = nodePtr; } } } Continued from previous slide.
  • 25. Program: 3 25 // This program calls the displayList member function. // The function traverses the linked list displaying the value stored in each node. #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(); } 2.5 7.9 10.5 12.6 Output:
  • 26. 26 In insertNode, a new node is created and the function argument is copied to its value member. Since the list already has nodes stored in it, the else part of the if statement will execute. It begins by assigning nodePtr to head. Stepping Through the Program Since nodePtr is not NULL and nodePtr->value is less than num, the while loop will iterate. During the iteration, previousNode will be made to point to the node that nodePtr is pointing to. nodePtr will then be advanced to point to the next node.
  • 27. 27 Once again, the loop performs its test. Since nodePtr is not NULL and nodePtr->value is less than num, the loop will iterate a second time. During the second iteration, both previousNode and nodePtr are advanced by one node in the list. This time, the loop's test will fail because nodePtr is not less than num. The statements after the loop will execute, which cause previousNode->next to point to newNode, and newNode->next to point to nodePtr. If you follow the links, from the head pointer to the NULL, you will see that the nodes are stored in the order of their value members.
  • 28. 28 Deleting a Node • Deleting a node from a linked list requires two steps: • Remove the node from the list without breaking the links created by the next pointers • Deleting the node from memory • The deleteNode function begins on the next slide.
  • 29. 29 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; } Continued on next slide…
  • 30. 30 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; } } Continued from previous slide.
  • 31. Program: 4 31 // 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; Continued on next slide…
  • 32. 32 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(); } Continued from previous slide. 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.
  • 33. 33 Look at the else part of the second if statement. This is where the function will perform its action since the list is not empty, and the first node does not contain the value 7.9. Just like insertNode, this function uses nodePtr and previousNode to traverse the list. The while loop terminates when the value 7.9 is located. At this point, the list and the other pointers will be in the state depicted in the figure below. Stepping Through the Program
  • 34. 34 next, the following statement executes. previousNode->next = nodePtr->next; The statement above causes the links in the list to bypass the node that nodePtr points to. Although the node still exists in memory, this removes it from the list. The last statement uses the delete operator to complete the total deletion of the node.
  • 35. 35 Destroying the List • The class's destructor should release all the memory used by the list. • It does so by stepping through the list, deleting each node one-by- one. FloatList::~FloatList(void) { ListNode *nodePtr, *nextNode; nodePtr = head; while (nodePtr != NULL) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; } } Notice the use of nextNode instead of previousNode. The nextNode pointer is used to hold the position of the next node in the list, so it will be available after the node pointed to by nodePtr is deleted.
  • 36. Assignment: Variations of the Linked List 36 The Doubly-Linked List The Circular Linked List • Implementation of these two variations in C++ using OOP • Submit softcopy of your codes on m.hammad.wasim@gmail.com before Wednesday, Jan, 07. 3:00 PM