Search code examples
c++nodesdoubly-linked-list

doubly linked list, i can seem to create a new Node, How far off am i?


the program is suppose to be a doubly linked list that prompts the user to either

1.Insert"
"2. Delete"<<endl<<
"3. Display"<<endl<<
"4. Sum"<<endl<<
"5. Average"<<endl<<
"6. Exit"
From the list above what would you like to do next?"

problem: at execution, after i enter a double for "value", the program freeze for a little bit then exits

//-- input prompt

cout<<"\nWhat value would you like to insert to the list: ";
cin>>value;
l.insertAtEnd(value);
break;

//--- function definition ---

void List::insertAtEnd(double & x)
    {   
        List::NodePointer ptr;

        ptr = new List::Node(last, x );
        last->next = ptr;
        last = ptr;
        count ++;
        sum += x;
    }

//--- prototypes

include

    // #include"Node.h" 
>     
>     using namespace std;
>     
>     #ifndef LIST
>     #define LIST
>     
>     typedef double ElementType;
>     
>     class List
>     {
>         public:
>             ...
>             void insertAtEnd(ElementType & x); //insert a value x on the end of the list
>             ...
>         
>     private:
>             ...
>             
>             class Node
>             {
>                 public:
>                 ElementType data;
>                 Node *prev;
>                 Node *next;
>                 //--- Node constructor
>                 /*-------------------------------------------------------------------
>                     Precondition:  None.
>                     Postcondition: A Node has been constructed with value in its data 
>                         part and its next part set to link (default 0).
>                     -------------------------------------------------------------------*/
>                 Node(Node *prevNodePtr,ElementType value, Node *link = 0)
>     
>                     : prev(prevNodePtr) ,data(value), next(link)
>                 {}
>             };
>     
>             typedef Node * NodePointer;
>             NodePointer last;
>             NodePointer first;
>     
>     };
>     
>     ostream & operator<<(ostream & out, const List & s);
>     
>     #endif // !LIST

please help...


Solution

  • I don't think you are far off. The main problem I see is that you don't deal with the first pointer in your List class.

    When you are adding a value to the end of a list like this the first value to be added to the list is a special case because you must also update the first pointer. Your code doesn't do this.

    The other unusual thing about your code is that you have those count and sum variables. It seems like you are trying to keep a running total of the list length and list sum. That's not wrong but it's an unusual thing to do. Normally you would calculate those when needed, not all the time. Also if you do keep doing things that way then you need to add count and sum to your list class. At the moment you have a list in one variable and it's length and sum in different variables. That's bad design because there's no close association between the variables. Imagine you had two lists. How would you keep the count and sum variables updated then? Personally I would just remove the code dealing with count and sum, just calculate these quantities when you need them.