473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Queue implementation

21
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 3020
scruggsy
147 100+
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
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...
4
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....
2
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...
3
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':...
13
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...
5
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...
2
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...
2
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.