Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-09-2005, 04:50 PM   #1
Registered: Jan 2005
Posts: 104

Rep: Reputation: 15
queue and linked lists


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
Old 03-09-2005, 06:27 PM   #2
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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....
Old 03-09-2005, 08:08 PM   #3
Registered: Jan 2005
Posts: 104

Original Poster
Rep: Reputation: 15
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
linked lists w/ nodes? |2ainman Programming 1 05-19-2004 01:32 AM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 05:56 PM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration