473,804 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Queue implementation

21 New Member
I write a queue program but it doesn't work properly:I enqueue an element and dequeue just after that, but the output is the first value and then many times of "Empty queue.."!Why ?Please help me.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. class Node{
  5. private:
  6.     int value;
  7.     Node * next;
  8. public:
  9.     Node(int n)
  10.     {
  11.         value=n;
  12.         next=NULL;
  13.     }
  14.     int getValue(){ return value;}
  15.     void setValue(int n) { value=n;}
  16.     Node *getNext(){ return next;}
  17.     void setNext(Node *nx){ next=nx;}
  18. };
  19. class Queue{
  20. private:
  21.     Node *front;
  22.     Node *back;
  23.     int size;
  24. public:
  25.     Queue();
  26.     ~Queue();
  27.     int Size(){ return size;}
  28.     void enqueue(int n);
  29.     int dequeue();
  30. };
  31. Queue::Queue()
  32. {
  33.     front=back=(Node *)NULL;
  34.     size=0;
  35. }
  36. void Queue::enqueue(int n)
  37. {
  38.     if(back){
  39.         Node *tmp=new Node(n);
  40.         back->setNext(tmp);
  41.         back=back->getNext();
  42.     }
  43.     else{
  44.         front=back=new Node(n);
  45.     }
  46.     size++;
  47. }
  48. int Queue::dequeue()
  49. {
  50.     int x;
  51.     if(front){
  52.          x=front->getValue();
  53.         Node *tmp=front;
  54.         front=front->getNext();
  55.         delete tmp;
  56.         size--;
  57.         return x;
  58.  
  59.     }
  60.     else{
  61.         cout<<"Empty queue:cannot dequeue!!";
  62.         return -1;
  63.     }
  64. }
  65. Queue::~Queue()
  66. {
  67.     int m;
  68.     while(front)
  69.          m=dequeue();
  70.  
  71. }
  72. main()
  73. {
  74.     Queue IntQueue;
  75.     for(int l=1;l<=5;l++){
  76.  
  77.         IntQueue.enqueue(l);
  78.         cout<<"Current size:"<<IntQueue.Size()<<";value in front of queue :"<<IntQueue.dequeue()<<endl;
  79.         cout<<"        Current size:"<<IntQueue.Size()<<endl;
  80.         }
  81.  
  82.  
  83.  
  84.     getch();
  85. }
  86.  
May 26 '07 #1
1 3041
scruggsy
147 New Member
When you dequeue the only element in the queue, front gets set to NULL via front=front->getNext. But you need to reset back to NULL at that same time, otherwise enqueue() adds the next node to the non-existent back node.
May 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

29
4244
by: Paul L. Du Bois | last post by:
Has anyone written a Queue.Queue replacement that avoids busy-waiting? It doesn't matter if it uses os-specific APIs (eg WaitForMultipleObjects). I did some googling around and haven't found anything so far. Because I know someone will ask: no, the busy-waiting hasn't been a problem in my app. I'm just interested in reading the code. p
4
2780
by: Sachin Jagtap | last post by:
Hi, I am getting exception while poping item from queue, I am writing messages coming from client in a queue in one thread and then in other thread i am reading from queue and writing in file. I have not implemented any syncronization between reading and wrinting, just i checking size of queue, if it is not empty i am reading from queue and wrting to file. somethign like
2
2721
by: Hollywood | last post by:
I have a system in which I have a single thread that places data on a Queue. Then I have one worker thread that waits until data is put on the thread and dequeues the Queue and processes that information. Works just fine. However, I possibly need 1..m worker threads that can act on the same piece of data, i.e. when a dequeue happens on the Queue it can't be removed from the Queue until all threads have used it. I've tried to come up...
3
5159
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main': G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to `Queue<char>::Queue()' G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In function `Z10do_commandcR5QueueIcE':
13
2555
by: Jonathan Amsterdam | last post by:
I think there's a slight design flaw in the Queue class that makes it hard to avoid nested monitor deadlock. The problem is that the mutex used by the Queue is not easy to change. You can then easily get yourself into the following situation (nested monitor deadlock): Say we have a class that contains a Queue and some other things. The class's internals are protected by a mutex M. Initially the Queue is empty. The only access to the...
5
3115
Rooro
by: Rooro | last post by:
Hello everyone i'm working on : " Familiar childhood games such as hide and Go Seek and Tag involve determining the player who is to be "It". One method has the players stand in a circle while one of the players recites some rhyme to count off players, eliminating every nth player, where n depends on the number of the syllables in the counting rhyme. This process stops when only one player, the one to be "It" remains. This process...
2
5310
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find after I've run through my unit test cases. Test Case:
2
2936
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include <cassert // for assert #include <new // for bad_alloc using namespace std; //private:{Queue::Queue(const Queue& Q)}
14
16977
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static priority queue, which is an array heap, and for example i/2 is the father of i and so on.. the code of the static priority queue is this: int i= ++CurrentSize; while(i != 1 && X > heap) { // cannot put x in heap heap = heap; //move...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.