-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddQueue.java
More file actions
51 lines (49 loc) · 1.25 KB
/
Copy pathAddQueue.java
File metadata and controls
51 lines (49 loc) · 1.25 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
public class AddQueue {
static class Queue{
int maxsize;
int queueArray[];
int front;
int rear;
Queue(int size){
maxsize= size;
queueArray= new int[size];
front= -1;
rear= -1;
}
void add(int val){
if(rear==maxsize-1){
System.out.println("The queue is full");
return;
}
if(rear==-1){
queueArray[++rear]=val;
front= 0;
queueArray[front]=val;
System.out.println(val + " added in queue");
}
else{
queueArray[++rear]= val;
System.out.println(val + " added in queue");
}
}
void display(){
if(rear==-1){
System.out.print("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(3);
q.add(6);
q.add(2);
q.add(7);
q.add(4);
q.display();
}
}