0

それで、再帰を使用して単一のリンクされたリストを逆順に出力する方法を理解しました。非メンバー関数を実行するのに問題があります。たとえば、int print_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);

}

機能追加

4

1 に答える 1