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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 4
Reputation: xsxixtxhx is an unknown quantity at this point 
Solved Threads: 0
xsxixtxhx xsxixtxhx is offline Offline
Newbie Poster

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

 
0
  #1
Apr 2nd, 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
  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:

  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
  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. //---------------------------------------------------------------------------
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Apr 2nd, 2005
>Node *newPtr = new Node ;
You're trying to use a constructor with no arguments, yet your Node is declared as:
  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:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: xsxixtxhx is an unknown quantity at this point 
Solved Threads: 0
xsxixtxhx xsxixtxhx is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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.
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC