C++ C++;用于反向打印的链表非成员函数

C++ C++;用于反向打印的链表非成员函数,c++,class,linked-list,non-member-functions,C++,Class,Linked List,Non Member Functions,因此,我了解了如何使用递归以相反顺序打印单个链表。我在执行非成员函数时遇到问题。 例如,在int print\u reverse(IntSLList&list))函数中,如何以迭代的方式进行反向打印 ************************ .h file ************************** class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int

因此,我了解了如何使用递归以相反顺序打印单个链表。我在执行非成员函数时遇到问题。 例如,在
int print\u reverse(IntSLList&list))
函数中,如何以迭代的方式进行反向打印

************************  .h  file **************************
class IntSLLNode {
public:
    IntSLLNode() {
        next = 0;
    }
    IntSLLNode(int el, IntSLLNode *ptr = 0) {
        info = el; next = ptr;
    }
    int info;
    IntSLLNode *next;
};

class IntSLList {
public:
    IntSLList() {
        head = 0;
    }
    ~IntSLList();
    int isEmpty() {
        return head == 0;
    }
    void addToHead(int);
    void addToTail(int);
    int  deleteFromHead(); // delete the head and return its info;
    int  deleteFromTail(); // delete the tail and return its info;
    bool isInList(int) const;
    void printAll() const;
    
private:
    IntSLLNode *head;
};
这里是主要的

************************ main **************************
#include <iostream>
using namespace std;

#include "intSLList.h"

int print_reverse(IntSLList & list){


  if (head == NULL)  
     return;  
  printReverse(head->next); 

  cout << head->data << " ";  

 //How to compelete this in an iterative(or recursive if iterative is too much work)way ?
 //like this?       
}


int main() {

    IntSLList list;
    
    list.print_reverse(list);

}
***************************主**************************
#包括
使用名称空间std;
#包括“intSLList.h”
int print_reverse(IntSLList和list){
if(head==NULL)
返回;
打印反转(头部->下一步);

cout data标题实际上没有办法访问列表的内容,只能通过销毁它。所以…这就是我们要做的

int  deleteFromTail(); // delete the tail and return its info;
但我们需要进行额外的步骤并重建它,因为没有人希望打印容器会破坏其内容。请参阅

#包括
#包括
#包括
类IntSLList{
公众:
int isEmpty(){返回m_list.empty();}
void addToHead(inti){m_list.push_front(i);}
void addToTail(inti){m_list.push_back(i)}
int deleteFromHead(){
int temp=m_list.front();
m_list.pop_front();
返回温度;
}
int deleteFromTail(){
int temp=m_list.back();
m_list.pop_back();
返回温度;
}
私人:
//没有给出实现,所以我在内部使用std::list。
std::列表m_列表;
};
int print_reverse(IntSLList和mylist){
//将我们正在销毁的数据存储在temp中
IntSLList temp;
//从字面上说,我们可以访问容器内容的唯一方式是破坏性的,所以…我猜我们要去那里
而(!mylist.isEmpty()){
int back=mylist.deleteFromTail();

std::不能向前迭代并将每个元素放在堆栈中。通过弹出直到堆栈为空来打印堆栈的内容。我假设您不允许使用双链接列表。如果是这种情况,您必须将元素放入一个容器中,您可以从头到脚进行迭代。递归解决方案更简单。@drescherjm yes.I我尝试过递归,但总是出错这就是我为什么尝试迭代的原因,但如果它太过复杂,那么最好看看递归方法,也就是说,如果你发布你的递归解决方案,很有可能我们这里的一条聪明的裤子可以告诉你如何修复它。几乎总是这样与其让函数为空,不如尝试问一个问题,只要这是一个可信的尝试。这不可能是真的。这里发布的标题也没有给出任何向前迭代的方式。或者做任何事情。请注意,我使用std::list创建了一个工作示例。即使不允许使用标准library、 始终使用标准库。然后在你有了工作代码之后,用你自己的手工编码版本替换它(这样你一次只能处理一组代码中的bug)。
#include <iostream>
#include <stack>
#include <list>

class IntSLList {
public:
    int isEmpty() { return m_list.empty(); }
    void addToHead(int i) { m_list.push_front(i); }
    void addToTail(int i) { m_list.push_back(i); }
    int  deleteFromHead() {
        int temp = m_list.front();
        m_list.pop_front();
        return temp;
    }
    int  deleteFromTail() { 
        int temp = m_list.back();
        m_list.pop_back();
        return temp;
    }

private:
    // no implementation given so I'm using std::list internally.
    std::list<int> m_list;
};

int print_reverse(IntSLList& mylist) {
    // store the data we are destroying in temp
    IntSLList temp;

    // literally the only way we can access the contents of the container is destructive so ... guess we're going there
    while (!mylist.isEmpty()) {
        int back = mylist.deleteFromTail();
        std::cout << back << std::endl;
        temp.addToHead(back);
    }

    // now rebuild the original list. I told you this would be bad.
    while (!temp.isEmpty()) {
        mylist.addToHead(temp.deleteFromTail());
    }

    // maybe this was supposed to be length, but not documented so I can return whatever I want.
    return -1;
}

int main() {
    IntSLList mylist;
    mylist.addToTail(1);
    mylist.addToTail(2);
    mylist.addToTail(3);
    print_reverse(mylist);
}