繁体   English   中英

使用运算符重载来打印单链接列表

[英]Using operator overload to print Singly Linked List

我在弄清楚如何使用运算符重载来打印单链接列表时遇到了很多麻烦。 每当我尝试编译时,都会收到错误消息,指出“->”不是SLinkedList的重载成员,而出现错误消息,指出“ next”不是SLinkedList的成员。 到目前为止,这就是我所拥有的。

template <typename E> class SLinkedList;
template <typename E> class SNode;

template <typename E>
ostream& operator<< (ostream& out, const SLinkedList<E>& v);

template <typename E> 
class SNode {
private:
    E elem;
    SNode<E>* next;
    friend class SLinkedList<E>;
};
template <typename E> 
class SLinkedList {
public:
    SLinkedList();
    ~SLinkedList();
    bool empty() const;
    const E& front() const;
    void addFront(const E& e);
    void removeFront();
    int getSize();

private:
    SNode<E>* head;
    int size;
};

template <typename E>
ostream& operator <<(ostream& out, SLinkedList<E>& v) {
    for (SNode<E>* n = v->next; n != NULL; n = n->next)
    {
        out << n->elem;
    }
    return out;
    }

//END OF CLASS METHOD DEFINITIONS

float randGen() {
    float num = (rand() % 1000) + 1;
    num = num / 1000;
    return num;
}

void main() {
    SLinkedList<float> lst;

    int lstElems = 10;
    for (int i = 0; i < lstElems; i++) {
        lst.addFront(randGen());
    }
    cout << lst << endl;



    system("pause");
}

一个, v是一个引用,而不是一个指针,这就是为什么编译器抱怨->没有被重载。

第二, next不是SLinkedList的成员, SNode的成员。

第三,您需要提供用于迭代链表的公共方法。 至少是返回head的方法。

#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;

template <typename E> class SLinkedList;
template <typename E> class SNode;

template <typename E>
ostream& operator<< (ostream& out, const SLinkedList<E>& v);

template <typename E>
class SNode {
private:
    E elem;
    SNode<E>* next;
    friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
public:
    class Iterator {
    public:
        const E &operator * () {
            return curNode->elem;
        }

        Iterator &operator ++ () {
            this->curNode = this->curNode->next;
            return *this;
        }

        bool operator == (const Iterator &o) const {
            if (this == &o) return true;
            return this->curNode == o.curNode;
        }

        bool operator != (const Iterator &o) const {
            return !(*this == o);
        }

        Iterator(SNode<E> *n) : curNode(n) {}
    private:
        SNode<E> *curNode;
    };

    SLinkedList() : head(nullptr) {}
    ~SLinkedList() {
        while (head != nullptr) {
            auto next = head->next;
            delete head;
            head = next;
        }
    }
    bool empty() const;
    const E& front() const;
    void addFront(const E& e) {
        auto p = new SNode<E>;
        p->elem = e;
        p->next = head;
        head = p;
    }
    void removeFront();
    int getSize();

    Iterator begin() const {
        return Iterator(head);
    }

    Iterator end() const {
        return Iterator(nullptr);
    }

private:
    SNode<E>* head;
    int size;
};

template <typename E>
ostream& operator <<(ostream& out, const SLinkedList<E>& v) {
    for (auto i = v.begin(); i != v.end(); ++i)
    {
        out << *i;
    }
    return out;
}

//END OF CLASS METHOD DEFINITIONS

float randGen() {
    float num = (rand() % 1000) + 1;
    num = num / 1000;
    return num;
}

void main() {
    SLinkedList<float> lst;

    int lstElems = 10;
    for (int i = 0; i < lstElems; i++) {
        lst.addFront(randGen());
    }
    cout << lst << endl;

    system("pause");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM