Hi,
I'm trying to code a simple list, however I seem to have problems with the functions insert, del and print.
Since my insert function ends up in a segmentation fault, I can't test del and print(which are probably wrong anyway).
So what am I doing wrong in insert?
Code:#include <iostream> using namespace std; class List{ struct element{ double value; element* next; }; private: unsigned int length; element* data; public: List(); ~List(); void insert(const double& a); void del(); void print(); }; List::List(){ length=0; data= NULL; } List::~List(){ for(int i=length; i!=0;--i) delete data->next; delete data; } // insert element at position 1 void List::insert(const double& a){ length++; element* newnext=data; data->value=a; data->next=newnext; /*???*/ } // delete element at position 1 void List::del(){ if(data !=NULL){ length--; /*TODO*/ } else cout << "<<No element deleted, List empty>>" << endl; } // print list void List::print(){ if (data !=NULL) for(int i=0; i!=length; ++i){ cout << data->value << endl; data=data->next; /*???*/ } else cout << "<<List is empty>>" << endl; } int main(){ cout << "Liste\n"; int n; List list; do{ cout << "Choose an option" << endl; cout << "1.Insert Element\n2.Delete Element\n3.Print Elements\n0.Abort"<< endl; cin >> n; switch(n){ case 0: cout << "End"<< endl; break; case 1: double x; cout << "Enter a value: "; cin >> x; list.insert(x); break; case 2: list.del(); break; case 3: list.print(); break; default: cout << "Invalid Choice"<< endl; break; } }while(n!=0); }