prog- Linked List

advertisement
Some operation on Linked List
In this section we develop some simple list classes. The
first of these, which we call NumberList, will store values of
type double. It is based on the ListNode structure defined in the
preceding section, and is shown here.
Contents of NumberList.h
1 #include <iostream>
2 using namespace std;
3 class NumberList
4 {
5 protected:
6
// Declare a class for the list node.
7
struct ListNode
8
{
9
double value;
10
ListNode *next;
11
ListNode(double value1, ListNode *next1 = NULL)
12
{
13
value = value1;
14
next = next1;
15
}
16
};
17 ListNode *head; // List head pointer
18 public:
19
NumberList() { head = NULL; } //Constructor
20
~NumberList(); // Destructor
21
void add(double number);
22
void remove(double number);
23
void displayList();
24 };
Because ListNode does not need to be accessed by any code
outside of NumberList, we have declared it inside the NumberList
class. We have also declared ListNode in a protected section to
make it accessible to classes that may later be derived from
NumberList.
Notice that the constructor initializes the head pointer to
NULL, thereby indicating that the list starts out empty.
The class has an add function that takes a value and adds
it to the end of the list, as well as a displayList function that
prints to the screen all values stored in the list.
Some operation on Linked List
A destructor function destroys the list by deleting all its
nodes.
With the exception of remove(), all of these functions are
defined in NumberList.cpp. The remove() function will be added
later.
Contents of NumberList.cpp
1 #include "NumberList.h"
2 using namespace std;
3
4 //*****************************************************
5 // add adds a new element to the end of the list. *
6 //*****************************************************
7 void NumberList::add(double number)
8 {
9 if (head == NULL)
10 head = new ListNode(number);
11 else
12 {
13
// The list is not empty.
14
// Use nodePtr to traverse the list
15
ListNode *nodePtr = head;
16
while (nodePtr->next != NULL)
17
nodePtr = nodePtr->next;
18
19
// nodePtr->next is NULL so nodePtr points to the last node.
20
// Create a new node and put it after the last node.
21
nodePtr->next = new ListNode(number);
22 }
23 }
24
25 //***************************************************
26 // displayList outputs a sequence of all values *
27 // currently stored in the list. *
28 //***************************************************
29 void NumberList::displayList()
30 {
31 ListNode *nodePtr = head; // Start at head of list
32 while (nodePtr)
33
{
34
// Print the value in the current node
35
cout << nodePtr->value << " ";
36
// Move on to the next node
37
nodePtr = nodePtr->next;
38
}
39 }
Some operation on Linked List
40
41 //******************************************************
42 // Destructor deallocates the memory used by the list. *
43 //******************************************************
44 NumberList::~NumberList()
45 {
46
ListNode *nodePtr = head; // Start at head of list
47
while (nodePtr != NULL)
48
{
49
// garbage keeps track of node to be deleted
50
ListNode *garbage = nodePtr;
51
// Move on to the next node, if any
52
nodePtr = nodePtr->next;
53
// Delete the "garbage" node
54
delete garbage;
55
}
56 }
Program 17-3
1 // This program demonstrates the add and
2 // display linked list operations.
3
4 #include "Numberlist.h"
5 using namespace std;
6
7 int main()
8 {
9
NumberList list;
10 list.add(2.5);
11 list.add(7.9);
12 list.add(12.6);
13 list.displayList();
14 cout << endl;
15 return 0;
16 }
Program Output
2.5 7.9 12.6
Some operation on Linked List
Linked Lists in Sorted Order
Consider a class SortedNumberList that maintains its
elements in ascending order. It is similar to the NumberList
class, except the add function is modified so that it keeps the
list in sorted order when placing new elements. Because a
sorted list is still a list, it makes senseto use inheritance
and derive it from NumberList.
Inserting a Node into a Sorted List
The entire function, including the code for creating a new
node and inserting it at the point just after previousNodePtr but
before nodePtr, is given here:
Contents of SortedNumberList.h
1 #include "SortedNumberList.h"
2
3 //*********************************************
4 // Adds a number to the sorted list. *
5 // This function overrides add in NumberList. *
6 //*********************************************
7 void SortedNumberList::add(double number)
8 {
9
ListNode *nodePtr, *previousNodePtr;
10
11
if (head == NULL || head->value >= number)
12
{
13
// A new node goes at the beginning of the list.
14
head = new ListNode(number, head);
15
}
16
else
17
{
18
previousNodePtr = head;
19
nodePtr = head->next;
20
21
// Find the insertion point.
22
while (nodePtr != NULL && nodePtr->value < number)
23
{
24
previousNodePtr = nodePtr;
25
nodePtr = nodePtr->next;
26
}
27
// Insert the new node just before nodePtr.
28
previousNodePtr->next = new ListNode(number, nodePtr);
29
}
30 }
Some operation on Linked List
Here is a program that uses the add function. A discussion of
how the function works follows the program
Program 17-4
1 // This program illustrates the NumberList append,
2 // insert, and displayList member functions.
3 #include "SortedNumberList.h"
4
5 int main()
6{
7 SortedNumberList list;
8
9 // Add elements in order
10 list.add(2.5);
11 list.add(7.9);
12 list.add(12.6);
13 // Add a value that should go in the middle of the list
14 list.add(10.5);
15 // Display the list.
16 list.displayList();
17 cout << endl;
18 return 0;
19 }
Program Output
2.5 7.9 10.5 12.6
Removing an Element
Removing an element from a linked list requires a number of
steps:
 Locating the node containing the element to be removed.
 Unhooking the node from the list.
 Deleting the memory allocated to the node.
The remove member function uses a pointer nodePtr to search for a
node containing the value number that is to be removed. During
this process, a second pointer previousNodePtr trails behind
nodePtr, always pointing to the node preceding the one pointed
to by nodePtr. When nodePtr points to the node to be deleted,
previousNodePtr->next is set to nodePtr->next. This causes the
successor pointers in the list to bypass the node containing
number, allowing its memory to be freed using delete. The entire
function is shown here:
Some operation on Linked List
25 //**********************************************
26 // Removes a number from a list. The function *
27 // does not assume that the list is sorted. *
28 //**********************************************
29 void NumberList::remove(double number)
30 {
31
ListNode *nodePtr, *previousNodePtr;
32
33
// If the list is empty, do nothing.
34
if (!head) return;
35
36
// Determine if the first node is the one to delete.
37
if (head->value == number)
38
{
39
nodePtr = head;
40
head = head->next;
41
delete nodePtr;
42
}
43
else
44
{
45
// Initialize nodePtr to the head of the list.
46
nodePtr = head;
47
48
// Skip nodes whose value member is not number
49
while (nodePtr != NULL && nodePtr->value != number)
50
{
51
previousNodePtr = nodePtr;
52
nodePtr = nodePtr->next;
53
}
54
// Link the previous node to the node after
55
// nodePtr, then delete nodePtr.
56
if (nodePtr)
57
{
58
previousNodePtr->next = nodePtr->next;
59
delete nodePtr;
60
}
61
}
62 }
Notice that the remove() function is a member of NumberList
rather than SortedNumber-List. Unlike add(), the remove() function
works with both sorted and unsorted lists, and so does not have
to be overridden. Program 17-5 demonstrates this new function
Some operation on Linked List
by first building a list of three values and then removing the
values one by one.
Program 17-5
1 // This program demonstrates the remove member function.
2 #include "NumberList.h"
3 using namespace std;
4
5 int main()
6 {
7
NumberList list;
8
9
// Build the list.
10 list.add(2.5);
11 list.add(7.9);
12 list.add(12.6);
13
14 // Display the list.
15 cout << "Here are the initial values:\n";
16 list.displayList();
17 cout << "\n\n";
18
19 // Demonstrate the remove function.
20 cout << "Now removing the value in the middle.\n";
21 list.remove(7.9);
22 cout << "Here are the values left.\n";
23 list.displayList();
24 cout << "\n\n";
25
26 cout << "Now removing the last value.\n";
27 list.remove(12.6);
28 cout << "Here are the values left.\n";
29 list.displayList();
30 cout << "\n\n";
31
32 cout << "Now removing the only remaining value.\n";
33 list.remove(2.5);
34 cout << "Here are the values left.\n";
35 list.displayList();
36 cout << endl;
37
38 return 0;
39 }
Some operation on Linked List
Program Output
Here are the initial values:
2.5 7.9 12.6
Now removing the value in the middle.
Here are the values left.
2.5 12.6
Now removing the last value.
Here are the values left.
2.5
Now removing the only remaining value.
Here are the values left.
Download