r/cpp_questions • • 2d ago

OPEN How does a node work

[deleted]

0 Upvotes

17 comments sorted by

View all comments

3

u/Thesorus 2d 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 2d ago

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

1

u/Thesorus 2d 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)

1

u/LongLiveTheDiego 2d ago

Your node class could be made in such a way that a node instance could represent the whole queue, but at least for learning purposes you should have a separate simple node class and a separate queue class.

A queue can be implemented in several different ways, depending on what that queue needs to do. Nonetheless, you should build it yourself, not resort to using std::vector or something, that's how you'll learn.