Проблема печати односвязного списка после обращения списка в C - PullRequest
0 голосов
/ 20 января 2019

Я перевернул односвязный список, а также поменял местами голову и хвост, но после 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.Где я ошибся?

1 Ответ

0 голосов
/ 21 января 2019

Нельзя перевернуть список и сохранить указатели head и tail в целости.

Функция reverseList должна принимать указатели на head и tail и обновлять их, чтобы они указывали на первый и последний узел обращенного списка. Кроме того, изменение односвязного списка может быть выполнено за один проход.

Вот модифицированная версия:

// 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);
...