1

Topic: Lists

The job: Implement and test function:

template <class T>
void InsertOrder (Node <T> * &head, T item);

Creating a node-object of class Node with given item and interposing it in the chained list c the pointer on a head head with maintenance of the order of nodes (on increase data).
Shows an error in line: InsertOrder (p, 3);
How correctly to write?
The program code:

#include <iostream>
/* run this program using the console pauser or add your own getch, system ("pause") or input loop */
using namespace std;
#include "nodelib.h"
template <class T>
void InsertOrder (Node <T> * AND head, T& item)
{
Node <T> *currPtr, *prevPtr, *newNode;
prevPtr = NULL;
currPtr = head;
while (currPtr! = NULL)
{
if (item <currPtr-> data)
break;
prevPtr = currPtr;
currPtr = currPtr-> NextNode ();
}
if (prevPtr == NULL)
InsertFront (head, item);
else
{
newNode = GetNode (item);
prevPtr-> InsertAfter (newNode);
}
}
int main (int argc, char ** argv)
{
int M [] = {1,2,3,4,5};
Node <int> *p=BuildNodeList (M, 5);
InsertOrder (p, 3);
PrintNodeList (p);
return 0;
}

2

Re: Lists

void InsertOrder (Node <T> * AND head, >>>T&<<< item)
...
InsertOrder (p,>>> 3 <<<);

Selected - it is incompatible.
It is necessary to you const T&

3

Re: Lists

Anatoly Moskovsky, so an error on

InsertFront (head, item);

produces

#include <iostream>
/* run this program using the console pauser or add your own getch, system ("pause") or input loop */
using namespace std;
#include "nodelib.h"
template <class T>
void InsertOrder (Node <T> * AND head, const T& item)
{
Node <T> *currPtr, *prevPtr, *newNode;
prevPtr = NULL;
currPtr = head;
while (currPtr! = NULL)
{
if (item <currPtr-> data)
break;
prevPtr = currPtr;
currPtr = currPtr-> NextNode ();
}
if (prevPtr == NULL)
InsertFront (head, item);
else
{
newNode = GetNode (item);
prevPtr-> InsertAfter (newNode);
}
}
int main (int argc, char ** argv)
{
int M [] = {1,2,3,4,5};
Node <int> *p=BuildNodeList (M, 5);
InsertOrder (p, 3);
PrintNodeList (p);
return 0;
}

4

Re: Lists

Vvvv111;
the type recursively will not be compiled yet