-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathQueueApp.java
75 lines (67 loc) · 1.13 KB
/
QueueApp.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
class Queue {
private
int max,rear,front;
int[] a;
public
Queue(int m) {
max = m;
a = new int[m];
rear = front = -1;
}
void insert(int val) {
if(isFull()) {
System.out.println("overflow");
} else {
if(isEmpty()) {
front = rear = 0;
a[rear] = val;
} else {
rear++;
a[rear] = val;
}
}
}
void display() {
for(int i=front; i<=rear; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
boolean isFull() {
if(rear == max -1) return true;
else return false;
}
boolean isEmpty() {
if(rear == -1) return true;
else return false;
}
int delete() {
if(isEmpty()) {
System.out.println("Underflow");
return -1;
} else {
int temp = a[front];
if(rear==front) {
rear = -1;
front = -1;
} else {
front++;
}
return temp;
}
}
}
class QueueApp {
public static void main(String[] agrs) {
Queue q = new Queue(10);
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.insert(50);
q.insert(60);
q.display();
q.delete();
q.display();
}
}