-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue using array.java
More file actions
60 lines (60 loc) · 1.01 KB
/
Queue using array.java
File metadata and controls
60 lines (60 loc) · 1.01 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
public class Main {
public static void main(String[] args) {
Queue q=new Queue(5);
q.enque(1);
q.enque(78);
q.enque(6);
q.enque(56);
q.display();
q.deque();
System.out.println("\n");
q.display();
q.deque();
System.out.println("\n");
q.display();
System.out.println();
System.out.println(q.peekFirst());
System.out.println();
System.out.println(q.peekLast());
}
}
class Queue {
int rear, front = -1;
int arr[];
int size;
public Queue(int size) {
this.size=size;
this.arr = new int[size];
}
public void enque(int data) {
if (rear == size - 1) {
System.out.println("Queue is overflow, You cant't add element");
return;
} else {
rear +=1;
arr[rear] = data;
}
if (front == -1) {
front += 1;
}
}
public void deque(){
if(front==-1){
System.out.println("under flow state");
return;
}
arr[front]=0;
front=front+1;
}
public int peekFirst(){
return arr[front];
}
public int peekLast(){
return arr[rear];
}
public void display(){
for(int i=front+1;i<=rear;i++){
System.out.print(arr[i]+" ");
}
}
}