-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.java
More file actions
113 lines (113 loc) · 2.6 KB
/
queue.java
File metadata and controls
113 lines (113 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.util.*;
class Queue <t>{
private t arr[];
private int size;
private int front = -1, rear = -1;
public Queue(int size){
this.size = size;
arr = (t[]) new Object[size];
}
public void Enqueue(t num){
if(front == -1 && rear == -1)
front = 0;
if(rear >= size -1){
System.out.println("Queue is FULL");
return;
}
rear += 1;
//System.out.println("Number is " + num);
arr[rear] = num;
}
public t Front(){
if(front > rear)
return null;
else
//System.out.println("Front element is " + arr[front]);
return arr[front];
}
public void Dequeue(){
if(front >= rear){
System.out.println("Queue is Empty");
return;
}
front ++;
}
public static void main(String args[]){
Scanner wats = new Scanner(System.in);
System.out.println("Enter your choice of Queue");
System.out.println("1. Queue for Characters");
System.out.println("2. Queue for Integers");
System.out.println("3. Queue for Floating point numbers");
int choice;
choice = wats.nextInt();
System.out.println("Enter the size of the Queue");
int size = wats.nextInt();
Queue <Integer> qi = new Queue(size);
Queue <Character> qc = new Queue(size);
Queue <Double> qd = new Queue(size);
Boolean c = false , d = false , i = false;
if(choice == 1)
c = true;
else if(choice == 2)
i = true;
else
d = true;
do{
System.out.println("Enter your choice");
System.out.println("1.Enqueue");
System.out.println("2.Dequeue");
System.out.println("3.Peek the Front element");
System.out.println("4.Exit");
choice = wats.nextInt();
switch (choice) {
case 1:
if(c){
System.out.println("Give your input");
char temp = wats.next().charAt(0);
qc.Enqueue(temp);
}
else if(i){
System.out.println("Give your input");
int temp = wats.nextInt();
qi.Enqueue(temp);
}
else{
System.out.println("Give your input");
double temp = wats.nextFloat();
qd.Enqueue(temp);
}
break;
case 2:
if(c)
qc.Dequeue();
else if(d)
qd.Dequeue();
else
qi.Dequeue();
break;
case 3:
if(c){
if(qc.Front() == null)
System.out.println("Queue is Empty");
else
System.out.println(qc.Front());
}
else if(d){
if(qd.Front() == null)
System.out.println("Queue is Empty");
else
System.out.printf("%f\n",qd.Front());
}
else{
if(qi.Front() == null)
System.out.println("Queue is Empty");
else
System.out.println(qi.Front());
}
default:
break;
}
}while(choice != 4);
}
}