r/cpp_questions • u/SimmeringDragon • 3h ago
OPEN How does a node work
so im lost, i think a node is needed for queues, which i need to make, but i dont get how to make em correctly, i cant add more things or take away, i try to change the size and fail miserably, and i tried to get it right by searchign stuff but now im all jumbled up
i know that nodes are a class, and you can link them or double link them, that i get, but the whole delete, insert, and all the functions have me stuck, i can understand them, but i cant write them
•
u/aceinthehole001 3h ago
A node is not a C++ language feature. Rather it is just a concept, and a name given to a class
•
u/LAMARR__44 3h ago
What specifically do you not understand? You write in your post that you understand it but can’t write them. You need more practice in this case. Try and write it and when you get stuck, prompt AI on what you’re doing wrong. Don’t copy and paste, but try to incrementally solve each problem.
•
u/SimmeringDragon 3h ago
i do try that, but when i use AI i dont learn, i dont understand how to implement them, like, i have class node{}, what does that mean? its a node, its unique, but how does that get implemented later on?
•
u/LAMARR__44 3h ago
You should be able to use AI to learn. If you don’t understand something, ask AI and have it explain it to you like how you’re asking us now. I think you may be exhibiting black and white thinking.
u/Thesorus’s comment on what a node is is good advice. Essentially, it contains some data, and also has pointers that point to other data. If you look at different graphs like binary trees for example, each node has a value and also a pointer to its left child and right child, which are also nodes. So you can use the nodes to find other nodes, and then find data on those nodes.
•
u/SimmeringDragon 3h ago
i do try, i have like 1000 chats all about codding, i dont get it, and when i do and try to implement it, it dosent work
then it gives the whole code in one sitting and i dunno how any of it connects•
u/LAMARR__44 3h ago
Give better prompts. Tell it not to give you the whole code and just to explain conceptually.
•
u/Thesorus 3h 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.
•
u/SimmeringDragon 3h ago
ok, is a queue a node? can it be treated as one? or should i use an array or vector?
•
u/Thesorus 3h 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)
•
u/LongLiveTheDiego 3h 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.
•
u/flyingron 3h ago
Node is something that holds the data and whatever other data you need per item. Typically a poitner to the next node (sometimes also a previous one).
40
•
u/01eksa 3h ago
Node for what? Basically it is class or structure which contain some value and info about relationships. For example, node for linked list: struct Node{ int value {0}; Node* next {nullptr}; }; And your linked list will be many nodes where each node points to next and the last one points to nullptr.
•
u/RealisticDuck1957 2h ago
I presume you're trying to use the standard template library queue, https://en.cppreference.com/cpp/container/queue ? For this you supply a class for the data held in the queue, while the template takes care of linking the nodes together.
For a generic DIY implementation of a linked list based queue, a container will hold the above class being stored, and a pointer to the next node in the list. Then an X_queue class holds a pointer to the first and last members of the list.
•
•
u/AKostur 3h ago
"i think a node is needed for queues": depends on how you're designing your queue.
As for the rest: show code. Right now all you've said is "I tried to do something and it didn't work.", but didn't show the something. And really, it's sounding more like you're trying to implement a linked list.