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

时间:2015-10-14 02:11:20

标签: c++ operator-overloading 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");
}

1 个答案:

答案 0 :(得分:1)

一,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");
}