首页 > 解决方案 > 写入反向链表的函数不能完全给出正确的输出

问题描述


我有一个 DS 任务,我们应该编写一个程序来接受链表,反转它们并将它们打印出来。现在我认为我做对了函数本身。但是程序必须以某种方式输出才能正确分级,这就是我苦苦挣扎的地方。

演示:这是示例输入和示例输出的样子
链接

这就是我的输出看起来像
链接

当然这是代码(抱歉,我想完全确定这有点久了。重点是 ReversePrinter() )

#include <bits/stdc++.h>

using namespace std;

class SinglyLinkedListNode {

    public:

        int data;

        SinglyLinkedListNode *next;

        SinglyLinkedListNode(int node_data) {

            this->data = node_data;

            this->next = nullptr;

        }

};

class SinglyLinkedList {

    public:

        SinglyLinkedListNode *head;

        SinglyLinkedListNode *tail;

        SinglyLinkedList() {

            this->head = nullptr;

            this->tail = nullptr;

        }

        void insert_node(int node_data) {

            SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);

            if (!this->head) {

                this->head = node;

            } else {

                this->tail->next = node;

            }

            this->tail = node;

        }

};

void print_singly_linked_list(SinglyLinkedListNode* node, string sep)
{

    while (node) {

        cout << node->data;

        node = node->next;

        if (node) {

            cout << sep;

        }

    }

}

void free_singly_linked_list(SinglyLinkedListNode* node) {

    while (node) {

        SinglyLinkedListNode* temp = node;

        node = node->next;

        free(temp);

    }

}

// Complete the reversePrint function below.

/*

 \* For your reference:

 \*

 \* SinglyLinkedListNode {

 \*     int data;

 \*     SinglyLinkedListNode* next;

 \* };

 \*

 \*/

void reversePrint(SinglyLinkedListNode* head) {

    SinglyLinkedListNode* prev = NULL;

    while(head != NULL)
    {
        SinglyLinkedListNode* next_node = head->next;
        head->next = prev;
        prev = head;
        head = next_node;
    }

    print_singly_linked_list(prev, "\n");


}

int main()

{

    int tests;

    cin >> tests;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    for (int tests_itr = 0; tests_itr < tests; tests_itr++) {

        SinglyLinkedList* llist = new SinglyLinkedList();

        int llist_count;

        cin >> llist_count;

        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        for (int i = 0; i < llist_count; i++) {

            int llist_item;

            cin >> llist_item;

            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            llist->insert_node(llist_item);

        }

        reversePrint(llist->head);

    }

    return 0;

}

我觉得有一些非常简单的事情我没有做对但仍然:)

标签: c++oopdata-structures

解决方案


如果您希望在所有输入之后都输出,您应该存储所有创建的列表,然后打印它们。

我还发现了一点:换行符不是从reversePrint()最后一个元素之后打印出来的,所以你必须在执行之后添加一个。

可以这样做:

int main()
{
    int tests;
    cin >> tests;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // allocate an array to store lists
    SinglyLinkedList** llists = new SingleyLinkedList*[tests];

    for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
        SinglyLinkedList* llist = new SinglyLinkedList();

        // create list (omit: do just as original code)

        // store created list
        llists[tests_itr] = llist;
        // instead of this
        // reversePrint(llist->head);
    }

    // print created lists
    for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
        // print a list
        reversePrint(llists[tests_itr]->head);
        // print a newline
        cout << '\n';
    }

    // free the array
    delete[] llists;

    return 0;
}

推荐阅读