944,173 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3493
  • C++ RSS
Apr 2nd, 2005
0

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

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. //---------------------------------------------------------------------------
  2. #ifndef RcrLnkListH
  3. #define RcrLnkListH
  4. //---------------------------------------------------------------------------
  5. typedef int Item;
  6.  
  7. class List
  8. {
  9. public:
  10. List();// head(0) { };
  11. ~List();
  12.  
  13. bool IsEmpty();
  14.  
  15. void Show();
  16. bool Insert( Item item );
  17. //{ return Insert( head, item ); }
  18. //bool Delete( Item item );
  19. //{ return Delete( head, item ); }*/
  20.  
  21. private:
  22. struct Node
  23. {
  24. Item item;
  25. Node *next;
  26.  
  27. Node( Item itm, Node * nxt )
  28. : item(itm), next(nxt) { }
  29. };
  30.  
  31. Node * head;
  32.  
  33. void Show( Node * node );
  34. bool Insert( Node * &node, Item item );
  35. /*bool Delete( Node * & node, Item item );*/
  36. };
  37. //---------------------------------------------------------------------------
  38. #endif

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #pragma hdrstop
  5. #include "RcrLnkListH.h"
  6. using namespace std;
  7.  
  8.  
  9. List:: List(): head(0) { };
  10.  
  11.  
  12.  
  13. List:: ~List()
  14. {
  15. }
  16.  
  17.  
  18. bool List:: IsEmpty()
  19. {
  20. return head ==0;
  21. }
  22.  
  23.  
  24. void List:: Show()
  25. {
  26. void Show( Node * node );
  27.  
  28. }
  29.  
  30.  
  31. void List::Show(Node *node)
  32. {
  33. if (node != NULL)
  34. cout<<node->item;
  35. Show(node->next);
  36.  
  37. }
  38.  
  39. bool List:: Insert( Item item )
  40. {
  41.  
  42. bool Insert( Node * &node, Item item );
  43. return Insert( head, item ); }
  44.  
  45.  
  46. bool List:: Insert( Node * &node, Item item )
  47. {
  48. if ((node == NULL) || (item < node->item))
  49. {
  50.  
  51. Node *newPtr = new Node ;
  52. if (newPtr == NULL)
  53. {}
  54. else
  55. {
  56. newPtr->item = item;
  57. newPtr->next = node;
  58. node = newPtr;
  59. }
  60. }
  61. else Insert(node->next, item);
  62. return Insert( head, item );
  63. }
  64.  
  65. /*bool Delete(Node * & node, Item item)
  66.   { return Delete( head, item ); }*/


Finally here is my Main CPP file
C++ Syntax (Toggle Plain Text)
  1.  
  2. //---------------------------------------------------------------------------
  3. #include <iostream>
  4. #pragma hdrstop
  5. #include "RcrLnkListH.h"
  6. using namespace std;
  7. //---------------------------------------------------------------------------
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. List list;
  12.  
  13. if (list.IsEmpty == 0)
  14. {
  15. cout<<"not";
  16. }
  17. else
  18. cout<<"is empty";
  19. list.Show();
  20.  
  21. list.Insert(3);
  22. //list.Insert(1);
  23. //list.Insert(2);
  24. //list.Insert(5);
  25. // list.Insert(4);
  26.  
  27. list.Show();
  28.  
  29. // list.Delete(3); list.Show();
  30. // list.Delete(1); list.Show();
  31. // list.Delete(5); list.Show();*/
  32.  
  33. cin.get();
  34. return 0;
  35. }
  36. //---------------------------------------------------------------------------
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsxixtxhx is offline Offline
4 posts
since Feb 2005
Apr 2nd, 2005
0

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:
C++ Syntax (Toggle Plain Text)
  1. struct Node
  2. {
  3. Item item;
  4. Node *next;
  5.  
  6. Node( Item itm, Node * nxt )
  7. : item(itm), next(nxt) { }
  8. };
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:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2005
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xsxixtxhx is offline Offline
4 posts
since Feb 2005
Apr 2nd, 2005
0

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.
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct node {
  7. int data;
  8. node *next;
  9.  
  10. node ( int init, node *link )
  11. : data ( init ), next ( link )
  12. {}
  13. };
  14.  
  15. node *insert ( node *list, int data )
  16. {
  17. if ( list == 0 ) { // Empty list
  18. list = new node ( data, 0 );
  19. } else if ( data < list->data ) { // New head
  20. list = new node ( data, list );
  21. } else { // Everywhere else
  22. node *it = list;
  23.  
  24. while ( it->next != 0 && data > it->next->data )
  25. it = it->next;
  26.  
  27. it->next = new node ( data, it->next );
  28. }
  29.  
  30. return list;
  31. }
  32.  
  33. void print ( node *list )
  34. {
  35. while ( list != 0 ) {
  36. cout<< list->data <<' ';
  37. list = list->next;
  38. }
  39.  
  40. cout<<endl;
  41. }
  42.  
  43. int main()
  44. {
  45. node *list = 0;
  46.  
  47. for ( int i = 0; i < 10; i++ )
  48. list = insert ( list, rand() % 100 );
  49.  
  50. print ( list );
  51. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: void functions 2
Next Thread in C++ Forum Timeline: stuck with hangman





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC