Linked Lists
Linked Lists
Linked Lists
Lecture 3
Node objects
Data Fields
Link Fields
8
struct ListNode
{
float value;
struct ListNode *next;
};
10
Declarations
• The next step is to declare a pointer to serve as the list
head, as shown below.
ListNode *head;
{
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);
};
14
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.)
17
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;
}
22
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.
27
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
31
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();
}
32
2.5
7.9
12.6
33
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.
34
Inserting a Node
Program 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
40
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.
41
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.
43
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.
44
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.
45
void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;
Program 4
// This program demonstrates the deleteNode member function
#include <iostream.h>
#include "FloatList.h“
void main(void)
{
FloatList list;
Program Output
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.
51
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.
52
FloatList::~FloatList(void)
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
Questions