LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 03-09-2005, 05:50 PM   #1
Palamides
Member
 
Registered: Jan 2005
Posts: 104

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

Rep: Reputation: 66
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, 09:08 PM   #3
Palamides
Member
 
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.
 
  


Reply


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 03:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 11:51 AM
linked lists w/ nodes? |2ainman Programming 1 05-19-2004 02:32 AM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 06:56 PM
c++ linked lists jclark00001 Programming 10 02-23-2003 03:40 PM


All times are GMT -5. The time now is 09:44 PM.

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