# Simple list-problem

• 01-17-2007
pjeremy
Simple list-problem
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); }```
• 01-17-2007
7stud
Suppose I start your program and choose 1 to insert from the menu, and then enter 12.5. Your program will have these values:
Code:

```n=1 list: length=0, data = NULL x = 12.5```
Then, the insert() function is called, which executes these lines:
Code:

```list.length++;  //length = 1 element* newnext=list.data;  //newnext = NULL list.data->value=a;  ```
The last line tries to dereference a NULL pointer, i.e. get the value at the address NULL.
• 01-17-2007
pjeremy
So it should be
Code:

```void List::insert(const double& a){         length++;         element* newnext=data;         data =new element;         data->value=a;         data->next=newnext; }```
right?
• 01-18-2007
pjeremy
I changed my approach and so far it works, however I'm stuck with the delete function and could use some help.
The delete function should delete the first element in the list.

EDIT: Okay, the delete function works, now all I need is someone to look at the code and tell me if it's okay or if there are any problems. Thanks.

Code:

```#include <iostream> using namespace std; class List{         private:                 struct ListElement{                         private:                                 double elementvalue;                                 ListElement *next;                         public:                                 // Constructor                                 ListElement( double value = 0 ){                                         elementvalue = value;                                         next = NULL;                                 }                                 // Destructor                                 virtual ~ListElement(){delete next;}                                 double getData(){return elementvalue;}                                 void setNext(ListElement *newnext ){next = newnext;}                                 ListElement *getNext(){return next;}                                 virtual void tostream(ostream &ostr) const{                                         ostr << " " << elementvalue;                                         if( next==NULL ){ ostr << "." << endl;}                                         else{                                                 ostr << ",";                                                 next->tostream(ostr);                                         }                                 }                 }; // END struct ListElement //                 ListElement *data;         public:                 // Constructor                 List(){data = NULL;}                 // Destructor                 virtual ~List(){delete data;}                 void insert(const double& a){                         ListElement *newnext = data;                         data = new ListElement(a);                         data->setNext(newnext);                         cout << endl;                 }                 void del(void){                         if(data!=NULL){                                 ListElement *newnext = data->getNext();                                 data->setNext(NULL);                                 delete data;                                 data = newnext;                         }                         else cout << "\n<<List is empty, nothing to delete>>\n" << endl;                 }                 void tostream(ostream &ostr) const {                         ostr << "\nList: ";                         if(data==NULL) ostr << "<<List is empty>>" << endl;                         else data->tostream(ostr);                 } }; // END class List // ostream &operator<<(ostream &ostr, const List &list){     list.tostream(ostr);     return ostr; } int main(){         cout << "List\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 << "\nEnter a value: "; cin >> x; list.insert(x); break;                         case 2: list.del(); break;                         case 3: cout << list << endl; break;                         default: cout << "\n<<Invalid Choice>>"<< endl; break;                 }         }while(n!=0); }```
• 01-18-2007
vart
Quote:

Originally Posted by pjeremy
I changed my approach and so far it works, however I'm stuck with the delete function and could use some help.
The delete function should delete the first element in the list.

Save the pointer to the element pointed by data in the temp-pointer
move data to point next element in the list
delete the element pointed by the temp-pointer
• 01-18-2007
pjeremy
Hi vart,
thanks, I edited the post above and I think it's okay now, but could you tell me if there are any problems in the code that I'm not aware of?
• 01-18-2007
```                ~List(){                         ListElement *dest;                                 while (data->getNext() !=NULL){                                         dest=data->getNext();                                         data->setNext(NULL);                                         delete data;                                         data=dest;                                 }                         delete dest;                 }```