Advertisement
earlution

Array implementation of queue (Simple)

Sep 7th, 2021 (edited)
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. // Java program to implement a queue using an array
  2. class Queue {
  3.     private static int front, rear, capacity;
  4.     private static int queue[];
  5.  
  6.     Queue(int c)
  7.     {
  8.         front = rear = 0;
  9.         capacity = c;
  10.         queue = new int[capacity];
  11.     }
  12.  
  13.     // function to insert an element at the rear of the queue
  14.     static void queueEnqueue(int data)
  15.     {
  16.         // check queue is full or not
  17.         if (capacity == rear) {
  18.             System.out.printf("\nQueue is full\n");
  19.             return;
  20.         }
  21.  
  22.         // insert element at the rear
  23.         else {
  24.             queue[rear] = data;
  25.             rear++;
  26.         }
  27.         return;
  28.     }
  29.  
  30.     // function to delete an element from the front of the queue
  31.     static void queueDequeue()
  32.     {
  33.         // if queue is empty
  34.         if (front == rear) {
  35.             System.out.printf("\nQueue is empty\n");
  36.             return;
  37.         }
  38.  
  39.         // shift all the elements from index 2 to rear rightward by one
  40.         else {
  41.             for (int i = 0; i < rear - 1; i++) {
  42.                 queue[i] = queue[i + 1];
  43.             }
  44.  
  45.             // store 0 at rear indicating there's no element
  46.             if (rear < capacity)
  47.                 queue[rear] = 0;
  48.  
  49.             // decrement rear
  50.             rear--;
  51.         }
  52.         return;
  53.     }
  54.  
  55.     // print queue elements
  56.     static void queueDisplay()
  57.     {
  58.         int i;
  59.         if (front == rear) {
  60.             System.out.printf("\nQueue is Empty\n");
  61.             return;
  62.         }
  63.  
  64.         // traverse front to rear and print elements
  65.         for (i = front; i < rear; i++) {
  66.             System.out.printf(" %d <-- ", queue[i]);
  67.         }
  68.         return;
  69.     }
  70.  
  71.     // print front of queue
  72.     static void queueFront()
  73.     {
  74.         if (front == rear) {
  75.             System.out.printf("\nQueue is Empty\n");
  76.             return;
  77.         }
  78.         System.out.printf("\nFront Element is: %d", queue[front]);
  79.         return;
  80.     }
  81. }
  82.  
  83. public class StaticQueueinjava {
  84.  
  85.     // Driver code
  86.     public static void main(String[] args)
  87.     {
  88.         // Create a queue of capacity 4
  89.         Queue q = new Queue(4);
  90.  
  91.         // print Queue elements
  92.         q.queueDisplay();
  93.  
  94.         // inserting elements in the queue
  95.         q.queueEnqueue(20);
  96.         q.queueEnqueue(30);
  97.         q.queueEnqueue(40);
  98.         q.queueEnqueue(50);
  99.  
  100.         // print Queue elements
  101.         q.queueDisplay();
  102.  
  103.         // insert element in the queue
  104.         q.queueEnqueue(60);
  105.  
  106.         // print Queue elements
  107.         q.queueDisplay();
  108.  
  109.         q.queueDequeue();
  110.         q.queueDequeue();
  111.         System.out.printf("\n\nafter two node deletion\n\n");
  112.  
  113.         // print Queue elements
  114.         q.queueDisplay();
  115.  
  116.         // print front of the queue
  117.         q.queueFront();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement