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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:26:11+00:00 2026-05-30T17:26:11+00:00

// Queue.java // demonstrates queue // to run this program: C>java QueueApp class Queue

  • 0
 // Queue.java
 // demonstrates queue
 // to run this program: C>java QueueApp

 class Queue
 {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

public Queue(int s)          // constructor
  {
  maxSize = s;
  queArray = new long[maxSize];
  front = 0;
  rear = -1;
  nItems = 0;
  }

public void insert(long j)   
  {
  if(rear == maxSize-1)         
     rear = -1;
  queArray[++rear] = j;        
  nItems++;                    
  }


public long remove()         
  {
  long temp = queArray[front++];
  if(front == maxSize)           
     front = 0;
  nItems--;                     
  return temp;
  }

 public long peekFront()     
  {
  return queArray[front];
  }

 public boolean isEmpty()    // true if queue is empty
  {
  return (nItems==0);
  }

public boolean isFull()     // true if queue is full
  {
  return (nItems==maxSize);
  }

public int size()           // number of items in queue
  {
  return nItems;
  }


public void display()
{ int startFront = front;

  for (int j = front ;j <nItems; j++ )
  {  
      System.out.println(queArray[j]);
      if (j == nItems-1 )
        {       j=0;
                System.out.println(queArray[j]);
        }   


      if (j==startFront-1)
          return;

       }
          }
         }  

 class QueueApp
  {
        public static void main(String[] args)
  {
  Queue theQueue = new Queue(5);  // queue holds 5 items

  theQueue.insert(10);            // insert 4 items
  theQueue.insert(20);
  theQueue.insert(30);
  theQueue.insert(40);

  theQueue.remove();              // remove 3 items
  theQueue.remove();              //    (10, 20, 30)
  theQueue.remove();

  theQueue.insert(50);            // insert 4 more items
  theQueue.insert(60);            //    (wraps around)
  theQueue.insert(70);
  theQueue.insert(80);


  theQueue.display();


  while( !theQueue.isEmpty() )    // remove and display
     {                            //    all items
    long n = theQueue.remove();  // (40, 50, 60, 70, 80)
     System.out.print(n);
     System.out.print(" ");
     }
  System.out.println("");

  }  // end main()
}  // end class QueueApp

Okay, this is the basic, out of the book, queue code. I am attempting to create a display method that will show the queue in order, from front to back. (This is an assignment, i know this is not practical….) If i run the program as is, it will display the queue in order from front to rear(at least that is what i believe i did). The problem i am having is if i change the nItems, it ceases to work. For example if you add the line of code, theQueue.remove(); right above the call to the display, the method ceases to work, i know it is because the front is now = to 4, instead of 3,and it will not enter the for method which needs the front to be < nItems, 4<4 is not true so the for loop does not initiate.

  • 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-30T17:26:13+00:00Added an answer on May 30, 2026 at 5:26 pm

    Simply use something like:

    public void display() {
        for (int i = 0; i < nItems; i++) {
            System.out.println(queArray[(front + i) % maxSize]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: public class MyClass { private Queue<MyOtherClass> myQueue; }
First of all, this is my code (just started learning java): Queue<String> qe =
Tts.java public void onInit(int arg0) { Bundle dataBundle = Tts.this.getIntent().getExtras(); speech = dataBundle.getString(IMSENS); tts.setLanguage(Locale.ENGLISH);
I have a queue of task in java. This queue is in a table
I am running a listener program on a JMS queue hosted in Sun Java
How can I instantiate a JMS queue listener in java (JRE /JDK / J2EE
I am looking for a fast queue implementation in Java. I see that LinkedList
I want to use java.util.ConcurrentLinkedQueue as a non-durable queue for a Servlet. Here's the
I have several queues that all extend a Queue class. I want to do
I have created a priority queue using the Java API and I want to

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.