-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.kunal; | ||
|
||
public class CircularQueue { | ||
protected int[] data; | ||
private static final int DEFAULT_SIZE = 10; | ||
|
||
protected int end = 0; | ||
protected int front = 0; | ||
private int size = 0; | ||
|
||
public CircularQueue(){ | ||
this(DEFAULT_SIZE); | ||
} | ||
|
||
public CircularQueue(int size) { | ||
this.data = new int[size]; | ||
} | ||
|
||
public boolean isFull() { | ||
return size == data.length; // ptr is at last index | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public boolean insert(int item) { | ||
if (isFull()) { | ||
return false; | ||
} | ||
data[end++] = item; | ||
end = end % data.length; | ||
size++; | ||
return true; | ||
} | ||
|
||
public int remove() throws Exception { | ||
if (isEmpty()) { | ||
throw new Exception("Queue is empty"); | ||
} | ||
|
||
int removed = data[front++]; | ||
front = front % data.length; | ||
size--; | ||
return removed; | ||
} | ||
|
||
public int front() throws Exception{ | ||
if (isEmpty()) { | ||
throw new Exception("Queue is empty"); | ||
} | ||
return data[front]; | ||
} | ||
|
||
public void display() { | ||
if (isEmpty()) { | ||
System.out.println("Empty"); | ||
return; | ||
} | ||
int i = front; | ||
do { | ||
System.out.print(data[i] + " -> "); | ||
i++; | ||
i %= data.length; | ||
} while (i != end); | ||
System.out.println("END"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.kunal; | ||
|
||
public class CustomQueue { | ||
private int[] data; | ||
|
||
private static final int DEFAULT_SIZE = 10; | ||
|
||
int end = 0; | ||
|
||
public CustomQueue(){ | ||
this(DEFAULT_SIZE); | ||
} | ||
|
||
public CustomQueue(int size) { | ||
this.data = new int[size]; | ||
} | ||
|
||
public boolean isFull() { | ||
return end == data.length; // ptr is at last index | ||
} | ||
|
||
public boolean isEmpty() { | ||
return end == 0; | ||
} | ||
|
||
public boolean insert(int item) { | ||
if (isFull()) { | ||
return false; | ||
} | ||
data[end++] = item; | ||
return true; | ||
} | ||
|
||
public int remove() throws Exception { | ||
if (isEmpty()) { | ||
throw new Exception("Queue is empty"); | ||
} | ||
|
||
int removed = data[0]; | ||
|
||
// shift the elements to left | ||
for (int i = 1; i < end; i++) { | ||
data[i-1] = data[i]; | ||
} | ||
end--; | ||
return removed; | ||
} | ||
|
||
public int front() throws Exception{ | ||
if (isEmpty()) { | ||
throw new Exception("Queue is empty"); | ||
} | ||
return data[0]; | ||
} | ||
|
||
public void display() { | ||
for (int i = 0; i < end; i++) { | ||
System.out.print(data[i] + " <- "); | ||
} | ||
System.out.println("END"); | ||
} | ||
|
||
} |