Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to display the contents of my queue?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to display the contents of my queue?

    How do i write a method that displays the contents of my queue?

    // 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)   // 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;
          }
    //--------------------------------------------------------------
       public long peekFront()      // peek at front of queue
          {
          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;
          }
    //--------------------------------------------------------------
       }  // end class Queue
    import java.util.Scanner;
    class QueueApp
       {
       public static void main(String[] args)
          {
          Queue theQueue = new Queue(5);  // queue holds 5 items
     
          Scanner scannerObject = new Scanner(System.in);
     
          theQueue.insert(scannerObject.nextInt());
     
          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);
     
          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
    Last edited by rocafella5007; April 29th, 2009 at 04:48 PM.


  2. #2

    Default Re: Display the content method?

    Hi,

    Why don't you use a simple for like this:

    for (int i = 0; i < queArray.length; i++)
    			System.out.println(queArray[i]);

Similar Threads

  1. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM
  2. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM
  3. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM
  4. Replies: 2
    Last Post: March 4th, 2009, 05:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 06:30 AM