-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_queue.java
More file actions
96 lines (77 loc) · 2.21 KB
/
Circular_queue.java
File metadata and controls
96 lines (77 loc) · 2.21 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
90
91
92
93
94
95
96
import java.util.*;
public class Circular_queue {
public static class queue {
static int rear;
static int front;
static int size;
static int arr[];
static int n;
queue(int n) {
arr = new int[n];
front = -1;
rear = -1;
size = n;
}
//if queue is empty
public static boolean Isempty() {
return rear == -1 && front == -1;
}
public static boolean Isfull() {
return (rear + 1) % size == front;
}
//add in queue
public static void add(int data) {
if (Isfull()) {
System.out.println("queue is full");
} else {
if(front==-1){
front=0;
}
rear = (rear + 1) % size;
arr[rear] = data;
}
}
//remove from the queue
public static int remove() {
if (Isempty()) {
System.out.println("our list is empty");
}
int result = arr[front];
//if we remove the last element of the list
if (front == rear) {
front = rear = -1;
} else {
front= (front + 1) % size;
}
return result;
}
//peek
public static int peek() {
if (Isempty()) {
System.out.println("queue empty");
return -1;
}
int top = arr[front];
return top;
}
//print queue
public static void print() {
for (int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
public static void main(String args[]) {
queue q = new queue(5);
q.add(45);
q.add(56);
q.add(77);
int b=q.remove();
System.out.println(b+"remove");
q.add(6);
q.add(8);
int c=q.remove();
System.out.println(c+"remove");
print();
}
}
}