链接列表帮助,迭代器问题

时间:2015-09-29 01:28:49

标签: c++ linked-list iterator

所以我必须为课程创建一个链接列表,我仍然坚持使用List::Current()函数。出于某种原因,当我尝试调用该函数时,我收到了处理错误。

List.h

class List {
private:
    struct Node {
        int data;
        Node* next;

        Node() : next(NULL){} //define our own default constructor
        Node(int data) : next(NULL), data(data){}
    };

    typedef struct Node* NodeRef;

    NodeRef head;
    NodeRef tail;
    NodeRef iterator; //points to one node at a time
    int size;

public:
    int current();


List.cpp

// initialize the values when they are instantiated
List::List() : head(NULL), tail(NULL), iterator(NULL), size(0) 
{}

int List::current() {
    return iterator->data;
}

void List::push_front(int data)             //Inserting a new node in     the front of the list
{
if (size == 0)                          //If there is no nodes in the        list, execute the if statement
{
    head = new Node(data);              //create a new node, and have head point to it
    iterator = tail = head;                     //have tail point to the new node also.

}
else                                    //If there are nodes in the list, execute the else statement
{
    NodeRef newNode = new Node(data);   //create a new node
    newNode->next = head;               //have the next pointer point to the head of the next node.
    head = newNode;                     //have the head pointer point to the new node inserted at the beginning of the list
}
size++;                                 //Increment the size counter

}

void List::push_back(int data)              //Inserting a node at the end of a list
{
if (size == 0)                          //If there are no nodes in the list, execute the if statement
{
    tail = new Node(data);              //Create a new node and have the tail pointer point to it.
    iterator = head = tail;                     //Have the head pointer point to the new node also.
}
else                                    //If there is atleast 1 node in the list, execute the else statement
{
    NodeRef newNode = new Node(data);   //Create a new node
    tail->next = newNode;               //Have the tail
    tail = newNode;                     //Have the tail pointer point to the new node.
    newNode->next = NULL;
}
size++;

}
void List::begin() //Set the iterator to the head of the list
{
iterator = head;
}

void List::scroll() //Allows us to scroll through the list
{
if (iterator == NULL)
    cout << "Iterator is pointing to null" << endl;
else
    iterator = iterator->next;
}


LinkedList.cpp

#include "stdafx.h"
#include "List.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[]) {
    List B; //Create a new list

    B.push_front(5);
    B.push_front(4);
    B.push_front(3);
    B.push_back(10);

    cout << B.current() << endl;

    system("PAUSE");
    return 0;
}

我留下了一些代码,因为我没有想到列出正常工作的其他功能是必要的。如果你想要我发布的所有内容。

我认为这解决了我的问题。

1 个答案:

答案 0 :(得分:1)

您的问题是您没有设置迭代器。

就我个人而言,我不会将它作为类的一部分包含在内,并且有类似begin()head()的东西,它使用头指针检索迭代器类实例。然后current和迭代方法将成为迭代类的一部分。

但对于您当前的设计,您可以检查push_front以查看迭代器是否为NULL,如果是,则将其设置为head。或者您可以使用begin_iteration方法将其设置为头部,这也允许您在列表中执行多次迭代。

修改

现在您已经透露了整个实现,您需要在2个地方设置iterator。在push_front结尾处,如果push_back中没有头部。换句话说,无论你在哪里设置head,都需要设置迭代器。

另外,你如何向前移动迭代器?你能重启一次迭代吗?