-
Notifications
You must be signed in to change notification settings - Fork 50
/
queue.java
100 lines (85 loc) · 1.7 KB
/
queue.java
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
// Program to implement Queue in java
import java.io.DataInputStream;
import java.io.IOException;
class queue_operation
{
int queue[] = new int[10];
int REAR = -1;
int FRONT = 0;
DataInputStream dis = new DataInputStream(System.in);
public void insert_operation() throws IOException
{
int a;
if(REAR == 9)
{
System.out.println("QUEUE OVERFLOW");
}
else
{
System.out.print("ENTER RLRMRNT WHICH YOU WANT TO INSERT :");
a = Integer.parseInt(dis.readLine());
queue[REAR+1] = a;
REAR++;
}
}
public void remove_operation()
{
int a;
if(FRONT == (REAR+1))
{
System.out.println("QUEUE UNDERFLOW");
}
else
{
System.out.println("REMOVED ELEMENT FROM QUEUE " +queue[FRONT]);
FRONT++;
}
}
public void display_operation()
{
int i;
if(FRONT == (REAR+1))
{
System.out.println("QUEUE IS EMPTY, NOTHING TO DISPLAY");
}
else
{
System.out.println("QUEUE CONTAINS");
for(i=FRONT;i<=REAR;i++)
{
System.out.print(queue[i]+ "\t");
}
}
}
}
class queue
{
public static void main(String arr[]) throws IOException
{
int ch;
queue_operation q = new queue_operation();
DataInputStream dis = new DataInputStream(System.in);
do
{
System.out.println("\n1 - INSERT");
System.out.println("2 - REMOVE");
System.out.println("3 - DISPLAY");
System.out.println("4 - EXIT");
System.out.println("PROVIDE YOUR CHOICE :");
ch = Integer.parseInt(dis.readLine());
switch(ch)
{
case 1: q.insert_operation();
break;
case 2: q.remove_operation();
break;
case 3: q.display_operation();
break;
case 4: System.exit(0);
break;
default: System.out.println("INVALID CHOICE");
}
}
while(ch!=0);
}
}