Lecture 4 Singly Linked List
Lecture 4 Singly Linked List
26/06/2023
Linked List Operations
► Insert at last
Deleting nodes
► Delete from beginning, middle, last
Traversing the list
Searching a specified item in the list
Destroying the list
head
a b c d
head = NULL;
head
}
Data Structures & Algorithms Lecture 4: Linked Lists
Linked list data 5
structure
•One of the attributes of a linked list is that there is not a physical
relationship between the nodes; that is, they are not stored contiguously in
•To determine the beginning of the list, and each additional element in the
structure
⚫The pointer to the first node in the list is referred to as the head
pointer, because it points to the head node in the list
⚫In addition to the head pointer, there are usually other pointers associated
with the list. These can include a pointer to the last element in the list (tail
pointer) and a pointer that traverses the list to find data (navigator or
traversal pointer)
two->value = 2;
public:
three->value = 3;
int value;
Node* next;
// Connect nodes
}; one->next = two;
two->next = three;
}; }
int main() {
insert(1);
struct ListNode
float value;
};
Data Structures & Algorithms Lecture 4: Linked Lists
Declaration
s
•The next step is to declare a pointer to serve as the list
head, as shown below.
ListNode *head;
list.
Data Structures & Algorithms Lecture 4: Linked Lists
Program 1
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
•
The first call to appendNode passes 2.5 as the
Else
public:
FloatList(void) { // Constructor
head = NULL;
}
~FloatList(void) { }; // Destructor
void appendNode(float);
void displayList(void);
void deleteNode(float);
};
Data Structures & Algorithms Lecture 4: Linked Lists
Linked List Example
//floatList.h
class FloatList {
private:
// Declare a structure for the list
struct ListNode {
float value;
struct ListNode *next;
};
public:
FloatList(void) // Constructor {
head = NULL;
}
~FloatList(void) { }; // Destructor
void appendNode(float);
void displayList(void);
void deleteNode(float);
};
Data Structures & Algorithms Lecture 4: Linked Lists
Appending a node to the list
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// 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
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
void main(void)
FloatList List;
list.appendNode(2.5); list.appendNode(7.9);
list.appendNode(12.6); list.displayList();
Data Structures & Algorithms Lecture 4: Linked Lists
Traversing the 27
List
• Algorithms that traverse a list start at the first node and examine each
node in succession until the last node has been processed
• Traversal logic is used by several different types of algorithms, such as
– changing a value in each node,
– printing the list,
– Performing an operation on a field in the list,
– or calculating the average of a field and etc…
• Any application that requires that the entire list be processed uses the
traversal
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
void main(void)
{
FloatList List; OUTPUT
list.appendNode(2.5); 2.5
list.appendNode(7.9); 7.9
list.appendNode(12.6); 12.6
list.displayList();
}
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
(As before, num holds the value being inserted into the list.)
nodePtr = head;
// than num.
ListNode;
newNode->value = num;
head = newNode;
newNode->next = NULL;
}
Thursday, October 31, 2024
Continued on next slide…
37
== NULL)
head = newNode;
newNode->next = nodePtr;
else
#include "FloatList.h”
10.5
// Build the list list.appendNode(2.5);
12.6
list.appendNode(7.9); list.appendNode(12.6);
// of &the
Data Structures list.
Algorithms Lecture 4: Linked Lists
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
head.
If you follow the links, from the head pointer to the NULL, you will see that the nodes
Thursday, October 31, 2024
Deleting a node
► Remove the node from the list without breaking the links
created by the next pointers
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;
}
}
From: http://www.tutorialspoint.com/data_structures_algorithms/data_structures_algorithms_tutorial.pdf
<iostream.h>
#include "FloatList.h“
void main(void)
FloatList list;
list.appendNode(12.6);
list.deleteNode(12.6); list.displayList();
list.deleteNode(2.5);
list.displayList();
Data Structures & Algorithms Lecture 4: Linked Lists
}
49
Program Output
Here are the initial values: 2.5
7.9
12.6
2.5
12.6
nodes left.
2.5
Data Structures & Algorithms Lecture 4: Linked Lists
50
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
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
The last statement uses the delete operator to complete the total deletion of the node.
FloatList::~FloatList(void)
nodePtr = head;
nodePtr = nextNode;
}
Thursday, October 31, 2024
}