Simple list-problem

This is a discussion on Simple list-problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to code a simple list, however I seem to have problems with the functions insert, del and ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    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);
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 01-17-2007 at 05:41 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    28
    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?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    28
    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);
    }
    Last edited by pjeremy; 01-18-2007 at 08:18 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    28
    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?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The current version of del() will only delete the first node of the list. If that's the purpose of del(), fine. If del() is supposed to allow the user to delete the entire list on demand rather than waiting for the list to go out of scope and have the destructor do it, then using a loop in del() to delete all the nodes of the list instead of an if/else statement would be needed. If del() is supposed to delete any given node() from the list, that's something different, too.

    If the user doesn't delete all the nodes in the list before the list goes out of scope, then the destructor should do it. As written the destructor is only deleting the first node in the list, not the entire list. Use a loop to make sure all the nodes in the list get deleted, not just the first one.
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    28
    If I understood you correctly, the class List destructor should look like this, right?
    Code:
    		~List(){
    			ListElement *dest;
    				while (data->getNext() !=NULL){
    					dest=data->getNext();
    					data->setNext(NULL);
    					delete data;
    					data=dest;
    				}
    			delete dest;
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21