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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:44:58+00:00 2026-06-12T19:44:58+00:00

I modified a program which creates a Queue and then add or remove items

  • 0

I modified a program which creates a Queue and then add or remove items to it.

The problem in my code is that after I remove one item, and then add an item it goes into infinite loop and I’m not sure how to prevent it from happening.

My goal is to modify display() method only.

This is how I display Queue:

   public void display() 
   {
       int i = front;

           do {
               if (maxSize == nItems)
               {
                   if (i == size())
                       i = 0;

                   System.out.print(queArray[i++] + " ");
               }
               else if (maxSize < nItems)
               {
                   System.out.print("Too many queue items!");
                   break;
               }
               else
                   maxSize = nItems;
           }
           while (i != rear + 1 && !isEmpty());
       }

This is how I add and remove items:

   public void insert(long j)   // put item at rear of queue
      {
      if(rear == maxSize-1)         // deal with wraparound
         rear = -1;
      queArray[++rear] = j;         // increment rear and insert
      nItems++;                     // one more item
      }

   public long remove()         // take item from front of queue
      {
      long temp = queArray[front++]; // get value and incr front
      if(front == maxSize)           // deal with wraparound
         front = 0;
      nItems--;                      // one less item
      return temp;
      }
  • 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-12T19:44:59+00:00Added an answer on June 12, 2026 at 7:44 pm

    Queue

    Here is the source code for the same.

    import java.util.Arrays;
    
    public class Queue {
    
        private int enqueueIndex;// Separate index to ensure enqueue happens at the end
        private int dequeueIndex;// Separate index to ensure dequeue happens at the
                                // start
        private int[] items;
        private int count;
        // Lazy to add javadocs please provide
        public Queue(int size) {
            enqueueIndex = 0;
            dequeueIndex = 0;
            items = new int[size];
        }
        // Lazy to add javadocs please provide
        public void enqueue(int newNumber) {
            if (count == items.length)
                throw new IllegalStateException();
            items[enqueueIndex] = newNumber;
            enqueueIndex = ++enqueueIndex == items.length ? 0 : enqueueIndex;
            ++count;
        }
        // Lazy to add javadocs please provide
        public int dequeue() {
            if (count == 0)
                throw new IllegalStateException();
            int item = items[dequeueIndex];
            items[dequeueIndex] = 0;
            dequeueIndex = ++dequeueIndex == items.length ? 0 : dequeueIndex;
            --count;
            return item;
        }
    
        @Override
        public String toString() {
            return Arrays.toString(items);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program in linux which creates/modify a file when it detects a new
I have a problem on a program I'm making that I honestly can't find
I have an AppleScript program which creates XML tags and elements within an Adobe
We have a program that take specific XML files and imports them, then changes
I am trying run a program from a qmake .pro file which modifies the
Android stores it's programs in APK format, which is a modified version of ZIP/JAR.
I am supposed to modified this Sierpinski's triangle program to count the number of
Modified the code this time the objective to count the line of code was
I modified one method of JQuery UI Resizable to achieve a centered resize. This
The whole story: I have created a fancy .NET program which has an installer

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.