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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:00:10+00:00 2026-05-30T01:00:10+00:00

I am trying to make a simple Queue class. Now I am stuck. Its

  • 0

I am trying to make a simple Queue class. Now I am stuck. Its almost complete. I have figured out that only one function “pop” is causing the problem. Can anyone please tell me what should I do.

Here is the code:
(queue.h)

#ifndef  _QUEUE_
#define  _QUEUE

#include <iostream>
#include <string>
using namespace std;


struct Stuff
{
    string name;
    int roll;
};


class Queue
{
private:

struct Node
{
    Stuff data ;
    struct  Node * next;
};
Node *front;
Node *back;
int qsize;
const int MAX;
public:

Queue(int size = 5);
~Queue();

bool isfull() const;
bool isempty() const;
int queuesize() const;

bool push(const Stuff & item);
bool pop();
Stuff first();
Stuff last();


};




#endif

queue.cpp

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

Queue::Queue(int size)
:MAX(size)
{
front = back = 0;
qsize = 0;

}
bool Queue::isempty() const
{
if(qsize == 0)
    return true;
else return false;
}
bool Queue::isfull() const
{
if(qsize == MAX)
    return true;
else 
    return false;
}
Queue::~Queue()
{
Node * temp;
while(front != NULL)
{
    temp = front;
    front = front->next;
    delete temp;
}

}
int Queue::queuesize() const
{
return qsize;
}

bool Queue::push(const Stuff & swag)
{
if( isfull() )
    return false;

Node *add = new Node;

if(add == NULL)
    return false;
add->data = swag;  
add->next = NULL;

if(front == NULL)
    front = add;
else
    back->next = add->next;
back = add;
qsize++;

return true;
}


bool  Queue::pop()
{
if(isempty() )
    return false;
if(front == NULL)
    return false;
Node *temp =front;


// I think this part is doing something wrong.
front = front->next;



delete temp;
qsize--;
if(qsize == 0)
    back = NULL;
return true;

}
Stuff Queue::first()
{
return front->data;
}
Stuff Queue::last()
{
return back->data;
}

main.cpp

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

 int main()
 {
Queue a(5);
Stuff data;
data.name = "icp";
data.roll= 755;
a.push(data);
Stuff x = a.last();

cout << x.name << "\t" << x.roll << endl;
data.name = "sms";
data.roll= 12544;
a.push(data);
x = a.last();   
cout << x.name << "\t" << x.roll << endl;

data.name = "jmc";
data.roll= 98740;
a.push(data);
x = a.last();
cout << x.name << "\t" << x.roll << endl;

cout << a.queuesize() << endl;

/////////////
x = a.first();  
cout << x.name << "\t" << x.roll << endl;
a.pop();

x = a.first();  
cout << x.name << "\t" << x.roll << endl;
a.pop();

x = a.first();  
cout << x.name << "\t" << x.roll << endl;
a.pop();


//// 
cin.get();
cin.get();
return 0;
}

The Program crashes after popping out the first element.
I have pointed out the part that I think is making some problem. Thanks in advance 🙂

  • 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-05-30T01:00:12+00:00Added an answer on May 30, 2026 at 1:00 am

    I think the problem is caused by push function.
    the following code

    if(front == NULL)
        front = add;
    else
        back->next = add->next;
    

    back->next is set to be add->next, which is NULL. add->next = NULL;

    the add is next one to back, so it should be back->next = add;

    You think the problem in pop function is because the front is NULL, so front->next is wrong.

    Hope this could help you.

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

Sidebar

Related Questions

I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
Trying to make simple jQuery function to create a scrollToTop button that fades in
I'm trying to make simple animation that would repeat several times (or infinitely). It
I trying to make a simple image gallery, where I have several radio button
I'm using a simple queue implementation and I'm trying to make a simple program
I'm trying to make a simple tournament system, and now I've come to another
I am trying to make simple testing but failed because I have no clue
I'm trying to make simple http server, that can be pause and resume,, I've

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.