LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   queue and linked lists (https://www.linuxquestions.org/questions/programming-9/queue-and-linked-lists-299772/)

Palamides 03-09-2005 04:50 PM

queue and linked lists
 
Hi

I was wondering if anyone could tell me how to implement a queue with a single linked list in C++ as in, I realise that the head points to the front of the queue which is where you dequeue nodes and the tail is where you insert nodes, is that correct?
and does the head pointer point to the last node, or does the node point to the head? same with the tail, does the tail point to the first node, or vice versa?
thanks a lot

jtshaw 03-09-2005 06:27 PM

Ok... so you have a node. Node contains some sort of data, and a Node * to the next node. So when you start both Node head and Node tail will be pointing to NULL. This is a special case. When you enqueue a node when head = NULL then you make a new NULL. Set the data value, and point both head and tail to your new NULL. The next point of any newly enqueued node should be NULL.

When you dequeue you typically point a temp NODE * to head, and set head to head->next. After you are done with the temp NODE delete it.

When you enqueue a node and tail != NULL you create a new node with your data value, set tail->next to that new node, then set tail=tail->next.

I think that is about it... This sounds like a school assignment so I won't write the code for ya, but that might help you understand better....

Palamides 03-09-2005 08:08 PM

thanks for the help, that cleared up things a lot, I was getting a proper output except that the last node wouldn't print out, so I thought something was wrong with my logic but I figured a way around that.
btw it is a school assignment, i am writing templates for stack and queues using linked lists.


All times are GMT -5. The time now is 04:45 PM.