Skip to content

Commit 1e02652

Browse files
committed
added Fixed array simple queue in java
1 parent a59f446 commit 1e02652

File tree

3 files changed

+228
-2
lines changed

3 files changed

+228
-2
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
abstract class FixedArraySimpleQueue
2+
{
3+
private static class FixedArrayQueue
4+
{
5+
private int front;
6+
private int rear;
7+
private int[] que;
8+
9+
/**
10+
* default constructor
11+
*/
12+
public FixedArrayQueue()
13+
{
14+
this(5);
15+
}
16+
/**
17+
* default constructor
18+
* @param size
19+
*/
20+
public FixedArrayQueue(int size)
21+
{
22+
que = new int[size];
23+
front=-1;
24+
rear=-1;
25+
}
26+
27+
/**
28+
* This method will add an element to the queue
29+
* @param val for value to enqueue
30+
* @throws Exception
31+
* @return void
32+
*/
33+
public void enqueue(int val) throws Exception
34+
{
35+
if(front==-1 && rear==-1)
36+
{
37+
front++;
38+
}
39+
rear++;
40+
if(rear==que.length)
41+
{
42+
throw new Exception("FullQueueException");
43+
}
44+
que[rear]=val;
45+
}
46+
47+
/**
48+
* This method will remove an element from the queue
49+
* @return int dequeued value
50+
* @throws Exception
51+
*/
52+
public int dequeue() throws Exception
53+
{
54+
if(front==-1 || rear==-1)
55+
{
56+
throw new Exception("EmptyQueueException");
57+
}
58+
int temp = que[front];
59+
60+
//shifting left
61+
int i=0;
62+
for(i=0;i<que.length-1;i++)
63+
{
64+
que[i]=que[i+1];
65+
}
66+
que[i]=0;
67+
rear--;
68+
69+
return temp;
70+
}
71+
72+
/**
73+
* It will return the front value from the queue
74+
* @return int front value
75+
*/
76+
public int front()
77+
{
78+
if(isEmpty())
79+
{
80+
return -1;
81+
}
82+
return que[front];
83+
}
84+
85+
/**
86+
* It will return the size of the Queue
87+
* @return int size of the Queue
88+
*/
89+
public int size()
90+
{
91+
return rear-front+1;
92+
}
93+
94+
/**
95+
* Tells whether the Queue is empty or not
96+
* @return boolean
97+
*/
98+
public boolean isEmpty()
99+
{
100+
if(front==-1 || rear==-1)
101+
{
102+
return true;
103+
}
104+
return false;
105+
}
106+
107+
/**
108+
* It will print the queue array
109+
* @param void
110+
* @return void
111+
**/
112+
public void printCheck()
113+
{
114+
for(int i=0;i<que.length;i++)
115+
{
116+
System.out.print(que[i]+" ");
117+
}
118+
System.out.println();
119+
}
120+
}
121+
122+
public static void main(String[] args)
123+
{
124+
try
125+
{
126+
FixedArrayQueue Q = new FixedArrayQueue();
127+
System.out.println(Q.isEmpty());
128+
Q.enqueue(1);
129+
Q.enqueue(2);
130+
Q.enqueue(3);
131+
Q.enqueue(4);
132+
Q.enqueue(5);
133+
Q.dequeue();
134+
Q.dequeue();
135+
Q.dequeue();
136+
Q.dequeue();
137+
Q.dequeue();
138+
Q.enqueue(1);
139+
Q.dequeue();
140+
Q.enqueue(2);
141+
Q.enqueue(3);
142+
Q.enqueue(4);
143+
Q.enqueue(5);
144+
Q.enqueue(6);
145+
System.out.println(Q.isEmpty());
146+
System.out.println(Q.front());
147+
System.out.println(Q.size());
148+
Q.printCheck();
149+
}
150+
catch(Exception e)
151+
{
152+
System.out.println(e);
153+
}
154+
}
155+
}

Java/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
#### QUEUES
6666

6767
* SIMPLE QUEUE
68-
* FIXED ARRAY SIMPLE QUEUE
68+
* [FIXED ARRAY SIMPLE QUEUE](Data-Structures/QUEUES/SIMPLE-QUEUE/FixedArraySimpleQueue.java)
6969
* DYNAMIC ARRAY SIMPLE QUEUE
7070
* LINKED SIMPLE QUEUE
7171
* CIRCULAR QUEUE
@@ -75,6 +75,7 @@
7575
* FIXED ARRAY PRIORITY QUEUE
7676
* LINKED PRIORITY QUEUE
7777
* HEAPED PRIORITY QUEUE
78+
* MISC QUEUE
7879

7980
#### HASHTABLE
8081

datastructures.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Indexer for Data Structures Lover
8080
* blog
8181
* docs
8282
* implementation
83-
* [C](C/Data-Structures/ARRAYS/jaggedarray.c)
83+
* [C](C/Data-Structures/ARRAYS/MISC/jaggedarray.c)
8484
* complexity
8585

8686
## :octocat: STRING
@@ -263,6 +263,76 @@ Indexer for Data Structures Lover
263263

264264
## :octocat: QUEUES
265265

266+
### SIMPLE QUEUE
267+
268+
#### FIXED ARRAY SIMPLE QUEUE
269+
270+
* blog
271+
* docs
272+
* implementation
273+
* [JAVA](Java/Data-Structures/QUEUES/SIMPLE-QUEUE/FixedArraySimpleQueue.java)
274+
* complexity
275+
276+
#### DYNAMIC ARRAY SIMPLE QUEUE
277+
278+
* blog
279+
* docs
280+
* implementation
281+
* complexity
282+
283+
#### LINKED SIMPLE QUEUE
284+
285+
* blog
286+
* docs
287+
* implementation
288+
* complexity
289+
290+
### CIRCULAR QUEUE
291+
292+
#### FIXED ARRAY CIRCULAR QUEUE
293+
294+
* blog
295+
* docs
296+
* implementation
297+
* complexity
298+
299+
#### DYNAMIC ARRAY CIRCULAR QUEUE
300+
301+
* blog
302+
* docs
303+
* implementation
304+
* complexity
305+
306+
#### LINKED CIRCULAR QUEUE
307+
308+
* blog
309+
* docs
310+
* implementation
311+
* complexity
312+
313+
### PRIORITY QUEUE
314+
315+
#### FIXED ARRAY PRIORITY QUEUE
316+
317+
* blog
318+
* docs
319+
* implementation
320+
* complexity
321+
322+
#### LINKED PRIORITY QUEUE
323+
324+
* blog
325+
* docs
326+
* implementation
327+
* complexity
328+
329+
#### HEAPED PRIORITY QUEUE
330+
331+
* blog
332+
* docs
333+
* implementation
334+
* complexity
335+
266336
## :octocat: TREES
267337

268338
### BINARY TREES

0 commit comments

Comments
 (0)