| | |
Using a class to add/delete/show numbers in a Link List
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
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
Here is my implemtation file which define the functions in the class:
Finally here is my Main CPP file
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)
//--------------------------------------------------------------------------- #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:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
//--------------------------------------------------------------------------- #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; } //---------------------------------------------------------------------------
>Node *newPtr = new Node ;
You're trying to use a constructor with no arguments, yet your Node is declared as:
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:
You're trying to use a constructor with no arguments, yet your Node is declared as:
C++ Syntax (Toggle Plain Text)
struct Node { Item item; Node *next; Node( Item itm, Node * nxt ) : item(itm), next(nxt) { } };
>Any suggestions ?
Be prepared for a good time trying to get this program to work. :mrgreen:
I'm here to prove you wrong.
>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.
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)
#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 ); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Binary Tree with Link List (C++)
- Link list Query (C)
- Link List Sorting Problem (C++)
- Link List Insert problems (C++)
- how to reverse link list using recurtion (C)
Other Threads in the C++ Forum
- Previous Thread: void functions 2
- Next Thread: stuck with hangman
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






