-
Notifications
You must be signed in to change notification settings - Fork 3
/
Queue_array.java
85 lines (72 loc) · 1.72 KB
/
Queue_array.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
package DATA_STRUCTURE;
import java.util.Scanner;
public class Queue_array {
Scanner s = new Scanner(System.in);
static final int max = 5; //FIRST IN FIRST OUT FIFO
int rear = -1;
int front = -1;
int[] Queue = new int[max];
boolean isFull(){
if(rear == max-1){
return true;
}
else{
return false;
}
}
boolean isEmpty(){
if(rear == -1 || front ==-1){
return true;
}
else {
return false;
}
}
void insert(){
if(isFull()){
System.out.println("Queue is Full..!!");
return;
}
else{
System.out.println("Enter element");
int e = s.nextInt();
rear++;
Queue[rear] = e;
if(rear == 0){
front = 0;
}
}
}
void Delete(){
if(isEmpty()){
System.out.println("Queue is empty");
}
else if(front == rear ){
front = rear = -1;
}
else{
int element = Queue[front];
front++;
System.out.println(element+" Deleted from queue");
}
}
void display(){
for (int i = front; i <= rear ; i++) {
System.out.print(Queue[i]+" ");
}
}
public static void main(String[] args) {
Queue_array q = new Queue_array();
q.insert();
q.insert();
q.insert();
q.insert();
q.insert();
q.Delete();
q.Delete();
System.out.println("\n\n");
System.out.print("Front --> ");
q.display();
System.out.print(" <--Rear");
}
}