c++ - 在 C++ 中以相反的顺序打印我的链表

标签 c++ linked-list

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

此外,我想在没有双链表的情况下执行此操作:

#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;
}

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

谢谢!

最佳答案

printList 中,您还必须检查 head == NULL,否则您正在访问指向 NULL 的指针成员。以下应该有效。

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

printReverse()中我实在无法理解为什么每次迭代都要取元素计数的一半来打印,打印两个元素。但是,您在这里真的不需要 for 循环。您可以在循环结束后立即停止 temp == head,因为那时您只是打印了 head。并且只打印一个元素,即下一个指针指向先前打印的元素的元素。

解决问题的另一个递归尝试如下所示:

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

关于c++ - 在 C++ 中以相反的顺序打印我的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14177337/

相关文章:

c++ - 将 const int 分配给指向 int 的 const 指针是非法的吗?

c++ - C++ : Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error

c - 递归/链表

c++ - C++ 匿名对象有什么问题?

c++ - 构造可变参数模板参数列表

c++ - clang --version 权限被拒绝错误

c++ - 在 Windows 中获取下一个打开的 tcp 端口

c - C中的链表删除

c++ - 为什么这段代码在 Linux 中运行良好但在 Windows 中却失败了?

c - 通过函数传递时指向 NULL 的全局链表