1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*(Printing a single linked list backward) Include the functions
reversePrint and recursiveReversePrint, as discussed in this chapter,
in the class linkedListType. Also, write a program function to print a
(single) linked list backward. (Use either the class unorderedLinkedList or
the class orderedLinkedList to test your function.)*/
#include "LinkedList.h"
#include "orderedLinkedList.h"
#include "linkedListIterator.h"
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
int main()
{
// list: 3, 12, 26, 34, 55
//should print: 55, 34, 26, 12, 3
//variable declarations page 1079
nodeType *first,*newNode;
int num;
//page 1080
first=NULL;
cin >>num; //read and store a new number in num
newNode = new nodeType; //Create the new node, newNode.
newNode->info = num; //Store the item in newNode
newNode->link = first; //Insert newNode before f i rst.
first = newNode; //Update the value of the pointer f i rst.
cin >> num ; //read the next number
//outputs the data stored in each node:
current = first;
while (current != NULL)
{
cout << current->info << " ";
current = current->link;
}
//buildListBackward
nodeType* buildListBackward()
{ //error expected ;
nodeType *first, *newNode;
int num;
cout << "Enter a list of integers ending with -999."
<< endl;
cin >> num;
first = NULL;
while (num != -999)
{
newNode = new nodeType; //create a node
newNode->info = num; //store the data in newNode
newNode->link = first; //put newNode at the beginning
//of the list
first = newNode; //update the head pointer of
//the list, that is, first
cin >> num; //read the next number
}
return first;
} //end buildListBackward
|