SP23 - CSC211 - Data Structures & Algorithms - W02
SP23 - CSC211 - Data Structures & Algorithms - W02
1
Starting Out with C++, 3rd Edition
17.1 Introduction to the Linked
List ADT
• 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
Starting Out with C++, 3rd Edition
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.
3
Starting Out with C++, 3rd Edition
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
Starting Out with C++, 3rd Edition
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
Starting Out with C++, 3rd Edition
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:
struct ListNode
{
float value;
struct ListNode *next;
};
6
Starting Out with C++, 3rd Edition
Declarations
• The next step is to declare a pointer to serve
as the list head, as shown below.
ListNode *head;
8
Starting Out with C++, 3rd Edition
class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
float value;
struct ListNode *next;
};
9
Starting Out with C++, 3rd Edition
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.
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.
10
Starting Out with C++, 3rd Edition
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;
void main(void)
{
FloatList List;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
}
(This program displays no output.)
12
Starting Out with C++, 3rd Edition
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
Starting Out with C++, 3rd Edition
newNode = new ListNode;
newNode->value = num;
newNode->next = nULL;
14
Starting Out with C++, 3rd Edition
The next statement to execute is the following if statement.
if (!head)
head = newNode;
15
Starting Out with C++, 3rd Edition
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.
16
Starting Out with C++, 3rd Edition
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;
}
17
Starting Out with C++, 3rd Edition
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.
18
Starting Out with C++, 3rd Edition
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.
19
Starting Out with C++, 3rd Edition
next, the else part of the if statement executes. As before,
nodePtr is made to point to the same node as head.
20
Starting Out with C++, 3rd Edition
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.
21
Starting Out with C++, 3rd Edition
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.
22
Starting Out with C++, 3rd Edition
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.
23
Starting Out with C++, 3rd Edition
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
24
Starting Out with C++, 3rd Edition
Program 17-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();
}
25
Starting Out with C++, 3rd Edition
Program 17-2 Output
2.5
7.9
12.6
26
Starting Out with C++, 3rd Edition
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.
27
Starting Out with C++, 3rd Edition
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.
28
Starting Out with C++, 3rd Edition
The code for the traversal algorithm is shown below. (As before, num
holds the value being inserted into the list.)
29
Starting Out with C++, 3rd Edition
void FloatList::insertNode(float num)
{
ListNode *newNode, *nodePtr, *previousNode;
31
Starting Out with C++, 3rd Edition
Program 17-3
// 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;
2.5
7.9
10.5
12.6
33
Starting Out with C++, 3rd Edition
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.
34
Starting Out with C++, 3rd Edition
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.
35
Starting Out with C++, 3rd Edition
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.
36
Starting Out with C++, 3rd Edition
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.
37
Starting Out with C++, 3rd Edition
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.
38
Starting Out with C++, 3rd Edition
void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;
39
Starting Out with C++, 3rd Edition
Continued from previous slide.
else
{
// Initialize nodePtr to head of list
nodePtr = head;
40
Starting Out with C++, 3rd Edition
Program 17-4
// This program demonstrates the deleteNode member function
#include <iostream.h>
#include "FloatList.h“
void main(void)
{
FloatList list;
41
Starting Out with C++, 3rd Edition
Continued from previous slide.
42
Starting Out with C++, 3rd Edition
Program Output
43
Starting Out with C++, 3rd Edition
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.
44
Starting Out with C++, 3rd Edition
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.
45
Starting Out with C++, 3rd Edition
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. The code is
shown on the next slide.
46
Starting Out with C++, 3rd Edition
FloatList::~FloatList(void)
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
50
Starting Out with C++, 3rd Edition
// DisplayList shows the value
// stored in each node of the linked list
// pointed to by head.
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
51
Starting Out with C++, 3rd Edition
// The insertNode function inserts a node with
// num copied to its value member.
template <class T>
void LinkedList<T>::insertNode(T num)
{
ListNode *newNode, *nodePtr, *previousNode;
52
Starting Out with C++, 3rd Edition
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
53
Starting Out with C++, 3rd Edition
// The deleteNode function searches for a node
// with Num as its value. The node, if found, is
// deleted from the list and from memory.
54
Starting Out with C++, 3rd Edition
else
{
// Initialize nodePtr to head of list
nodePtr = head;
55
Starting Out with C++, 3rd Edition
// Destructor
// This function deletes every node in the list.
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
#endif
56
Starting Out with C++, 3rd Edition
Program 17-5
// This program demonstrates the linked list template.
#include <iostream.h>
#include "LinkedList.h“
void main(void)
{
LinkedList<int> list;
57
Starting Out with C++, 3rd Edition
cout << "Now inserting the value 5.\n";
list.insertNode(5);
cout << "Here are the nodes now.\n";
list.displayList();
cout << endl;
58
Starting Out with C++, 3rd Edition
Program Output
Here are the initial values:
2
4
6
59
Starting Out with C++, 3rd Edition
17.4 Variations of the Linked List
60
Starting Out with C++, 3rd Edition
17.4 Variations of the Linked List
61
Starting Out with C++, 3rd Edition