Java Program to Implement Queue

«
»
This is a Java Program to implement a queue using array. Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes queue a First-In-First-Out (FIFO) data structure.

Here is the source code of the Java Program to implement a queue using array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java Program to Implement Queue
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. /*  Class arrayQueue  */
  8. class arrayQueue
  9. {
  10.     protected int Queue[] ;
  11.     protected int front, rear, size, len;
  12.  
  13.     /* Constructor */
  14.     public arrayQueue(int n) 
  15.     {
  16.         size = n;
  17.         len = 0;
  18.         Queue = new int[size];
  19.         front = -1;
  20.         rear = -1;
  21.     }    
  22.     /*  Function to check if queue is empty */
  23.     public boolean isEmpty() 
  24.     {
  25.         return front == -1;
  26.     }    
  27.     /*  Function to check if queue is full */
  28.     public boolean isFull() 
  29.     {
  30.         return front==0 && rear == size -1 ;
  31.     }    
  32.     /*  Function to get the size of the queue */
  33.     public int getSize()
  34.     {
  35.         return len ;
  36.     }    
  37.     /*  Function to check the front element of the queue */
  38.     public int peek() 
  39.     {
  40.         if (isEmpty())
  41.            throw new NoSuchElementException("Underflow Exception");
  42.         return Queue[front];
  43.     }    
  44.     /*  Function to insert an element to the queue */
  45.     public void insert(int i) 
  46.     {
  47.         if (rear == -1) 
  48.         {
  49.             front = 0;
  50.             rear = 0;
  51.             Queue[rear] = i;
  52.         }
  53.         else if (rear + 1 >= size)
  54.             throw new IndexOutOfBoundsException("Overflow Exception");
  55.         else if ( rear + 1 < size)
  56.             Queue[++rear] = i;    
  57.         len++ ;    
  58.     }    
  59.     /*  Function to remove front element from the queue */
  60.     public int remove() 
  61.     {
  62.         if (isEmpty())
  63.            throw new NoSuchElementException("Underflow Exception");
  64.         else 
  65.         {
  66.             len-- ;
  67.             int ele = Queue[front];
  68.             if ( front == rear) 
  69.             {
  70.                 front = -1;
  71.                 rear = -1;
  72.             }
  73.             else
  74.                 front++;                
  75.             return ele;
  76.         }        
  77.     }
  78.     /*  Function to display the status of the queue */
  79.     public void display()
  80.     {
  81.         System.out.print("\nQueue = ");
  82.         if (len == 0)
  83.         {
  84.             System.out.print("Empty\n");
  85.             return ;
  86.         }
  87.         for (int i = front; i <= rear; i++)
  88.             System.out.print(Queue[i]+" ");
  89.         System.out.println();        
  90.     }
  91. }
  92.  
  93. /* Class QueueImplement  */
  94. public class QueueImplement
  95. {
  96.     public static void main(String[] args)
  97.     {
  98.         Scanner scan = new Scanner(System.in);
  99.  
  100.         System.out.println("Array Queue Test\n");
  101.         System.out.println("Enter Size of Integer Queue ");
  102.         int n = scan.nextInt();
  103.         /* creating object of class arrayQueue */
  104.         arrayQueue q = new arrayQueue(n);        
  105.         /* Perform Queue Operations */        
  106.         char ch;
  107.         do{
  108.             System.out.println("\nQueue Operations");
  109.             System.out.println("1. insert");
  110.             System.out.println("2. remove");
  111.             System.out.println("3. peek");
  112.             System.out.println("4. check empty");
  113.             System.out.println("5. check full");
  114.             System.out.println("6. size");
  115.             int choice = scan.nextInt();
  116.             switch (choice)
  117.             {
  118.             case 1 : 
  119.                 System.out.println("Enter integer element to insert");
  120.                 try
  121.                 {
  122.                     q.insert( scan.nextInt() );
  123.                 }
  124.                 catch(Exception e)
  125.                 {
  126.                     System.out.println("Error : " +e.getMessage());
  127.                 }                         
  128.                 break;                         
  129.             case 2 : 
  130.                 try
  131.                 {
  132.                     System.out.println("Removed Element = "+q.remove());
  133.                 }
  134.                 catch(Exception e)
  135.                 {
  136.                     System.out.println("Error : " +e.getMessage());
  137.                 }
  138.                 break;                         
  139.             case 3 : 
  140.                 try
  141.                 {
  142.                     System.out.println("Peek Element = "+q.peek());
  143.                 }
  144.                 catch(Exception e)
  145.                 {
  146.                     System.out.println("Error : "+e.getMessage());
  147.                 }
  148.                 break;                            
  149.             case 4 : 
  150.                 System.out.println("Empty status = "+q.isEmpty());
  151.                 break;                
  152.             case 5 : 
  153.                 System.out.println("Full status = "+q.isFull());
  154.                 break;                          
  155.             case 6 : 
  156.                 System.out.println("Size = "+ q.getSize());
  157.                 break;                         
  158.             default : System.out.println("Wrong Entry \n ");
  159.                 break;
  160.             }
  161.             /* display Queue */
  162.             q.display();            
  163.             System.out.println("\nDo you want to continue (Type y or n) \n");
  164.             ch = scan.next().charAt(0);
  165.  
  166.         } while (ch == 'Y'|| ch == 'y');                                                        
  167.     }    
  168. }

advertisement
Array Queue Test
 
Enter Size of Integer Queue
5
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
4
Empty status = true
 
Queue = Empty
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
24
 
Queue = 24
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
6
 
Queue = 24 6
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
16
 
Queue = 24 6 16
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
19
 
Queue = 24 6 16 19
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
32
 
Queue = 24 6 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
14
Error : Overflow Exception
 
Queue = 24 6 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
5
Full status = true
 
Queue = 24 6 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
3
Peek Element = 24
 
Queue = 24 6 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 24
 
Queue = 6 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 6
 
Queue = 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
6
Size = 3
 
Queue = 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
3
Peek Element = 16
 
Queue = 16 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 16
 
Queue = 19 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 19
 
Queue = 32
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 32
 
Queue = Empty
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Error : Underflow Exception
 
Queue = Empty
 
Do you want to continue (Type y or n)
 
y
 
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
4
Empty status = true
 
Queue = Empty
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.