Accessing an element within my custom linked list class

妖精的绣舞 提交于 2019-12-11 09:57:09

问题


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.

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

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.

Here is my List class:

#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

回答1:


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

Node* getNext() { return next; }

Then from that you can get the value.

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

Again, I recommend the first option.

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

then to use:

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



回答2:


for starters getNext() should not return a pointer to the template class, it should return a pointer to the Node structure.

So it should be

Node* getNext(){return next;}



回答3:


Because it's a member of Node struct and getNext is member of List. You should access it from an object of type Node.



来源:https://stackoverflow.com/questions/19387577/accessing-an-element-within-my-custom-linked-list-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!