forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueue.java
More file actions
158 lines (133 loc) · 4.48 KB
/
CircularQueue.java
File metadata and controls
158 lines (133 loc) · 4.48 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.*;
import java.lang.*;
/*
code By Varul Srivastava
First Year undergraduate at IIIT Hyderabad
*/
public class CircularQueue {
int size;
int start = -1, end=0;
int queue_arr[];
int c=-1729;
public CircularQueue(int length){
queue_arr=new int[length];
size=length;
length--;
while(length>=0){
queue_arr[length--]=-1729; //this number cannot be the value of any index. Make it settable.
}
start=-1;
end=-1;
}
//overloaded constructor that takes the prohibited value of the queue
public CircularQueue(int length,int c){
queue_arr=new int[length--];
this.c=c;
size=length;
while(length>=0){
queue_arr[length]=c; //this number cannot be the value of any index. Make it settable.
}
start=-1;
end=-1;
}
//enter an element in the end of queue
public void pushEnd(int value){
try{
if( end==start && end !=-1 && queue_arr[end]!=c )
{
System.out.println("Out of Bounds");
}
else if(end==start && start==-1){
end=start=0;
queue_arr[end]=value;
end++;
}
else
{
if(end==size){
end=0;
}
queue_arr[end]=value;
end++;
if(end==size){
end =0;//when elements have not been popped out from queue
}
}
}
catch(Exception e){
System.err.println(e);
}
}
//pull out an element from the queue
public int popFront(){
if(start==size){
start=0;
}
if(start==-1 || (start==end && queue_arr[start]==c)){
System.err.println("IndexOutOfBounds");
// throw new emptyStackException;
return -1; //Some developer desired constant (not necessary)
}
int ret= queue_arr[start];
queue_arr[start++]=c;
return ret;
}
//checks if the stack is empty
public boolean isEmpty(){
return start==end && (start==-1 || queue_arr[start]==c);
}
//checks if stack is full
public boolean isFull(){
return start==end && queue_arr[end]==c;
}
//main method to test the code
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Queue");
int length=sc.nextInt();
CircularQueue ob=new CircularQueue(length);
char ch='y';//default set to yes, can be changed to No, doesnot matter
do{
System.out.println("1-Add an element to the Queue at the end\n2-Check wether the Queue is empty\n3-Return/Delete an Element from the Front\n4-Check if Queue is Full");
System.out.println("Enter the option");
int opt=sc.nextInt();//option choosen by the user
switch(opt){
case 1:
System.out.println("Enter the element to be inserted to Queue");
int elem=sc.nextInt();
ob.pushEnd(elem);
break;
case 2:
if(ob.isEmpty()){
System.out.println("Queue is Empty");
}
else{
System.out.println("Queue is Not Empty");
}
break;
case 3:
System.out.println("Enter the deleted/returned is :"+ob.popFront());
break;
case 4:
if(ob.isFull()){
System.out.println("Queue is Full");
}
else{
System.out.println("Queue is not Full");
}
default:
System.out.println("Wrong input");
}
System.out.println("Do you want to continue for more inputs?(y/n)");//Any random character will be considered as No
ch=sc.next().charAt(0);
ch=Character.toLowerCase(ch);
}while(ch=='y');
/*created Date Monday October 7 2019 1800 hrs
last modified Saturday October 12 2019 1913 hrs
Further Edits:Change options according to the methods
Check -1 or 0 cases of queue
Run and debug the codes
Add better comments
*/
}
}