Question

I'm writing a program to perform various operations on a double,circular linked list . All the other functions are working fine, but after trying hard too , I somehow can't figure why my program gets terminated when I execute the insert_end() function . The function is :

    void list::insert_end()
     {  int data;
         node*temp,*p;
         if(start==NULL)
        cout<<"CREATE list first!:"<<endl;
        else
        { cout<<"enter data to enter in a node after the last node:"<<endl;
          cin>>data;
          temp=new node(data);
          while(p->next!=start)
         { p=p->next;
         } // now p points to last node of doubly,circular list!! i.e. the linked list is traversed till p's next pointer points to start
           temp->pre=p;
          temp->next=p->next;
          p->next->pre=temp;
          p->next=temp;
          display();
        }
      }

It is a menu driven program .

Please help me regarding the insert_end function ..I'm a beginner ...

Was it helpful?

Solution

You have an uninitialized pointer p declared here:

node*temp,*p;

And you are dereferencing it here, despite not having set it to any value:

while(p->next!=start)

Perhaps you want to add p=start; to have it start at the first node.

Note that if you have a doubly-linked circular list, then you don't need the loop to find the last node: The last node is the one before the first node, i.e. start->pre.

OTHER TIPS

You are not initializing the node pointer p!

You need to set p to the start node of the list before reaching your while loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top