0% found this document useful (0 votes)
56 views8 pages

Both in Different Program

The document describes a program for implementing a queue in Java using an array and class. It defines a QueueAction class with methods to get data on the queue size, enqueue elements, dequeue elements, and display the queue. A main method in the QueueProgram class runs a menu loop allowing the user to enqueue, dequeue, display, and exit while demonstrating the queue functions.

Uploaded by

Abhishek Kumar
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)
56 views8 pages

Both in Different Program

The document describes a program for implementing a queue in Java using an array and class. It defines a QueueAction class with methods to get data on the queue size, enqueue elements, dequeue elements, and display the queue. A main method in the QueueProgram class runs a menu loop allowing the user to enqueue, dequeue, display, and exit while demonstrating the queue functions.

Uploaded by

Abhishek Kumar
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/ 8

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
Linked list
mport java.io.*;
class Que_Node
{
int info;
Que_Node nptr;
static Que_Node front;
static Que_Node rear;
public static void main()throws IOException
{
InputStreamReader x=new InputStreamReader(System.in);
BufferedReader y=new BufferedReader(x);
int e,choice,flag;
front=null;
rear=null;
do
{
System.out.println( \f 1. Add \n 2. Remove \n 3. Display \n 4. Exit);
choice = Integer.parseInt(y.readLine());
switch(choice)
{
case 1: System.out.println(Element);
e=Integer.parseInt(y.readLine());
Que_Node current = new Que_Node();
current=current.createQue_Node(e);
current.addQue_Node(current);
break;
case 2:
front.removeQue_Node();
break;
case 3:
front.displayQue_Nodes();
break;
case 4: System.out.println(Quitting);
System.exit(0);
default:
System.out.println(Wrong choice);
}

System.out.println(Enter: 1 to continue, 0 to terminate);


flag = Integer.parseInt(y.readLine());
} while(flag==1);

public Que_Node createQue_Node(int e)


{
Que_Node temp = new Que_Node();
temp.info = e;
temp.nptr = null;
return temp;
}
public void addQue_Node(Que_Node nd)
{
if(front==null && rear==null)
{
front=nd;
rear=nd;
}

else
{
rear.nptr=nd;
rear=nd;
}
}

public void displayQue_Nodes()


{
if(front==null)
System.out.println(Queue Empty);
else
{
Que_Node copy = front;
System.out.println(The elements of the queue are :);
System.out.print(FRONT : );
while(copy!=null)
{
System.out.print(copy.info + );
copy=copy.nptr;
}
System.out.println( : REAR);
}
}
public void removeQue_Node()
{
if(front==null)
System.out.println(Queue underflow);
else
{
System.out.println(Element + front.info + has been removed);
front=front.nptr;
}
}

You might also like