Nodes

The basic unit of storage for a linked list is a node. A node stores data and one or more pointers to other nodes. At its most basic, a linked list node consists of data plus a pointer to the next node in the linked list.

The following shows how to declare a simple node for storing doubles.

struct Node{
  double data_;    // the data portion of the node
  Node* next_;     // a pointer to the next node
};

To create linked lists that hold other data types the data portion of a node would need to be a different data types.

class Hamster
  char name_[50];
  int age_;
public:
....
};

//Each node holds one instance of Hamster.
struct Node{
  Hamster data_;
  Node* next_;
};

Other data structures such as trees, also store data in nodes. If you wish to create a library of data structures and want to avoid naming conflicts, you can nest the Node declaration within your linked list class. This also allows you to use a struct of a node and not worry about access permissions

results matching ""

    No results matching ""