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 8742677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:32:30+00:00 2026-06-13T11:32:30+00:00

I’m creating a queue class in c++ and am having trouble getting the front

  • 0

I’m creating a queue class in c++ and am having trouble getting the front function to work. It is supposed to print the value of the first node in the queue. My queue.cpp class is here

#include "queue.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

queue::queue()
{
   front_p = NULL;
   back_p = NULL;
   current_size = 0;
}

void queue::enqueue(int item)
{
    node newnode = node(item, NULL);
   if (front_p == NULL) //queue is empty
    {
       front_p = &newnode;
       back_p = &newnode;
    }
   else 
   {
       back_p->next = &newnode;
       back_p = &newnode;
   }
   current_size ++;
}

int queue::dequeue()
{
   //if there is only one node
    if (front_p == back_p)
    {
        front_p = NULL;
        back_p = NULL;
    }
    //if there are two or more
    else
        front_p = front_p->next;
    current_size --;
}

int queue::front()
{
    if (front_p != NULL)
        return (*front_p).data;
}

bool queue::empty()
{
    if (front_p == NULL && back_p == NULL)
        return true;
    else
        return false;
}

int queue::size()
{
    return current_size;
}

My header file (queue.h) is here

class queue
{
  public:
    queue(); // constructor - constructs a new empty queue.
    void enqueue( int item ); // enqueues item.
    int dequeue();  // dequeues the front item.
    int front();   // returns the front item without dequeuing it.
    bool empty();  // true iff the queue contains no items.
    int size();  // the current number of items in the queue.
    int remove(int item); // removes all occurrances of item 
      // from the queue, returning the number removed.

  private:
    class node  // node type for the linked list 
    {
       public:
           node(int new_data, node * next_node ){
              data = new_data ;
              next = next_node ;
           }
           int data ;
           node * next ;
    };

    node* front_p ;
    node* back_p ;
    int current_size ; // current number of elements in the queue.
};

test program (tester.cpp)

#include <iostream>
#include "queue.h"
#include <stdlib.h>
using namespace std;

int main(int argc, char * const argv[])
{
    queue q1;
    q1.enqueue(5);
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    q1.enqueue(10);
    cout << "front: " << q1.front() << endl;
    cout << "front: " << q1.front() << endl;
    cout << "size: " << q1.size() << endl;
}

makefile

all: tester

tester: queue.o tester.o
    g++ tester.o queue.o -o tester

tester.o: tester.cpp
    g++ -c tester.cpp

queue.o: queue.cpp queue.h
    g++ -c queue.cpp

clean:
    rm -f tester *.o

When I run my test program I get this:

front: 5
front: 6299744
front: 6299744
front: 10
front: 6299744
size: 2

As you can see, after the first enqueue, front returns what it is supposed to, the value of the front of the queue. But after that it returns some strange number and I have no idea where it comes from! Then when I enqueue again, it prints fine again. Only after calling front twice does it start printing messed up values. Can anyone help me understand whats going on?

  • 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-13T11:32:31+00:00Added an answer on June 13, 2026 at 11:32 am

    Your program runs into undefined behavior because you have pointers to memory you don’t own:

    void queue::enqueue(int item)
    {
       node newnode = node(item, NULL);
       if (front_p == NULL) //queue is empty
        {
           front_p = &newnode;
           back_p = &newnode;
        }
       else 
       {
           back_p->next = &newnode;
           back_p = &newnode;
       }
       current_size ++;
    }
    

    At the end of the function, newnode is destroyed, but front_p and back_p are still pointing to that memory location. Either allocate dynamically:

    node* newnode = new node(item, NULL);
    

    or use std::shared_ptr<node>.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.