【问题标题】:C - Swap first and last element in doubly linked listC - 交换双向链表中的第一个和最后一个元素
【发布时间】:2016-11-09 16:38:49
【问题描述】:

我正在尝试交换双向链表的第一个和最后一个元素。到目前为止,我有以下代码,我在其中创建了一个列表并向其中添加了一些数字。但是两次输出都是同一个列表。

#include <stdio.h>
#include <stdlib.h>

struct node2 {
    int number;
    struct node2 *next, *prev;
};

void addNodeDouble(struct node2 **head, struct node2 **tail, int num, int thesi) {
    if (*head == NULL) {
        struct node2 * current;
        current = (struct node2 *)malloc(1 * sizeof(struct node2));
        current->number = num;
        current->prev = NULL;
        current->next = NULL;
        *head = current;
        *tail = current;
    } else {
        if (thesi == 1) {
            struct node2 *current, *temp;
            current = (struct node2 *)malloc(1 * sizeof(struct node2));
            current->number = num;
            temp = *head;
            while (temp->next != NULL)
                temp = temp->next;

            temp->next = current;
            current->prev = *tail;
            current->next = NULL;
            (*tail)->next = current;
            *tail = current;
        } else  {
            struct node2 *current;
            current = (struct node2 *)malloc(1 * sizeof(struct node2));
            current->number = num;
            current->next = *head;
            (*head)->prev = current;
            *head = current;
        }
    }
}

void ReversedisplayList(struct node2 **head, struct node2 **tail) {
    struct node2 *current;
    if (*head == NULL)
        printf("I lista einai adeia!\n");
    else {
        current = *tail;
        while (current != NULL) {
            printf("%d ", current->number);
            current = current->prev;
        }
    }
}

void swapElements2(struct node2 **head, struct node2 **tail) {
    struct node2 *current, *temp;

    temp = (*tail)->prev;
    current = *tail;

    temp->next = *head;
    current->next = (*head)->next;
    (*head)->next = NULL;
    *head = current;
}

int main() {
    struct node2 *head, *tail;
    head = tail = NULL;

    addNodeDouble(&head, &tail, 4, 1);
    addNodeDouble(&head, &tail, 8, 1);
    addNodeDouble(&head, &tail, 3, 0);
    addNodeDouble(&head, &tail, 1, 1);
    addNodeDouble(&head, &tail, 7, 0);

    printf("\n\nDoubly linked list (reversed): ");
    ReversedisplayList(&head, &tail);

    swapElements2(&head, &tail);
    printf("\nChanged list: ");
    ReversedisplayList(&head, &tail);
}  

我明白了:

Doubly linked list (reversed): 1 8 4 3 7 
Changed list: 1 8 4 3 7 

但我想要:

Changed list: 7 8 4 3 1 

【问题讨论】:

  • 您可能需要考虑交换节点的数据而不是节点本身。在这种情况下,这会让你的事情变得更容易。
  • @Hypino 不,我实际上想交换节点

标签: c linked-list swap doubly-linked-list


【解决方案1】:

要交换第一个和头尾元素,您必须完成以下过程。 首先我们要在某个临时变量中获取tail的前一个节点和head的下一个节点,并交换head和tail的next和prev指针。

void swapElements2(struct node2 **head, struct node2 **tail) {
    struct node2 *ttail, *thead;

    ttail = (*tail) -> prev;
    thead = (*head) -> next;

    (*head) -> next = NULL;
    (*tail) -> prev = NULL;

    (*head) -> prev = ttail;
    (*tail) -> next = thead;

    ttail -> next = (*head);
    thead -> prev = (*tail);

    (*tail)  = ttail -> next;
    (*head)  = thead -> next;
}

【讨论】:

  • 缩进不正确,应该有一个简短的解释,而不仅仅是代码转储。
  • 谢谢,我已经添加了解释。
  • 请正确缩进你的代码,这样更易​​读,更容易理解。
  • 对不起,我不知道你是什么意思
  • @jafarbtech 谢谢好心的先生。这解决了我的问题。感谢您的帮助,并感谢您提供的解释。
【解决方案2】:

您忘记更改(*head) -&gt; prev(*tail) -&gt; prev

(*head)->prev = temp;
(*tail)->prev = NULL;

【讨论】:

  • 嗯,在swapElements2函数的末尾添加这些命令?因为如果我这样做,我会得到 "Changed list: 1"(here)
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多