微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C ++链表非成员函数可反向打印

如何解决C ++链表非成员函数可反向打印

因此,我了解了如何使用递归以相反的顺序打印单个链接列表。我在执行非成员函数时遇到了麻烦。 例如,在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);

}

添加功能

解决方法

除了破坏头之外,头实际上不提供访问列表内容的任何方法。所以...这就是我们要做的。

int  deleteFromTail(); // delete the tail and return its info;

除了我们需要采取额外的步骤并重建它之外,因为没有人期望打印容器来破坏其内容。参见https://en.wikipedia.org/wiki/Principle_of_least_astonishment

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

3
2
1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。