Queue Implementation In JAVA
import java.io.*;
class Queue
{
int size = 10;
int que[];
int rear, front;
Queue()
{
que = new int[size];
rear = -1;
front = 0;
}
void insert(int x)
{
if(rear==size)
System.out.println("Queue is full");
else if(rear == -1)
{
que[++rear] = x;
front++;
}
else
que[++rear] = x;
}
void delete()
{
if(front == -1)
System.out.println("Queue is empty");
else if(rear == front)
rear = front = -1;
else
front++;
}
void display()
{
for(int i=front; i<=rear;i++)
System.out.print("\t"+que[i]);
System.out.println("");
}
}
class QueueImpl
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
Queue q= new Queue();
q.insert(6);
q.insert(13);
q.insert(2);
q.insert(19);
q.insert(10);
q.insert(7);
q.delete();
q.delete();
System.out.println("Initial Queue");
q.display();
int ch = 4;
do
{
System.out.println("What operation you wan to perform?:");
System.out.println("1. Insert an element in queue");
System.out.println("2. Delete an element from queue");
System.out.println("3. Display queue");
System.out.println("4. Quit");
try {
ch= Integer.parseInt(in.readLine());}
catch(Exception e) { }
int x = 0;
switch(ch)
{
case 1:
System.out.println("Enter the element");
try {
x= Integer.parseInt(in.readLine());}
catch(Exception e) { }
q.insert(x);
System.out.println("Now the Queue is:");
q.display();
break;
case 2:
q.delete();
System.out.println("Element deleted");
System.out.println("Now the Queue is:");
q.display();
break;
case 3:
q.display();
break;
default:
System.out.println("Wrong choice");
}
} while (ch!=4);
}
}
------------------------------------Output----------------------------------
-----------------------------------------------------------------------------
Initial Queue
19 10 7
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
23
Now the Queue is:
19 10 7 23
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
67
Now the Queue is:
19 10 7 23 67
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
2
Element deleted
Now the Queue is:
10 7 23 67
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
12
Now the Queue is:
10 7 23 67 12
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
4
Wrong choice
class Queue
{
int size = 10;
int que[];
int rear, front;
Queue()
{
que = new int[size];
rear = -1;
front = 0;
}
void insert(int x)
{
if(rear==size)
System.out.println("Queue is full");
else if(rear == -1)
{
que[++rear] = x;
front++;
}
else
que[++rear] = x;
}
void delete()
{
if(front == -1)
System.out.println("Queue is empty");
else if(rear == front)
rear = front = -1;
else
front++;
}
void display()
{
for(int i=front; i<=rear;i++)
System.out.print("\t"+que[i]);
System.out.println("");
}
}
class QueueImpl
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
Queue q= new Queue();
q.insert(6);
q.insert(13);
q.insert(2);
q.insert(19);
q.insert(10);
q.insert(7);
q.delete();
q.delete();
System.out.println("Initial Queue");
q.display();
int ch = 4;
do
{
System.out.println("What operation you wan to perform?:");
System.out.println("1. Insert an element in queue");
System.out.println("2. Delete an element from queue");
System.out.println("3. Display queue");
System.out.println("4. Quit");
try {
ch= Integer.parseInt(in.readLine());}
catch(Exception e) { }
int x = 0;
switch(ch)
{
case 1:
System.out.println("Enter the element");
try {
x= Integer.parseInt(in.readLine());}
catch(Exception e) { }
q.insert(x);
System.out.println("Now the Queue is:");
q.display();
break;
case 2:
q.delete();
System.out.println("Element deleted");
System.out.println("Now the Queue is:");
q.display();
break;
case 3:
q.display();
break;
default:
System.out.println("Wrong choice");
}
} while (ch!=4);
}
}
------------------------------------Output----------------------------------
-----------------------------------------------------------------------------
Initial Queue
19 10 7
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
23
Now the Queue is:
19 10 7 23
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
67
Now the Queue is:
19 10 7 23 67
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
2
Element deleted
Now the Queue is:
10 7 23 67
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
1
Enter the element
12
Now the Queue is:
10 7 23 67 12
What operation you wan to perform?:
1. Insert an element in queue
2. Delete an element from queue
3. Display queue
4. Quit
4
Wrong choice
You Might Also Like :