-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsEmpty.java
More file actions
77 lines (70 loc) · 1.77 KB
/
Copy pathIsEmpty.java
File metadata and controls
77 lines (70 loc) · 1.77 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
public class IsEmpty {
static class Queue{
int maxsize;
int queueArray[];
int front;
int rear;
Queue(int size){
maxsize= size;
queueArray= new int[maxsize];
front= -1;
rear= -1;
}
// insert elements to the queue
void add(int val){
if(rear==maxsize-1){
System.out.println("The queue is full");
return;
}
if(rear==-1){
front= rear= 0;
queueArray[rear]= val;
}
else{
queueArray[++rear]= val;
}
}
// delete element from the queue
void remove(){
if(rear==-1){
System.out.println("The queue is empty");
return;
}
front++;
}
// check if queue is empty or not
boolean isEmpty(){
if(rear==-1){
return true;
}
return false;
}
// display elements of the queue
void display(){
if(rear==-1){
System.out.println("Empty queue");
return;
}
for(int i= front; i<=rear; i++){
System.out.print(queueArray[i] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Queue q= new Queue(5);
q.add(1);
q.add(2);
q.add(3);
q.display();
q.remove();
q.add(4);
q.display(); // 1 2 3 4 5
if(q.isEmpty()){
System.out.println("The queue is empty");
}
else{
System.out.println("The queue is not empty");
}
}
}