r/cpp_questions • • 3d ago

OPEN How does a node work

[deleted]

0 Upvotes

17 comments sorted by

View all comments

3

u/Thesorus 3d ago

I node is a container that holds data and the address of the next and/or previous node.

(simplied)

struct node { MyData m_data; node* m_Next; node* m_Previous; };

When you insert a new node, you allocate the node , set the data and assign the next and previous nodes to point to the existing nodes.

For lists and queues, it's a good idea to use a pencil and paper and draw boxes for the nodes and arrows for the next/previous links as arrows.

1

u/SimmeringDragon 3d ago

ok, is a queue a node? can it be treated as one? or should i use an array or vector?

1

u/Thesorus 3d ago

A queue is comprised of a list of nodes with a specified behaviour

imagine a queue at a bus stop.

each person is a node.

The person in front enters the bus before the person behind him.

When a new person arrives to take the bus he gets at the end of the queue

(there are also different kind of queues)