访问我的自定义链接列表类中的元素 [英] Accessing an element within my custom linked list class

查看:102
本文介绍了访问我的自定义链接列表类中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立自己的链表类,我有一些问题,找出如何写一些函数,以帮助我遍历这个列表。这是我第一次从头构建一个链表,所以如果我的方法是非常规的,请让我知道什么可能更传统。

I'm building my own linked list class and I'm having some issues figuring out how to write some functions to help me traverse this list. This is my first time building a linked list from scratch, so if my approach is unconventional please let me know what might be more conventional.

我想写一个函数,在List类中,允许我递增到下一个元素getNext()和一个getPrev();

I'd like write a function, within the List class that allows me to increment to the next element called getNext() as well as one that getPrev();

我这样写了getNext: p>

I wrote getNext like this:

T* getNext(){return next;}

但它告诉我下一个没有在范围内声明。我也想写一个函数,让我访问和修改列表中的对象。我正在考虑使用括号运算符,但首先我需要写一个函数来返回数据成员。也许如果我采取一个类似的方法,我在我的流行函数..现在思考它。

However it tells me next is not declared within the scope. I'd also like to write a function that lets me access and modify the object within the list. I was considering using the bracket operator, but first I need to write a function to return the data member. Perhaps If I take a similar approach as I did within my pop functions.. thinking about it now. However, I'd still appreciate any advice.

这是我的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


推荐答案

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

getNext should be part of the struct Node and return a Node*

Node* getNext() { return next; }

然后从中获取该值。

如果你必须拥有列表本身的一部分,我不推荐它需要一个参数 Node 您想要

If you have to have it part of the list itself, which I would not recommend it will need to take a parameter of what Node you would like the next of:

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

推荐第一个选项。

这是一个大致的整个类与这两个:

Here is an approximate whole class with both of these:

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);
}

这篇关于访问我的自定义链接列表类中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆