DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Using a class to add/delete/show numbers in a Link List (http://www.daniweb.com/forums/thread21299.html)

xsxixtxhx Apr 1st, 2005 11:39 pm
Using a class to add/delete/show numbers in a Link List
 
I have this program that is supposed to take in numbers from the Main cpp file and add them to the link list. Eventually i have to sort them then display them on the screen. For right now i am just trying to insert them to the link list but i keep getting a complie error. I am trying to compile this program and i get the following error "'[HTML]Node' : no appropriate default constructor available[/HTML]" Any suggestions ?

I have attached a link to the files in case it is easier for you to view it that way
Project 3

Thanks

Here is my .h file which creates the file currently i am trying to get the Insert Fuction to work so i have the other set as commetns
 //---------------------------------------------------------------------------
#ifndef RcrLnkListH
#define RcrLnkListH
//---------------------------------------------------------------------------
typedef int Item;

class List
{
public:
  List();// head(0) { };
  ~List();

  bool IsEmpty();

  void Show();
  bool Insert( Item item );
      //{ return Insert( head, item ); }
  //bool Delete( Item item ); 
      //{ return Delete( head, item ); }*/

private:
  struct Node
  {
      Item item;
      Node *next;

      Node( Item itm, Node * nxt )
                  : item(itm), next(nxt) { }
  };

  Node * head;

  void Show( Node * node );
  bool Insert( Node * &node, Item item );
  /*bool Delete( Node * & node, Item item );*/
};
//---------------------------------------------------------------------------
#endif

Here is my implemtation file which define the functions in the class:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#pragma hdrstop
#include "RcrLnkListH.h"
using namespace std;


List:: List(): head(0) { };


 
List:: ~List()
{
}


bool List:: IsEmpty()
{
        return head ==0;
}
 

void List:: Show()
{
void Show( Node * node );

}


void List::Show(Node *node)
{
if (node != NULL)
                cout<<node->item;
                Show(node->next);

}

bool List:: Insert( Item item )
      {
               
                bool Insert( Node * &node, Item item );
                return Insert( head, item ); }

         
bool List:: Insert( Node * &node, Item item )
{
        if ((node == NULL) || (item < node->item))
        {
               
                Node *newPtr = new Node ;
                if (newPtr == NULL)
                {}
                else
                {
                        newPtr->item = item;
                        newPtr->next = node;
                        node = newPtr;
                }
        }
        else Insert(node->next, item);
return Insert( head, item );
}
 
/*bool Delete(Node * & node, Item item) 
      { return Delete( head, item ); }*/


Finally here is my Main CPP file

//---------------------------------------------------------------------------
#include <iostream>
#pragma hdrstop
#include "RcrLnkListH.h"
using namespace std;
//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
  List list;

  if (list.IsEmpty == 0)
  {
          cout<<"not";
  }
  else
          cout<<"is empty";
  list.Show();

  list.Insert(3);
  //list.Insert(1);
  //list.Insert(2);
  //list.Insert(5);
  // list.Insert(4);

  list.Show();

  // list.Delete(3);  list.Show();
  // list.Delete(1);  list.Show();
  // list.Delete(5);  list.Show();*/

  cin.get();
  return 0;
}
//---------------------------------------------------------------------------

Narue Apr 2nd, 2005 10:09 am
Re: Using a class to add/delete/show numbers in a Link List
 
>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 takes two 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:

xsxixtxhx Apr 2nd, 2005 7:13 pm
Re: Using a class to add/delete/show numbers in a Link List
 
does anyone have any hits on how i can redo the implemenation of Insert to get this to work ?

Thanks

Narue Apr 2nd, 2005 7:31 pm
Re: Using a class to add/delete/show numbers in a Link List
 
>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 );
}


All times are GMT -4. The time now is 9:35 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC