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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:16:58+00:00 2026-05-29T09:16:58+00:00

I’m trying to implement a queue using an array. Here is my code: #include

  • 0

I’m trying to implement a queue using an array. Here is my code:

#include <iostream.h>
#define SIZE 5

class queue
{
    int *Queue, front, rear;

    public:
    queue() {
        Queue = new int[SIZE];
        front = rear = -1;
    }

    void push() {
        if (rear == (SIZE-1)) {
            cout<<"\n Overflow!";
        } else {
            rear++;
            cout<<"\n Enter element: ";
            cin>>Queue[rear];   
        }
    }

    void pop() {
        if (front == rear) {
            cout<<"\n Underflow!";
        } else {
            cout<<"\nElement popped: "<<Queue[++front];
        }
    }

    void display() {
        if (front == rear) {
            cout<<"\n Queue Empty";
        } else {
            for(int i = (front+1); i<=rear; i++) {
                cout<<Queue[i]<<" ";
            }
        }
    }
};


int main()
{
    int choice;
    queue q;
    while(choice != 4)
    {
        cout<<"\n\n Enter your choice :"
            <<"\n 1. Push an element into Queue."
            <<"\n 2. Pop an element from Queue."
            <<"\n 3. Display the Queue."
            <<"\n 4. Exit the program.\n\n";
        cin>>choice;

        switch (choice) {
            case 1:
                q.push();
                break;
            case 2:
                q.pop();
                break;
            case 3:
                q.display();
                break;
            case 4:
                break;
        }
    }

    return 0;
}

The thing is that once the overflow is met, even after popping an element the rear remains the same and another element is not added when there is a vacant space where it can go.

The solution for this could be to shift every element one place ahead so that there is empty spot at the end but I am having trouble with the shift. Also, if I try inserting after popping 2-3 times before reaching overflow then it still gives overflow even when there are only 3 elements in the queue.
How can I solve this?

  • 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-29T09:16:59+00:00Added an answer on May 29, 2026 at 9:16 am

    You never reset front or rear, they keep increasing forever.

    the overlflow test is also wrong:

    if (rear == (SIZE-1)) {
            cout<<"\n Overflow!";
    

    what you should be testing is if your are about to overwrite front. I think you will benefit from keeping just front and the number of elements N instead. Then overflow and underflow becomes N>SIZE and N==0 instead. Then just increase and decrease N as you pop and push. Keep front as it is but also keep it modulo SIZE.

    Also, as written in a comment. No need to move around the data.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.