>Node *newPtr = new Node ;
You're trying to use a constructor with no arguments, yet your Node is declared as:
struct Node
{
Item item;
Node *next;
Node( Item itm, Node * nxt )
: item(itm), next(nxt) { }
};
Notice how the only constructor declared takestwo arguments. If you declare any constructors then the default constructor is not created automagically for you.
>Any suggestions ?
Be prepared for a good time trying to get this program to work. :mrgreen:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>does anyone have any hits on how i can redo the implemenation of Insert to get this to work ?
Don't use recursion. Not only does it complicate a simple algorithm, it's potentially dangerous for long lists. I generally prefer to avoid recursion for linear data structures. If you can't efficiently divide the structure in half then that's an indication that recursion may not be the best solution.
#include <cstdlib>
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
node ( int init, node *link )
: data ( init ), next ( link )
{}
};
node *insert ( node *list, int data )
{
if ( list == 0 ) { // Empty list
list = new node ( data, 0 );
} else if ( data < list->data ) { // New head
list = new node ( data, list );
} else { // Everywhere else
node *it = list;
while ( it->next != 0 && data > it->next->data )
it = it->next;
it->next = new node ( data, it->next );
}
return list;
}
void print ( node *list )
{
while ( list != 0 ) {
cout<< list->data <<' ';
list = list->next;
}
cout<<endl;
}
int main()
{
node *list = 0;
for ( int i = 0; i < 10; i++ )
list = insert ( list, rand() % 100 );
print ( list );
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401