KEMBAR78
Queue | PDF | Queue (Abstract Data Type) | Computer Programming
0% found this document useful (0 votes)
27 views4 pages

Queue

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Queue

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.io.

*;

import java.util.Scanner;

class Queue {

private static int front, rear, capacity,count;

private static int queue[];

Queue(int size) {

front = rear = -1;

capacity = size;

count=0;

queue = new int[capacity];

// insert an element into the queue

static void queueEnqueue(int item) {

// check if the queue is full

if (capacity == count) {

System.out.printf("\nQueue is full\n");

return;

// insert element at the rear

else {

rear++;

if(count==0)

front = 0;

queue[rear] = item;

count++;

System.out.println("Count is:"+count);
}

return;

//remove an element from the queue

static void queueDequeue() {

// check if queue is empty

if (front == -1) {

System.out.printf("\nQueue is empty\n");

return;

else

int dnum = queue[front];

queue[front]=0;

front++;

count--;

System.out.println(dnum);

System.out.println("Count is:"+count);

return;

// print queue elements

static void queueDisplay()

int i;

if (front == -1) {

System.out.printf("Queue is Empty\n");
return;

// traverse front to rear and print elements

for (i = 0; i <= rear; i++) {

System.out.printf(" %d , ", queue[i]);

System.out.println("Count is:"+count);

return;

// print front of queue

static void queueFront()

if (front == rear) {

System.out.printf("Queue is Empty\n");

return;

System.out.printf("\nFront Element of the queue: %d", queue[front]);

return;

public class QueueArraySimple {

public static void main(String[] args) throws IOException {

// Create a queue of capacity 4

Queue q = new Queue(4);

int choice;

Scanner scanner = new Scanner (System.in);

BufferedReader bfn = new BufferedReader(

new InputStreamReader(System.in));
do

System.out.println("Menu:");

System.out.println("1. Enqueue");

System.out.println("2. Dequeue");

System.out.println("3. Queue Display");

System.out.println("4. Quit");

System.out.print("Enter your choice: ");

choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.println("Enter Queue Element:");

int it = Integer.parseInt(bfn.readLine());

// inserting elements in the queue

q.queueEnqueue(it);

break;

case 2:

System.out.println("Dequeued Elements are:");

q.queueDequeue();

break;

case 3:

System.out.println("Queue Elements are:");

q.queueDisplay();

break;

default:

System.out.println("Invalid choice. Please choose a valid option.");

}while (choice != 4);

You might also like