简体   繁体   English

反向双链表

[英]Reverse double linked list

I have to write a function to reverse a double linked list, so that the tail becomes the head. 我必须编写一个函数来反转双链表,以便尾部成为头部。

For example, the elements before: {(1,1), (1,2), (2,2), (2,3)} 例如,之前的元素:{(1,1),(1,2),(2,2),(2,3)}

after: {(2,3), (2,2), (1,2), (1,1)} 之后:{(2,3),(2,2),(1,2),(1,1)}

Here's the structure: 结构如下:

  struct snake {
    unsigned int i;
    unsigned int j;
    struct snake *next;
    struct snake *prev;
  };

This is the function prototipe I must use: 这是我必须使用的函数原型:

void snake_reverse(struct snake **s);

I tried something like this and several other attempts 我尝试了这样的尝试和其他几次尝试

void snake_reverse(struct snake **s) {
struct snake *last, *tmp = NULL;

last = *s;

while (last !=  NULL)
 {
   tmp = last->prev;
   last->prev = last->next;
   last->next = tmp;
   last = last->prev;
 }

 if(tmp != NULL )
    *s = tmp->prev;
}

Also tried this: 还尝试了以下方法:

while (last !=  NULL)
 {
   tmp = last->next;
   last->next = last->prev;
   last->prev = tmp;
   last = tmp;
 }

 if(tmp != NULL )
    *s = tmp;

but he doesn't work. 但他不工作。 I'm almost sure I'm not wrong. 我几乎可以确定我没有记错。 The first ->prev of the list is NULL, the last ->next of the list is NULL. 列表的第一个-> prev为NULL,列表的最后一个-> next为NULL。

I get no errors or crash, but the task of the function is to invert the direction of the snake by reversing all elements and changing the head of the list. 我没有遇到任何错误或崩溃,但是该函数的任务是通过反转所有元素并更改列表的开头来反转蛇的方向。 Can you say what's wrong here? 你能说出什么问题吗?

EDIT: The problem was in another module of the program not made by me. 编辑:问题出在不是我的程序的另一个模块中。

Anyway the best solution is that of kmkaplan. 无论如何,最好的解决方案是kmkaplan。 Thanks everybody 谢谢大家

You have to set *s to the new head of the list. 您必须将*s设置为列表的新标题。 That is the old tail of the list, the last element you process. 那是列表的旧尾,是您处理的最后一个元素。

void snake_reverse(struct snake **s) {
    struct snake *last, *tmp = NULL;
    last = *s;
    while (last !=  NULL) {
        *s = last
        tmp = last->prev;
        last->prev = last->next;
        last->next = tmp;
        last = last->prev;
    }
}

I am not sure but I think that the last line of code in your while loop is wrong. 我不确定,但是我认为while循环中的最后一行代码是错误的。 As I understand the last variable is the tail of the snake. 据我了解,最后一个变量是蛇的尾巴。 That means that last->next = null. 这意味着last-> next = null。 In the second line of code in your while you make the previous of last null and in the last line of code in the while the last becomes 0. I think that changing your last line of code in the while loop will change this. 在您的while的第二行代码中,使last的前一个为null,在while的最后一行代码中,last变为0。我认为,在while循环中更改最后的代码行将对此进行更改。

last = last->next

You're never setting the new head of the list since tmp is always NULL after the while loop. 您永远不会设置列表的新标题,因为while循环之后tmp始终为NULL。 Try this; 尝试这个;

void snake_reverse(struct snake **s) {
  struct snake *last, *newHead, *tmp = NULL;

  last = *s;

  while (last !=  NULL)
  {
     tmp = last->prev;
     if (tmp!=NULL)
       newHead = tmp;
     last->prev = last->next;
     last->next = tmp;
     last = last->prev;
  }

  *s = newHead;
}

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

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