迭代器指针不引用第一个元素

时间:2017-10-18 16:08:39

标签: c++ linked-list iterator

我正在尝试实现一个可以在c ++中迭代的LinkedList。

因此我创建了一个Iterator类,这样取消引用Iterator就会返回第一个元素。但是,这没有奏效。当我然后实例化一个新的int LinkedList并尝试通过取消引用begin()的结果来访问第一个元素时,我不检索列表的第一个元素,而是检索一个10位数字,例如'1453755360'

我的节点类只包含两个右/左节点指针和一个数据变量

linkedlist类

template <typename T>
class LinkedList{

public:
    LinkedList(){
        count =(0);
        head =(nullptr);
        tail =(nullptr);
    }

    void push_head(T input){

        Node<T> newNode = Node<T>(input);
        newNode.left = nullptr;
        newNode.right = head;

        head = &newNode;
        count++;
    }

    T front(){
        T& data = (head->data);
        return data;
    }

    void push_tail(T input){

        Node<T> newNode = Node<T>(input);
        newNode.right = tail;
        newNode.left = nullptr;

        tail = &newNode;
        count++;
    }

    T back(){
        T& data = (tail->data);
        return data;
    }


    Iterator<T> begin(){
        Iterator<T> test = Iterator<T>(head);
        return test;
    }


private:
    int count;
    Node<T> *head;
    Node<T> *tail;

};

这是我测试代码的地方

    LinkedList<int> ll;

    ll.push_tail(7);
    ll.push_tail(9);

    if (*(ll.begin()) == 9) {
        cout << "pass" << endl;
    } else {
        cout << "returned : " << *(ll.begin()) << endl;
    }

2 个答案:

答案 0 :(得分:1)

fgaam hello 实施要求如果string text; string cipherAlphabet; std::ifstream u("plaintext.txt"); //getting content from plainfile.txt, string is text std::stringstream plaintext; plaintext << u.rdbuf(); text = plaintext.str(); //to get text std::ifstream t("keyfile.txt"); //getting content from keyfile.txt, string is cipherAlphabet std::stringstream buffer; buffer << t.rdbuf(); cipherAlphabet = buffer.str(); //get cipherAlphabet;*/ string cipherText; string plainText; bool encipherResult = Encipher(text, cipherAlphabet, cipherText); bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText); cout << cipherText; cout << plainText; 为空,则必须设置push_back()相对于head的相同内容。

答案 1 :(得分:0)

您正在堆栈上分配节点对象,因此当它们超出范围时会自动销毁它们。您正在存储指向这些对象的指针,这些指针会在对象被销毁时悬空。您需要使用new来分配堆上的节点。

此外,push_front()在列表为空时未更新tail,并且在列表不为空时未更新现有head以指向新节点。与push_back()类似。

尝试更像这样的事情:

template <typename T>
struct Node
{
    T data;
    Node *left;
    Node *right;

    Node(const T &d = T(), Node *l = nullptr, Node *r = nullptr)
        : data(d), left(l), right(r) {}
};

template <typename T>
class NodeIterator {
public:
    typedef std::ptrdiff_t difference_type;
    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;
    typedef std::bidirectional_iterator_tag iterator_category;

    NodeIterator(Node<T> *input = nullptr) : cur(input) {}
    NodeIterator(const NodeIterator &) = default;
    NodeIterator(NodeIterator &&) = default;
    ~NodeIterator() = default;

    NodeIterator& operator=(const NodeIterator &) = default;
    NodeIterator& operator=(NodeIterator &&) = default;

    reference operator*() {
        return cur->data;
    }

    NodeIterator& operator++ () {
        if (cur) cur = cur->right;
        return *this;
    }

    NodeIterator operator++ (int) {
        NodeIterator tmp(*this);
        if (cur) cur = cur->right;
        return tmp;
    }

    NodeIterator& operator-- () {
        if (cur) cur = cur->left;
        return *this;
    }

    NodeIterator operator-- (int) {
        NodeIterator tmp(*this);
        if (cur) cur = cur->left;
        return tmp;
    }

    bool operator==(const NodeIterator &rhs) const {
        return (rhs.cur == cur);
    }

    bool operator!=(const NodeIterator &rhs) const {
        return (rhs.cur != cur);
    }

private:
    Node<T> *cur;
};

template <typename T>
class LinkedList {
public:
    typedef NodeIterator<T> iterator;

    LinkedList() : count(0), head(nullptr), tail(nullptr) {}

    ~LinkedList() {
        while (head) {
            Node<T> *tmp = head;
            head = head->right;
            delete tmp;
        }
    }

    void push_front(const T &input) {
        Node<T> *newNode = new Node<T>(input, nullptr, head);

        if (head) head->left = newNode;
        head = newNode;

        if (!tail) tail = newNode;

        ++count;
    }

    T& front() {
        return head->data;
    }

    void push_back(const T &input) { 
        Node<T> *newNode = new Node<T>(input, tail, nullptr);

        if (!head) head = newNode;

        if (tail) tail->right = newNode;
        tail = newNode;

        ++count;
    }

    T& back() {
        return tail->data;
    }

    iterator begin() {
        return iterator(head);
    }

    iterator end() {
        return iterator();
    }

private:
    int count;
    Node<T> *head;
    Node<T> *tail;    
};

然后你可以这样做:

LinkedList<int> ll;

ll.push_back(7);
ll.push_back(9);

auto iter = ll.begin();
if (*iter == 7) {
    cout << "pass" << endl;
} else {
    cout << "returned : " << *iter << endl;
}

你现在甚至可以这样做:

for (LinkedList<int>::iterator iter = ll.begin(), end = ll.end(); iter != end; ++iter) {
    cout << *iter << endl;
}

for (int i : ll) {
    cout << i << endl;
}