【问题标题】:Accessing an element within my custom linked list class访问我的自定义链表类中的元素
【发布时间】:2013-10-23 15:10:51
【问题描述】:

我正在构建自己的链表类,但在弄清楚如何编写一些函数来帮助我遍历这个列表时遇到了一些问题。这是我第一次从头开始构建链表,所以如果我的方法非常规,请告诉我什么可能更传统。

我想在 List 类中编写一个函数,它允许我递增到名为 getNext() 的下一个元素以及 getPrev();

我是这样写getNext的:

T* getNext(){return next;}

但是它告诉我 next 没有在范围内声明。我还想编写一个函数,让我可以访问和修改列表中的对象。我正在考虑使用方括号运算符,但首先我需要编写一个函数来返回数据成员。也许如果我在我的 pop 函数中采取类似的方法......现在考虑一下。不过,我仍然很感激任何建议。

这是我的 List 类:

#ifndef LIST_H
#define LIST_H


//List Class
template <class T>
class List{
    struct Node {
        T data;
        Node *next;
        Node *prev;
        //Constructs Node Element
        Node(T t, Node* p, Node* n) { data = (t); prev = (p); next = (n); }
//      T *getNext() {return next;}
    };
        Node *head;
        Node *tail;
public: 
    //Constructor
    List() { head = NULL; tail=NULL; }
    //Destructor
    ~List() { 
        while(head){
            Node * temp(head);
            head = head->next;
            delete temp;
        }
    }
    //is empty
    bool empty() const {return (!head || !tail ); }
    operator bool() const {return !empty(); }
    //Push back
    void push_back(T data) {
        tail = new Node(data, tail, NULL);
        if(tail->prev) //if the node in front of tail is initilized
            tail->prev->next = tail;

        if( empty() )
            head = tail;
    }
    //Push front
    void push_front(T data) {
        head = new Node(data, NULL, head);
        if(head->next)//if the node following head is initilized
            head->next->prev = head;

        if( empty() )
         tail = head;
    };
    T pop_back() {
        if( empty() )
            throw("Error in List: List is empty\n");

        Node* temp(tail);
        T data(tail->data);
        tail = tail->prev;

        if( tail )
            tail->next = NULL;
        else
            head = NULL;

        delete temp;
        return data;
    }
    T pop_front() {
        if (empty())
            throw("Error in List: List is empty\n");

        Node* temp(head);
        T data(head->data);
        head = head->next;

        if(head)
            head->prev=NULL;
        else
            tail = NULL;

        delete temp;
        return data;
    }

    T getNext(){return next;}
};


#endif

【问题讨论】:

    标签: c++ linked-list doubly-linked-list


    【解决方案1】:

    getNext 应该是struct Node 的一部分并返回Node*

    Node* getNext() { return next; }
    

    然后你可以从中得到价值。

    如果您必须将它作为list 本身的一部分,我不建议这样做,它需要采用Node 的参数,您想要nextnext

    Node* getNext(Node* n) {return n->next;}
    

    再次,我推荐第一个选项。

    这是一个包含这两者的近似整个班级:

    template<typename T>
    class List {
      public:
        struct Node {
          Node* next, prev;
          T data;
    
          //some constructor and stuff
    
          Node* Next() {return next;}
        }
    
        //some constructors and other functions
    
        Node* getNext(Node* _n) {return _n->Next();}
    }
    

    然后使用:

    int main() {
      List<int> l;
      //add some stuff to the list
      //get the head of the list
      List<int>::Node* head = l.head; //or some corresponding function
    
      //then
      List<int>::Node* next = head->Next();
      //or
      List<int>::Node* next2 = l.getNext(head);
    }
    

    【讨论】:

    • 第一个参数在逻辑上是有意义的,为了从列表中调用它,我尝试做这样的事情:Node* getNext(){ return Next(); }//within list 当我尝试第二种方式时,它输出“class List 没有名为 getNext() 的成员"
    • 这可能是因为您说Next() 并且没有与之关联的对象。如果您在我的第二种方法中注意到该函数需要一个参数。
    • 我曾尝试使用第二种方法,但是当我编译累时出现此错误:no matching function for call to âList&lt;CLASSNAME&gt;::getNext()â
    • 另外,澄清Next() 电话。结构的next 成员输出为out of the scope,所以我创建了这样的函数来尝试在节点结构Node* Next() { return next;} 中修复它但是有类似的错误。
    • 啊好的,没有匹配的函数调用列表错误是因为您必须向List::getNext提供一个参数...例如myList.getNext(myNode)。我再次重申,第二个想法不是要走的路。你应该坚持第一个,就像Next
    【解决方案2】:

    因为它是Node 结构的成员,而getNextList 的成员。您应该从 Node 类型的对象访问它。

    【讨论】:

      【解决方案3】:

      对于初学者,getNext() 不应返回指向模板类的指针,而应返回指向 Node 结构的指针。

      应该是这样的

      Node* getNext(){return next;}
      

      【讨论】:

        猜你喜欢
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多