简体   繁体   中英

How to keep track of last added element of Linked List using static pointer to Node?

class Node{ public: int info; Node* link; static int count; static Node* endNode; Node(){ this->info = 0; this->link = NULL; if(count==0) endNode = this; else count++; } void insert(int x){ Node *p = new Node; p->info = x; p->link = NULL; endNode->link = p; endNode = p; }

The classic approach is to have the count and end pointer in the Linked List class:

class Linked_List
{
    int node_count;
    Node * p_head;
    Node * p_tail;
};

The p_tail member points to nullptr or the last (ending) node.

Moving the count member to a Linked_List class allows for different linked lists to have different node quantities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM