Skip to content

Commit f74862a

Browse files
stacks and queues
1 parent e18b3e9 commit f74862a

19 files changed

+513
-0
lines changed

Diff for: lectures/19-stacks-n-queues/Stacks and Queues.pdf

843 KB
Binary file not shown.

Diff for: lectures/19-stacks-n-queues/code/.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/description.html

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/misc.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/project-template.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lectures/19-stacks-n-queues/code/code.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.kunal;
2+
3+
public class CircularQueue {
4+
protected int[] data;
5+
private static final int DEFAULT_SIZE = 10;
6+
7+
protected int end = 0;
8+
protected int front = 0;
9+
private int size = 0;
10+
11+
public CircularQueue(){
12+
this(DEFAULT_SIZE);
13+
}
14+
15+
public CircularQueue(int size) {
16+
this.data = new int[size];
17+
}
18+
19+
public boolean isFull() {
20+
return size == data.length; // ptr is at last index
21+
}
22+
23+
public boolean isEmpty() {
24+
return size == 0;
25+
}
26+
27+
public boolean insert(int item) {
28+
if (isFull()) {
29+
return false;
30+
}
31+
data[end++] = item;
32+
end = end % data.length;
33+
size++;
34+
return true;
35+
}
36+
37+
public int remove() throws Exception {
38+
if (isEmpty()) {
39+
throw new Exception("Queue is empty");
40+
}
41+
42+
int removed = data[front++];
43+
front = front % data.length;
44+
size--;
45+
return removed;
46+
}
47+
48+
public int front() throws Exception{
49+
if (isEmpty()) {
50+
throw new Exception("Queue is empty");
51+
}
52+
return data[front];
53+
}
54+
55+
public void display() {
56+
if (isEmpty()) {
57+
System.out.println("Empty");
58+
return;
59+
}
60+
int i = front;
61+
do {
62+
System.out.print(data[i] + " -> ");
63+
i++;
64+
i %= data.length;
65+
} while (i != end);
66+
System.out.println("END");
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.kunal;
2+
3+
public class CustomQueue {
4+
private int[] data;
5+
6+
private static final int DEFAULT_SIZE = 10;
7+
8+
int end = 0;
9+
10+
public CustomQueue(){
11+
this(DEFAULT_SIZE);
12+
}
13+
14+
public CustomQueue(int size) {
15+
this.data = new int[size];
16+
}
17+
18+
public boolean isFull() {
19+
return end == data.length; // ptr is at last index
20+
}
21+
22+
public boolean isEmpty() {
23+
return end == 0;
24+
}
25+
26+
public boolean insert(int item) {
27+
if (isFull()) {
28+
return false;
29+
}
30+
data[end++] = item;
31+
return true;
32+
}
33+
34+
public int remove() throws Exception {
35+
if (isEmpty()) {
36+
throw new Exception("Queue is empty");
37+
}
38+
39+
int removed = data[0];
40+
41+
// shift the elements to left
42+
for (int i = 1; i < end; i++) {
43+
data[i-1] = data[i];
44+
}
45+
end--;
46+
return removed;
47+
}
48+
49+
public int front() throws Exception{
50+
if (isEmpty()) {
51+
throw new Exception("Queue is empty");
52+
}
53+
return data[0];
54+
}
55+
56+
public void display() {
57+
for (int i = 0; i < end; i++) {
58+
System.out.print(data[i] + " <- ");
59+
}
60+
System.out.println("END");
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.kunal;
2+
3+
public class CustomStack {
4+
protected int[] data;
5+
private static final int DEFAULT_SIZE = 10;
6+
7+
int ptr = -1;
8+
9+
public CustomStack(){
10+
this(DEFAULT_SIZE);
11+
}
12+
13+
public CustomStack(int size) {
14+
this.data = new int[size];
15+
}
16+
17+
public boolean push(int item) {
18+
if (isFull()) {
19+
System.out.println("Stack is full!!");
20+
return false;
21+
}
22+
ptr++;
23+
data[ptr] = item;
24+
return true;
25+
}
26+
27+
public int pop() throws StackException {
28+
if (isEmpty()) {
29+
throw new StackException("Cannot pop from an empty stack!!");
30+
}
31+
// int removed = data[ptr];
32+
// ptr--;
33+
// return removed;
34+
return data[ptr--];
35+
}
36+
37+
public int peek() throws StackException {
38+
if (isEmpty()) {
39+
throw new StackException("Cannot peek from an empty stack!!");
40+
}
41+
return data[ptr];
42+
}
43+
44+
public boolean isFull() {
45+
return ptr == data.length - 1; // ptr is at last index
46+
}
47+
48+
public boolean isEmpty() {
49+
return ptr == -1;
50+
}
51+
}

0 commit comments

Comments
 (0)