简体   繁体   English

在双圆形链表的末尾插入节点

[英]Inserting node at the end of a double, circular linked list

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 . 所有其他功能都运行良好,但在努力之后,我无法理解为什么我的程序在执行insert_end()函数时被终止。 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 ... 请帮我看看insert_end函数..我是初学者...

You have an uninitialized pointer p declared here: 你有一个未初始化的指针p在这里声明:

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; 也许你想添加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, ie start->pre . 请注意,如果您有一个双向链接循环列表,那么您不需要循环来查找最后一个节点:最后一个节点是第一个节点之前的节点,即start->pre

You are not initializing the node pointer p ! 您没有初始化节点指针p

You need to set p to the start node of the list before reaching your while loop. 在到达while循环之前,需要将p设置为列表的起始节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM