c - 在 C 中反转列表后打印单链表的问题

标签 c linked-list singly-linked-list

我已经反转了一个单链表并且还交换了头部和尾部,但是在 reverseList() 之后输出仅显示列表的头部。

/* Program to create a linked list and read numbers into it until the user 
   wants and print them using functions
Author: Shekhar Hazari
Created On: 20, January 2019 */

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

struct node { int data; struct node *next; };

typedef node *list;

list head, tail;

list append(int d, list t); 
void printList(list h); 
void reverseList(list, list);

int main() {
    char more = 'Y';
    int dat;

    head = (list)malloc(sizeof(node));
    printf("Enter the first integer: ");
    scanf("%d", &dat);
    head->data = dat;
    head->next = NULL;
    tail = head;
    printf("\nWant to add more data into the list? ");
    scanf(" %c", &more);

    while (more == 'y' || more == 'y') {
        printf("Enter the integer to add to list: ");
        scanf("%d", &dat);

        tail = append(dat, tail);

        printf("\nWant to add more data into the list? ");
        scanf(" %c", &more);
    }

    printf("\nPrinting the list in the order it was entered: ");
    printList(head);

    reverseList(head, tail);
    printf("\nPrinting the list after 'reverseList': ");
    printList(head);

    return 0;
}

// function to append integer to the list 
list append(int d, list t) {
    list temp;
    temp = (list) malloc(sizeof(node));
    temp->data = d;
    t->next = temp;
    temp->next = NULL;
    t = temp;
    return t;
}

// function to print the list
void printList(list h) {
    list temp;
    temp = h;
    while (temp != NULL) {
        printf("%d\t", temp->data);
        temp = temp->next;
    }
}

// function to reverse a singly linked list 
void reverseList(list h, list t) {
    list temp1, temp2;

    temp1 = t; //temp2 = head;

    while (temp1 != h) {
        temp2 = h;

        while (temp2->next != temp1)
            temp2 = temp2->next;

        temp1->next = temp2;
        temp1 = temp2;
    }
    h = t;
    t = temp1;
    t->next = NULL;

    return;
}

例如,我将 5, 10, 15, 20, 25 插入到列表中,在 reverseList() 之后,输出为 5。我哪里出错了?

最佳答案

您不能反转列表并保持 headtail 指针不变。

reverseList 函数应该获取指向headtail 的指针,并更新它们以指向反向列表的第一个和最后一个节点。此外,反转单向链表可以一次性完成。

修改后的版本:

// function to reverse a singly linked list 
void reverseList(list *headp, list *tailp) {
    list temp, last, next;

    *tailp = temp = *headp;

    if (temp) {
        while ((next = temp->next) != NULL) {
            temp->next = last;
            last = temp;
            temp = next;
        }
        *headp = last;
    }
}

main调用

reverseList(&head, &tail);

关于c - 在 C 中反转列表后打印单链表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280924/

相关文章:

c++ - 在 X11 下如何判断你的光标是否指向你的桌面

c - 不同线程在同一个 condvar 上调用 pthread_cond_wait() 和 pthread_cond_timedwait() 是否正确?

java - 将 Deque 定义为 LinkedList

c++ - 为什么 "\?"是 C/C++ 中的转义序列?

Javascript链表

c++ - 如何添加和返回链表前面的节点

c - 下面的 LinkedList 代码有什么问题?

javascript - 在 JavaScript 中反转链表的策略

java - 为什么一种方法会破坏我的链表而另一种方法不会?

c - 读取(C 函数)行为异常