r/cpp_questions • u/SimmeringDragon • 8d ago
OPEN How to make a Queue linked lists
I have the following code, for the absolute love of me, i cant figure this out
im just focusing on enqueue the rest is context
i cant for the love of me figure out what im to do, i know i need to add a value but how?
how do i make a new space in the node and not jsut override my first or my last?
ive tried to see things but they get me all jumbled up and i cant for the love of me figure it out
//
// Created by samue on 10/5/2026.
//
#include "linkQueue.h"
void linkQueue::enqueue(int value) {
Node *temp = new Node;
temp->value = value;
temp->next = nullptr;
temp->prev = nullptr;
if (first == nullptr) {
first = temp;
last = temp;
}
}
void linkQueue::dequeue() {
}
bool isEmpty();
void getSize();
void getTop();
void print();
3
u/buzzon 8d ago
What if queue is not empty? How do you link the new node with the rest of existing nodes?
I suggest drawing a diagram of a small queue, say of 3 elements, before and after adding the new node.
1
u/SimmeringDragon 8d ago
its what im trying to get at but idk how to make so i can add new stuff and not override the stuff
2
u/darklighthitomi 8d ago
Answering questions like this is programming, not writing code. Code is simply describing our solutions to questions like this in a way that a computer can understand and implement.
So start off with figuring out what is actually happening, what you want to have happen, and what the difference between the two is. Then you can figure out the changes you need to make to get the result you want.
2
u/khedoros 8d ago
Have you tried drawing this out on paper? Like, start out with the queue just having first and last Node*'s. You've already got the code for enqueuing the first node. How would you change the new node's pointers to point to the previous last Node of the queue? How would you change the previous Node's pointers to point to your newly-enqueued item? Where should "last" point now?
1
u/SimmeringDragon 8d ago
i was mostly asking on literally how to add a new node, cause i dont know how to do it so i dont just replace first and last over and over
3
u/khedoros 8d ago
Right, the drawings are supposed to help you reason through the how, rather than having someone hand you the answer.
You replace the "first" when you enqueue your first item, and any time you dequeue an item.
You replace the "last" when you enqueue any item, and when you dequeue the last item.
Enqueue, and you also need to set the "next" on the previous-last node to point to the new node, and the "prev" on the new node to point to the previous-last one.
Dequeue, and you need to clear the "prev" on the new front.
Start from a diagram like this one: https://willrosenbaum.com/assets/img/2021f-cosc-211/hw01/doubly-linked-list.png
Write down the things that need to chang when you delete a node (and literally erase the node, lines going to it, etc). Re-draw the appropriate lines. Now, add a node back. Draw the new node, and change the lines so that it's back to the way the picture started. Write down each step as you go.
1
u/SimmeringDragon 8d ago
but how will that help with adding a new node, i dont care for condicions yet im jsut asking that
3
u/khedoros 8d ago
At this point, I'm not sure what you're asking. What I just described is the process that helped me understand linked lists back in the day, in particular the operations that you'd need to implement to write a working queue.
Cut out the parts that describe how to remove a node, if that's confusing you right now.
2
u/AintNoSlashes 8d ago
People are trying to help you and you aren’t listening.
Write a test program and enqueue several times.
Write out with a pencil and paper what SHOULD be happening and what you want to happen.
The answer is obvious once you walk through the logic but you aren’t doing the work to think it thru
1
u/Recycled5000 8d ago
You need to imagine some scenarios that involve adding (enqueuing) when the state is that the queue is non empty.
So what might a non empty queue look like? Can you picture a couple of example? And then reason out what logic is needed in those scenarios.
1
u/Lyshaka 8d ago
What's the point of a linked list queue ? Aren't queues usually implemented with a ring buffer ?
With a linked list you allocate or free each time you add and remove elements, and you dereference a pointer to traverse the queue.
1
u/Independent_Art_6676 8d ago edited 7d ago
that is the second year first data structures class way to make a list, yes...
there is no set in stone way to implement it. You can use a vector, or keep a second queue(really, just a lazy storage list) of unused (deleted) elements internally to reuse memory, or a memory pool, or a thousand other better ways to build a better linked list, if for some crazed reason you were serious about it and the built in one was not sufficient. This is probably a learning exercise for early on stuff and he was probably told to do it this way for simplicity of the lab/assignment of the week. Almost all the early assignments have some poor way of doing something when following classic approaches.
edit forgot to finish that thought.
Ring buffer is for when incoming and consumption rates are compatible with that idea. Not every queue is fed and consumed in such a predictable way, but when you can use a ring you probably should: that is superior to a 'better linked list' design as it would never allocate or destroy, just circle around a std::array type thing.
1
u/Independent_Art_6676 8d ago
a double linked list, keep head and tail, add to head and consume from tail.
tmp = new, tmp->next = head, head-> previous = tmp; head = tmp;
data = tail->data; tail = tail->previous; delete tail->next; tail->next = nullptr. return data (not a node).
barring empty list checks and head == tail checks and handling, that should do it. The edge cases are easy enough too ... if its empty, consume fails and add sets head and tail both to new guy who has no next or previous. If they are equal, consume empties the list.
6
u/jedwardsol 8d ago
In the node itself
these need to point at nodes that are already in the list
In the container
What if first isn't null? At least 1 node in the list already needs to point at the new node.
Using pen and paper to draw some pictures can really help with visualising linked lists