-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKWQueueExample.java
More file actions
87 lines (81 loc) · 1.92 KB
/
KWQueueExample.java
File metadata and controls
87 lines (81 loc) · 1.92 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
package com.codinginterview;
public class KWQueueExample {
public static void main(String[] args) {
System.out.println("Queue Example....");
Queue q1 = new Queue(5);
q1.display();
q1.add(10);
q1.add(20);
q1.add(30);
q1.add(40);
q1.add(50);
q1.add(60);
q1.add(70);
q1.display();
q1.deleteItem();
q1.display();
q1.deleteItem();
q1.display();
q1.deleteItem();
q1.display();
q1.deleteItem();
q1.display();
q1.deleteItem();
q1.display();
q1.deleteItem();
q1.display();
}
}
class Queue {
int front = 0, rear = 0;
int rearIndex = -1;
boolean isFull, isEmpty;
int[] queue;
int size;
Queue(int size){
queue = new int[size];
this.size = size;
}
boolean isFull(){
if(queue.length == size){
return true;
}
return false;
}
boolean isEmpty(){
if(queue.length == 0){
return true;
}
return false;
}
void add(int item){
if(rearIndex != size - 1){
queue[++rearIndex] = item;
rear = item;
}else{
System.out.println("Queue is full, Insertion is not allowed for item..." + item);
}
}
int deleteItem(){
int temp = queue[0];
if(rearIndex == -1){
System.out.println("Queue is empty!");
}else{
for (int i = 0; i <= rearIndex - 1; i++){
queue[i] = queue[i + 1];
}
rearIndex --;
}
return temp;
}
void display(){
if(rearIndex == -1){
System.out.println("Queue is Empty!!");
return;
}
System.out.println("Queue Display!");
for(int i = 0; i <= rearIndex; i++){
System.out.print(queue[i] + " ");
}
}
}