User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,198 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,614 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2185 | Replies: 3
Reply
Join Date: Feb 2005
Posts: 4
Reputation: xsxixtxhx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xsxixtxhx xsxixtxhx is offline Offline
Newbie Poster

Using a class to add/delete/show numbers in a Link List

  #1  
Apr 1st, 2005
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;
}
//---------------------------------------------------------------------------
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,005
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 409
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Using a class to add/delete/show numbers in a Link List

  #2  
Apr 2nd, 2005
>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:
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Feb 2005
Posts: 4
Reputation: xsxixtxhx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xsxixtxhx xsxixtxhx is offline Offline
Newbie Poster

Re: Using a class to add/delete/show numbers in a Link List

  #3  
Apr 2nd, 2005
does anyone have any hits on how i can redo the implemenation of Insert to get this to work ?

Thanks
Reply With Quote  
Join Date: Sep 2004
Posts: 6,005
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 409
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Using a class to add/delete/show numbers in a Link List

  #4  
Apr 2nd, 2005
>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 );
}
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC