-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueBasic.java
More file actions
66 lines (55 loc) · 1.32 KB
/
Copy pathQueueBasic.java
File metadata and controls
66 lines (55 loc) · 1.32 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
package queue;
public class QueueBasic {
int size;
private final int[] items;
private int rear, front;
public QueueBasic(int size) {
this.size = size;
items = new int[size];
front = rear = -1;
}
public boolean isEmpty() {
return front == -1;
}
public boolean isFull() {
return rear == size - 1;
}
public void enqueue(int data) {
if (isFull()) {
System.out.println("Queue is full, cannot add new Element");
return;
}
if (isEmpty()) {
front = 0;
}
rear++;
items[rear] = data;
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is Empty cannot deque");
return -1;
}
int temp = items[front];
if (front == rear) {
front = rear = -1;
return temp;
}
front++;
return temp;
}
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty, cannot peak");
return -1;
}
return items[front];
}
public void printQueue(){
if(isEmpty()) return;
for(int i = front; i <= rear; i++){
System.out.println(items[i]);
}
System.out.println();
}
}