Skip to content

Commit 67df979

Browse files
Add files via upload
1 parent 24944e2 commit 67df979

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

QueueArray.java

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import java.io.*;
2+
import java.util.*;
3+
class QueueArray{
4+
5+
Scanner sc = new Scanner(System.in);
6+
int queue[];
7+
int front=0,rear=0,size=0,n=0;
8+
9+
public static void main(String args[])throws IOException{
10+
11+
QueueArray Queue = new QueueArray();
12+
Queue.Driver();
13+
}
14+
15+
public void Driver(){
16+
17+
String in="";
18+
int elem=0;
19+
20+
System.out.print("Enter the size of the Queue: ");
21+
if(ZeroQueue()) return;
22+
23+
CreateQueue(n);
24+
boolean INTR=true;
25+
do{
26+
int choice = QueueMenu();
27+
switch (choice){
28+
case (1):
29+
{
30+
if(!QueueIsFull()){
31+
System.out.print("ENQUEUE: ");
32+
in = SafeInput();
33+
if(in.equals("invalid")){
34+
sc.nextLine();
35+
break;
36+
}
37+
elem = Integer.parseInt(in);
38+
Enqueue(elem);
39+
System.out.println(elem+" has been added to the queue");
40+
}
41+
else
42+
System.out.println("Queue is full");
43+
break;
44+
}
45+
case (2):
46+
{
47+
if(!QueueIsEmpty()){
48+
elem = Dequeue();
49+
System.out.println("Dequeued element: "+elem);
50+
}
51+
else
52+
System.out.println("Queue is empty");
53+
break;
54+
}
55+
case (3):
56+
{
57+
if(!QueueIsEmpty())
58+
System.out.println("Front element is: "+queue[front]);
59+
else
60+
System.out.println("Queue is empty");
61+
break;
62+
}
63+
case (4):
64+
{
65+
System.out.println("Size of Queue is "+ size);
66+
break;
67+
}
68+
case (5):
69+
{
70+
if(!QueueIsEmpty()){
71+
System.out.print("QUEUE: ( ");
72+
Display();
73+
System.out.println(")");
74+
}
75+
else
76+
System.out.println("Queue is empty");
77+
break;
78+
}
79+
case (0):
80+
INTR=false;
81+
82+
}
83+
if(choice>5||choice<0)
84+
System.out.println("Not a valid menu option");
85+
86+
}while(INTR);
87+
}
88+
89+
public boolean ZeroQueue(){
90+
String in="";
91+
in = SafeInput();
92+
if(in.equals("invalid"))
93+
return true;
94+
95+
n = Integer.parseInt(in);
96+
if(n<=0){
97+
System.out.println("No worthwhile queue size. Exiting...");
98+
return true;
99+
}
100+
return false;
101+
}
102+
103+
public String SafeInput(){
104+
int a;
105+
try{
106+
a=sc.nextInt();
107+
return Integer.toString(a);
108+
}
109+
catch(InputMismatchException e)
110+
{
111+
System.out.println("Result not an integer");
112+
return "invalid";
113+
}
114+
}
115+
116+
public void CreateQueue(int n){
117+
queue = new int[n];
118+
}
119+
120+
public int QueueMenu(){
121+
System.out.println("QUEUE OPTIONS: ");
122+
System.out.println("1.ENQUEUE 2.DEQUEUE 3.FRONT 4.SIZE 5.DISPLAY 0.EXIT");
123+
String in = SafeInput();
124+
if(in=="invalid"){
125+
sc.nextLine();
126+
return -1;
127+
}
128+
else return Integer.parseInt(in);
129+
}
130+
131+
public void Enqueue(int elem){
132+
queue[rear]=elem;
133+
rear = (++rear)%n;
134+
++size;
135+
}
136+
public int Dequeue(){
137+
int elem = queue[front];
138+
front = (++front)%n;
139+
--size;
140+
return elem;
141+
}
142+
public void Display(){
143+
for(int i=front,j=0;j<size;j++){
144+
System.out.print(queue[i] +" ");
145+
i=(i+1)%n;
146+
}
147+
}
148+
public boolean QueueIsFull(){
149+
boolean value;
150+
value = ((size==n) ? true : false);
151+
return value;
152+
}
153+
public boolean QueueIsEmpty(){
154+
boolean value;
155+
value = (size==0) ? true : false;
156+
return value;
157+
}
158+
159+
}

0 commit comments

Comments
 (0)