在 C++ 中以相反的顺序打印我的链表 [英] Printing my linked list in reverse order in C++

查看:21
本文介绍了在 C++ 中以相反的顺序打印我的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 C++ 还很陌生,今天我决定坐下来了解链表的工作原理.到目前为止,我玩得很开心,但是在尝试以相反的顺序打印我的链表时遇到了一个问题(而不是颠倒链表的顺序!)

So I'm fairly new to C++ and today I decided to sit down and understand how linked lists work. I'm having a lot of fun doing it so far, but I've encountered a problem when trying to print my linked list in reverse order (not reverse the order of the linked list!)

另外,我想在没有双链表的情况下做到这一点:

Also, I wanted to do this without having a double linked list:

#include <iostream>
#include <string>

using namespace std;

class LinkedList
{
    public:
        LinkedList()
        {
            head = NULL;
        }

        void addItem(string x)
        {
            if(head == NULL)
            {
                head = new node();
                head->next = NULL;
                head->data = x;
            } else {
                node* temp = head;
                while(temp->next != NULL)
                    temp = temp->next;

                node* newNode = new node();
                newNode->data = x;
                newNode->next = NULL;
                temp->next = newNode;
            }
        }
        void printList()
        {
            node *temp = head;
            while(temp->next != NULL)
            {
                cout << temp->data << endl;
                temp = temp->next;
            }
            cout << temp->data << endl;
        }

        void addToHead(string x)
        {
            node *temp = head;
            head = new node;
            head->next = temp;
            head->data = x;
        }

        int countItems()
        {
            int count = 1;
            for(node* temp = head; temp->next != NULL; temp = temp->next)
                ++count;
            return count;
        }

        void printReverse()
        {
            node* temp2;
            node* temp = head;
            while(temp->next != NULL)
                temp = temp->next;

            //Print last node before we enter loop
            cout << temp->data << endl;

            for(double count = countItems() / 2; count != 0; --count)
            {
                //Set temp2 before temp
                temp2 = head;
                while(temp2->next != temp)
                    temp2 = temp2->next;
                cout << temp2->data << endl;

                //Set temp before temp2
                temp = head;
                while(temp->next != temp2)
                    temp = temp->next;
                cout << temp->data << endl;
            }
            cout << "EXIT LOOP" << endl;
        }

    private:
        struct node
        {
            string data;
            node *next;
        }

    *head;
};

int main()
{
    LinkedList names;

    names.addItem("This");
    names.addItem("is");
    names.addItem("a");
    names.addItem("test");
    names.addItem("sentence");
    names.addItem("for");
    names.addItem("the");
    names.addItem("linked");
    names.addItem("list");

    names.printList();

    cout << endl;

    names.addToHead("insert");

    names.printList();

    cout << endl;

    cout << names.countItems() << endl;

    cout << "Print reverse: " << endl;
    names.printReverse();
    cout << endl;

    return 0;
}

现在我不确定我的代码崩溃的确切原因,感谢任何帮助!

Now I'm not sure exactly why my code crashes, any help is appreciated!

谢谢!

推荐答案

printList中,还要检查head == NULL,否则就是访问成员指向 NULL 的指针.以下应该有效.

Within printList, you have to also check for head == NULL, otherwise you are acessing members of a pointer pointing to NULL. The following should work.

    void printList()
    {
        node *temp = head;
        while(temp != NULL) // don't access ->next
        {
            cout << temp->data << endl;
            temp = temp->next;
        }
    }

printReverse() 中,我真的不明白为什么每次迭代都要打印元素计数的一半并打印两个元素.但是,您在这里真的不需要 for 循环.您可以在循环结束后在 temp == head 后立即停止,因为那时您只是打印了头部.并且只打印一个元素,该元素的 next 指针指向之前打印的元素.

In printReverse() I really can't understand why you take half of the counts of the elements to print and print two elements in every iteration. However, you really don't need a for-loop here. You can simply stop as soon as temp == head after your loop, since then you just printed the head. And only print one element, the one whose next pointer points to the previously printed element.

另一个递归的尝试解决这个问题看起来像这样:

Another, recursive, attempt to solve the problem looks like this:

    void printReverse()
    {
        printReverseRecursive(head);
    }
    void printReverseRecursive(node *n)
    {
        if(n) {
            printReverseRecursive(n->next);
            cout << n->data << endl;
        }
    }

这篇关于在 C++ 中以相反的顺序打印我的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆