Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Simple Example Program For Queue in Java Using Array and Class

The document presents a Java program that implements a queue using an array and class. The QueueAction class contains methods to get data input, enqueue items, dequeue items, and display the queue. The QueueProgram class contains the main method that runs a menu loop allowing the user to enqueue, dequeue, display, and exit the program. The program demonstrates enqueueing items until the queue is full, then dequeueing items until the queue is empty.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Simple Example Program For Queue in Java Using Array and Class

The document presents a Java program that implements a queue using an array and class. The QueueAction class contains methods to get data input, enqueue items, dequeue items, and display the queue. The QueueProgram class contains the main method that runs a menu loop allowing the user to enqueue, dequeue, display, and exit the program. The program demonstrates enqueueing items until the queue is full, then dequeueing items until the queue is empty.

Uploaded by

rishabh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Simple Example Program For Queue In Java Using Array and Class

import java.io.*;

class QueueAction {

BufferedReader is = new BufferedReader(new


InputStreamReader(System.in));
int items[];
int i, front = 0, rear = 0, noOfItems, item, count = 0;

void getdata() {
try {
System.out.println("Enter the Limit :");
noOfItems = Integer.parseInt(is.readLine());
items = new int[noOfItems];
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

void enqueue() {
try {
if (count < noOfItems) {
System.out.println("Enter Queue Element :");
item = Integer.parseInt(is.readLine());
items[rear] = item;
rear++;
count++;
} else {
System.out.println("Queue Is Full");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

void dequeue() {
if (count != 0) {
System.out.println("Deleted Item :" + items[front]);
front++;
count--;
} else {
System.out.println("Queue IS Empty");
}
if (rear == noOfItems) {
rear = 0;
}
}

void display() {
int m = 0;
if (count == 0) {
System.out.println("Queue IS Empty");
} else {
for (i = front; m < count; i++, m++) {
System.out.println(" " + items[i]);
}
}
}
}

class QueueProgram {

public static void main(String arg[]) {


DataInputStream get = new DataInputStream(System.in);
int choice;
QueueAction queue = new QueueAction();
queue.getdata();
System.out.println("Queue\n\n");
try {
do {
System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit
\n");
System.out.println("Enter the Choice : ");
choice = Integer.parseInt(get.readLine());
switch (choice) {
case 1:
queue.enqueue();
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
}
} while (choice != 4);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

Sample Output:
Enter the Limit :
4
Queue

1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


1
Enter Queue Element :
45
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


1
Enter Queue Element :
67
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


1
Enter Queue Element :
89
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


1
Enter Queue Element :
567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


1
Queue Is Full
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


3
45
67
89
567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


2
Deleted Item :45
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


2
Deleted Item :67
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


2
Deleted Item :89
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


2
Deleted Item :567
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


2
Queue IS Empty
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the Choice :


4

You might also like