简体   繁体   English

双向链表插入在中间

[英]doubly linked list insert at middle

Can anyone identify what is happening in my code that is causing the segmentation fault? 谁能识别我的代码中引起分段错误的内容? Please modify/correct the wrong part. 请修改/纠正错误的部分。

void InsertAtMid (Node *head){

    int num,count=0,i;
    Node *ptr=head;
    Node *newnode=NULL;
    Node *newnode2=head;

    printf("Enter node to be inserted: ");
    scanf("%d", &num);

    if (head==NULL){
            newnode = head;
            newnode=(Node *)malloc(sizeof(Node));
            newnode->x=num;
            newnode->next=NULL;
            newnode->prev=NULL;
    } else {
            ptr=head->next;
            while(ptr->x!=(count/2)){
                ptr=ptr->next;
            }
            newnode->next=ptr->next;
            newnode->prev=ptr;
            ptr->next->prev=newnode;
            ptr->next=newnode;
    }
}

So, based on my understanding of your code - the following should [mostly] work: 因此,根据我对您的代码的理解-以下应该[大部分]有效:

void InsertAtMid (Node **head){
    int num = 0;
    int count = 0
    int advance = 0;
    Node *ptr = *head;
    Node *newnode = NULL;

    printf("Enter node to be inserted: ");
    scanf("%d", &num);

    if (*head == NULL) {
      *head = (Node *)malloc(sizeof(Node));
      ptr = *head;
      ptr->x = num;
      ptr->next = NULL;
      ptr->prev = NULL;
    } else {
      // *** Count the number of items
      ptr = *head;
      while (ptr != NULL) {
         ptr = ptr->next;
         count++;
      }

      // *** Move to the middle of the list
      ptr = *head;
      while (advance < (count/2)){
         ptr = ptr->next;
         advance++;
      }

      // *** Insert the new value
      newnode = (Node *)malloc(sizeof(Node));
      newnode->x = num;
      newnode->next = ptr->next;
      newnode->prev = ptr;
      ptr->next->prev = newnode;
      ptr->next = newnode;
   }
}

The following are the issues I fixed: 以下是我解决的问题:

  • You are assigning to head at one point, but since "head" isn't passed in as a reference, the value isn't going to be maintained beyond the first time the function is called. 您是在某个时候分配给head的,但是由于没有传递“ head”作为参考,因此该值不会在第一次调用该函数之后被保留。 Needless to say you need a pointer to a pointer of type node. 不用说,您需要一个指向节点类型的指针的指针。
  • You never calculated the number of items in the list. 您从未计算过列表中的项目数。 Often "head" pointer would store this information and you would increment when you add a node, but since you don't have that the only way to determine it is to traverse the list till you find the count. 通常,“ head”指针将存储此信息,并且在添加节点时会增加该信息,但是由于没有这种唯一的确定它的方法是遍历列表直到找到计数。
  • You never allocated space for the new node to insert except if you were initializing the head pointer. 除非初始化头指针,否则您从未分配空间供新节点插入。 This was also an issue. 这也是一个问题。

Hope that helps some. 希望对您有所帮助。 Best of luck! 祝你好运!

int num,count=0,i;
...
ptr=head->next;
while(ptr->x!=(count/2)){
    ptr=ptr->next;

count is initialized to 0 and never changed. count初始化为0,并且从未更改。

So unless you enter "0" for x, that while loop is just going to walk off the end of the list, every time. 因此,除非您为x输入“ 0”,否则while循环每次都会走出列表的末尾。

Test to work out under what circumstances your code segfaults. 测试以找出在什么情况下您的代码段错误。

You'll find that it works OK when head == NULL , but fails if head is not null. 您会发现,当head == NULL时,它可以正常工作,但是如果head不为null,则会失败。

So you know that your error is somewhere in the else block. 因此,您知道您的错误在else块中的某处。

Step through your running code in a debugger (if you don't know how, it's never too early to learn: whenever you solve a problem with a debugger, you think "why didn't I turn to this sooner?"). 在调试器中逐步执行正在运行的代码(如果您不知道怎么做,那么学习它永远不会太早:每当您使用调试器解决问题时,您都会想到“为什么不早点解决?”。)

Work out what you expect to happen, watch the variables in the debugger, and when the actual values deviate from your expectations, reason about why. 弄清您期望发生的情况,观察调试器中的变量,以及当实际值偏离您的期望时,说明原因。

It's not clear to me what you expect to happen in your code, but what will actually happen for a list with one node, is that: 对我来说尚不清楚您期望在代码中发生什么,但是对于具有一个节点的列表,实际发生的是:

  • it will execute ptr=head->next; 它将执行ptr=head->next; -- so ptr is now NULL . -因此ptr现在为NULL
  • then for the while condition it will try to dereference ptr->x , and since ptr is NULL, it will segfault. 然后对于while条件,它将尝试取消对ptr->x引用,并且由于ptr为NULL,它将进行段错误。

A quick fix for that would be: 一个快速的解决方法是:

while(ptr != NULL && ptr->x ....) {

But you need to think about whether that's the actual logic you want; 但是您需要考虑这是否是您想要的实际逻辑; and once you get past that, you'll hit other problems (for example, count never changes), which can be sorted out with a debugger in the same way. 而一旦您克服了这些麻烦,就会遇到其他问题(例如, count永不改变),这些问题可以用调试器以相同的方式加以解决。

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

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