Print&amp ;;的多态函数反向在单个类中用C ++打印

时间:2015-11-05 10:32:45

标签: c++ polymorphism operator-overloading

我已经编写了两种打印链表数据的方法。一个是正​​常工作的印刷品。一个用于以相反的顺序打印列表。我想让我的print方法接受reversePrint并像往常一样完成剩下的工作。由于操作员超载,我无法理解如何使其工作。我的方法如下。请忽略void * cast并使用template <class T> std::ostream& operator <<(std::ostream& str, LinkedList<T> const& data){ data.print(str); return str; } template <class T> void LinkedList<T>::print(std::ostream& str) const{ Node *curr = head; while(curr != nullptr){ str << curr->data << "\n"; curr = curr->next; } } template <class T> void* LinkedList<T>::printReverse(std::ostream& str) { Node* curr = head; //Node* last = (Node*)getLastNode(head); Node* rHead = nullptr; while(curr != nullptr){ Node* temp = curr->next; curr->next = rHead; rHead = curr; curr = temp; } while(rHead != nullptr){ std::cout << rHead->data << "\n"; rHead = rHead->next; } return (void*)rHead; } 方法返回。

template <class T>
class LinkedList {

private:
    struct Node {
        T data;
        Node *next;

        Node(T data, Node *next) :
                data(data), next(next) {
        }

    };
    Node* head;

public:
    LinkedList() :
            head(nullptr) {
    }

    ~LinkedList() {
        Node *temp;
        for (; head; head = temp) {
            temp = head->next;
            std::cout << "Destructor called for " << temp->data;
            delete head;
            }
    }

    void append(T item);
    void insert_at_head(T value);
    void* getLastNode(Node* n);
    void print(std::ostream& str = std::cout) const;
    void* printReverse(std::ostream& str = std::cout);

};

类定义。

{{1}}

1 个答案:

答案 0 :(得分:1)

printReverse可能修改列表。

我会递归地写它。想法:

template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
    if (!head) return;
    printReverse(str, head->next);
    std::cout << head->data << "\n";
}

例如课堂上的声明:

    void printReverse(std::ostream &str) const { printReverse(str, head); }
  private:
    void static printReverse(std::ostream &str, Node const* head);

偏离主题:

  • 考虑const-correctness
  • 考虑命令/查询分离(单独的节点迭代打印)

<强> Live On Coliru

#include <iostream>
template <class T> class LinkedList {

  private:
    struct Node {
        T data;
        Node *next;
        //Node(T data, Node *next) : data(data), next(next) {}
    };
    Node *head;

  public:
    LinkedList() : head(nullptr) {}

    ~LinkedList() {
        while (head) {
            Node* temp = head->next;
            std::cout << "Destructor called for " << head->data;
            delete head;
            head = temp;
        }
    }

    void append(T item) {
        Node* t = getLastNode(head);
        (t?t->next : head) = new Node { item, nullptr };
    }

    void insert_at_head(T value);

    Node const*getLastNode(Node const*n) const {
        for (Node const* it = n; it; it = it->next)
            if (!it->next) return it;
        return nullptr;
    }

    Node*getLastNode(Node*n) {
        for (Node* it = n; it; it = it->next)
            if (!it->next) return it;
        return nullptr;
    }

    void print(std::ostream &str = std::cout) const;
    void printReverse(std::ostream &str) const { printReverse(str, head); }
  private:
    void static printReverse(std::ostream &str, Node const* head);
};

template <class T> std::ostream &operator<<(std::ostream &str, LinkedList<T> const &data) {
    data.print(str);
    return str;
}

template <class T> void LinkedList<T>::print(std::ostream &str) const {
    for (Node const* curr=head; curr; curr = curr->next) {
        str << curr->data << "\n";
    }
}

template <class T> void LinkedList<T>::printReverse(std::ostream &str, Node const* head) {
    if (!head) return;
    printReverse(str, head->next);
    std::cout << head->data << "\n";
}

int main() {
    LinkedList<int> ll;
    ll.append(1);
    ll.append(2);
    ll.append(3);
    ll.append(4);

    ll.print(std::cout);
    ll.printReverse(std::cout);
}

打印

1
2
3
4
4
3
2
1