我如何使用C ++以相反的顺序打印此单个链表中的元素

how i can print the elements in this single linked list in reverse order using c++

本文关键字:打印 顺序 链表 元素 单个 何使用      更新时间:2023-10-16
            #include <iostream>
            #include<string>
            using namespace std;
            struct nodeType
            {
                int info;
                nodeType *next;
            };
            class linkedListType
            {
            private:
                nodeType *first, *last;
                int length;
            public:
                linkedListType()//constructor
                {
                    first = last = NULL;
                    length = 0;
                }
                void print() // normal print
                {
                    nodeType * current = first;
                    while (current != NULL)
                    {
                        cout << current->info <<" ";
                        // update statement
                        current = current ->next;
                    }
                }

                void insertEnd(int item) //insert item to the end of the list
                {  // forward insertion 
                    nodeType* newNode = new nodeType;
                    newNode ->info = item;
                    if (length == 0)
                    {
                        first = last = newNode;
                        newNode->next = NULL;
                    }//if
                    else
                    {
                        last->next = newNode;
                        last =  newNode;
                        newNode->next = NULL;
                    }// else
                    length++;
                }
                }
                void clearList()
        {
            nodeType * current;
            while ( first != NULL)
            {
                current = first;
                first = first->next;
                delete current;
                length--;
            }// while
    ~linkedListType() //destroctor
        {
            clearList();
        }

> `
//

块引用我不能写这个方法实现请任何人帮助我并解释为什么/// 谁能帮我写信给我,解释为什么/ ////////////////////////////////////////////////////////////////////

`

                void printReverse() /*this is the function that i cant understand it or complete it. this function print elements in the list in reverse*/
                {
                    nodeYype* current=last ,*newnode =new nodType ;
                    for(int i=length;i>=0;i--)
                            //i cant complete this method
                }
            };
            void main()
            {
               linkedListType list1;
               list1.insertEnd(12); //insert item
               list1.insertEnd(25);//insert item
               list1.insertEnd(18);//insert item
               list1.insertEnd(37);//insert item
               list1.insertEnd(60);//insert item
               list1.insertEnd(100);//insert item
               list1.insertEnd(37);//insert item
               list1.insertEnd(37);//insert item
               list1.insertEnd(37);//insert item
               list1.insertEnd(60);//insert item
               list1.insertEnd(25);//insert item
               list1.insertEnd(100);//insert item
               list1.insertEnd(25);//insert item
               cout <<"Printing the linked list elementsn";
               list1.print();
               cout <<"nPrinting the list elements in reverse ordern";
               list1.printReverse();
            }
void nodeType::PrintListReverse()
{
  if (next)
     next->PrintListReverse();
  std::cout << info << std::endl;
}

递归地查找列表的末尾,在返回时打印。

(我只是因为无聊才启用你)

或者:

void linkedListType::PrintList()
{
    std::vector<int> info(length);
    nodeType* curNode = first;
    for (int i = 0; curNode != NULL; i++, curNode = curNode->next)
    {
        info[i] = curNode->info;
    }
    for (int i = length-1; i >=0; i--)
    {
        std::cout << info[i] << std::endl;
    }
}

如果可以编写递归函数以正确的顺序遍历列表,则以相反的顺序打印它很容易。

有两种可能性。要么编写递归函数,要么以相反的顺序重建列表。也就是说,在打印列表之前,您可以在现有的基础上创建一个新列表,也可以重建原始列表本身。

你已经有一个循环,它将 i 从长度递减到 0。 基于 i,您可以遍历列表并打印您到达的节点。 微调 1 个错误,以便您实际从最后一个打印到第一个,并且在列表为空时不打印。