Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:58:59+00:00 2026-06-17T20:58:59+00:00

I implement a queue myself. when testing, I expect the dequeue to reutrn when

  • 0

I implement a queue myself. when testing, I expect the dequeue to reutrn when the queue is empty by testing if the tail pointer point to the head node. However, the address of the head node and the one pointer point are different when the point indeed points to the head, hence the queue never stop dequeue even if there is nothing in it.
This confuse me a lot. Any suggestion on this?
thanks a lot.

//header
#ifndef QUEUE_H
#define QUEUE_H

struct node
{
int val;
struct node* next;

};

class queue
{
private:
    node head;
    node* tail;
public:
    queue();
    void enqueue(int val);
    void dequeue(int& holder, bool& v);
};





#endif
// queue.cpp
#include "queue.h"

using namespace std;
queue::queue()
{
    tail = &head;

}
void queue::enqueue(int val)
{
    node* tmp = new node;
    tmp->val = val;
    if(tail == &head)
    {
            tail = tmp;
            tmp->next = &head;
            head.next = tmp;
    }
    else
    {
            node* holder = head.next;
            head.next = tmp;
            tmp->next = &head;
            holder->next = tmp;
    }
}

void queue::dequeue(int& holder,bool& v)
{

    if(tail == &head)
   {
            v = false;
   }
    else
    {
            node* cur = tail;
            tail = tail->next;
            holder = cur->val;
            v = true;
            delete cur;

    }

}
//test.cpp
#include <iostream>
#include "queue.h"
using std::cout;
using std::endl;
int main()
{
int ary[] = {1,2,3,4,5};

queue myq;
for(int i = 0;i< sizeof(ary);i++)
{
    myq.enqueue(ary[i]);
}
int tmp;
bool flag;
for(int i = 0;i<=7;i++)
{
    myq.dequeue(tmp,flag);
    if(flag)
            cout<<"number is "<<tmp<<endl;
    else
            cout<<"queue empty"<<endl;
}
return 0 ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:59:00+00:00Added an answer on June 17, 2026 at 8:59 pm

    I could give you the exact fix, but that would teach you absolutely nothing, you’d just copy and paste that into your code and go away do something else. So I’m going to help you with a line of code to add to show what the current value of &head is, and thus help identify what the actual problem is…

    Add this line to enqueue (at the top of the function) and dequeue:

         std::cout << __FUNCTION__ << ": " << &head << std::endl;
    

    It will print the name of the function and the address off head. (You may need to include iostream as well)

    Pay particular attention to how many printouts you get!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a priority queue of Node* , where Node is a
I found the article regarding what I want to implement at Queue implementation in
I need to implement a FIFO queue for messages on a game server so
I'm trying to implement a Polymorphic Queue. Here is my trial: QQueue <Request *>
I am planning to implement a bounded queue without using the Queue<T> class. After
Somebody knows how to implement Oracle Advance Queue from C# using PL/SSQL and ODP.NET?
I am currently trying to implement a job queue in php. The queue will
The current Go library doesn't provide the queue container. To implement a simple queue,
I'm trying to implement a simple priority queue from array of queues. I'm trying
I want to implement a queue, that is hit by multiple threads. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.