-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation of Queue Using Array .java
More file actions
89 lines (66 loc) · 1.82 KB
/
Copy pathImplementation of Queue Using Array .java
File metadata and controls
89 lines (66 loc) · 1.82 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
///// Implementation of Queue Using Array In java /////
public class Queue{
int capacity;
int front, rear, size;
int arr[];
Queue(int capacity){
this.capacity = capacity;
this.size = 0;
this.front = 0;
this.rear = this.capacity - 1;
arr = new int[this.capacity];
}
public boolean isEmpty(){
if (this.size == 0){
return true;
}
return false;
}
public boolean isFull(){
if (this.size == this.capacity){
return true;
}
return false;
}
public void enqueue(int d){
if(isFull()){
System.out.println("Queue is Full");
return;
}
this.rear= (this.rear+1)%this.capacity;
this.arr[this.rear] = d;
this.size++;
System.out.println("Enqueue :"+d);
}
public int dequeue(){
if (isEmpty()) {
System.out.println("Queue is Empty!!");
return 0;
}
int temp = this.arr[this.front];
this.front = (this.front+1)%this.capacity;
this.size--;
System.out.println("Dequeue :" + temp);
return temp;
}
public void front(){
System.out.println("Front is here on :"+this.arr[this.front]);
}
public void rear()
{
System.out.println("rear is here on :"+this.arr[this.rear]);
}
///// Main Function (Driver Fucntion) /////
public static void main(String []args){
Queue q = new Queue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.dequeue();
q.dequeue();
q.front();
q.rear();
}
}