diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/clients/src/main/java/com/hithomelabs/clients/ArrayQueueClient.java b/clients/src/main/java/com/hithomelabs/clients/ArrayQueueClient.java new file mode 100644 index 0000000..dca3c9f --- /dev/null +++ b/clients/src/main/java/com/hithomelabs/clients/ArrayQueueClient.java @@ -0,0 +1,22 @@ +package com.hithomelabs.clients; + +import com.hithomelabs.princeton1.module4.ArrayQueue; +import com.hithomelabs.princeton1.module4.ArrayStack; +import com.hithomelabs.princeton1.module4.Queue; +import com.hithomelabs.princeton1.module4.Stack; + +public class ArrayQueueClient { + public static void main(String[] args) { + Queue queue = new ArrayQueue(); + for(int i = 0; i < 21; i++){ + queue.enqueue(i); + } + for(int i = 0; i < 13; i++){ + System.out.println(queue.dequeue()); + } + + // Using object s, as stack is generic but every java Object has implementation of toString() + for (Object s : queue) + System.out.println(s); + } + } diff --git a/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayQueue.java b/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayQueue.java new file mode 100644 index 0000000..4e8988e --- /dev/null +++ b/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayQueue.java @@ -0,0 +1,103 @@ +package com.hithomelabs.princeton1.module4; + +import javax.annotation.Nonnull; +import java.util.Iterator; + +public class ArrayQueue extends Queue{ + + // insertion from tail, removal from head + public static final int DEFAULT_CAPACITY = 10; + private int capacity; + private int tail; + private int head; + private int size; + private E[] arr; + + public ArrayQueue(){ + this(DEFAULT_CAPACITY); + } + + public ArrayQueue(int capacity){ + this.capacity = capacity; + arr = (E[]) new Object[this.capacity]; + size = 0; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public E dequeue() { + if(isEmpty()) + return null; + else{ + E element = arr[head]; + // Garbage collection + arr[head] = null; + head = (head+1)%capacity; + size = size - 1; + if(capacity >= 40 && size < capacity/4){ + capacity = capacity/2; + resize(capacity, capacity*2); + } + return element; + } + } + + @Override + public void enqueue(E element) { + // We plan capacity expansion if needed + if (size == capacity){ + capacity = capacity * 2; + resize(capacity, capacity/2); + } + arr[tail] = element; + tail = (tail + 1) % capacity; + ++size; + } + + // When resize takes place always the original array is full, so copy the complete array as is + private void resize(int capacity, int oldCapacity) { + E[] resizedArr = (E[]) new Object[capacity]; + for(int i = 0; i < size; i++) { + resizedArr[i] = arr[head]; + // halving because capacity has now doubled + arr[head] = null; + head = (head + 1) % oldCapacity; + } + arr = resizedArr; + // When resizing takes place, we bring the head to 0 and the tail to size + // tail is where new inserts will be made and head will be where elements will be removed + tail = size; + head = 0; + + } + + @Override + public int size() { + return size; + } + + @Override + @Nonnull + public Iterator iterator() { + return new Iterator() { + int counter = size; + int pointer = head; + @Override + public boolean hasNext() { + return counter != 0; + } + + @Override + public E next() { + E element = arr[pointer]; + pointer = (pointer + 1)% capacity; + --counter; + return element; + } + }; + } +} diff --git a/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java b/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java index 42e0623..0ae2eb6 100644 --- a/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java +++ b/module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java @@ -1,4 +1,5 @@ package com.hithomelabs.princeton1.module4; +import javax.annotation.Nonnull; import java.util.Iterator; // Concrete implementation of stack using arrays @@ -14,7 +15,7 @@ public class ArrayStack extends Stack { public ArrayStack(int capacity){ this.capacity = capacity; - this.arr = (E[]) new Object[this.capacity]; + arr = (E[]) new Object[this.capacity]; } // Constructor chaining, default constructor will call parametrised constructor with default initial capacity 10 @@ -24,20 +25,20 @@ public class ArrayStack extends Stack { @Override public boolean isEmpty() { - return this.size == 0; + return size == 0; } private void changeCapacity(int newCapacity){ E[] resizedArr = (E[]) new Object[newCapacity]; - for (int i = 0; i < this.size; i++) - resizedArr[i] = this.arr[i]; - this.arr = resizedArr; + for (int i = 0; i < size; i++) + resizedArr[i] = arr[i]; + arr = resizedArr; } private void incrementSize(){ - if (this.size == this.capacity){ - this.capacity = 2 * this.capacity; - changeCapacity(this.capacity); + if (size == capacity){ + capacity = 2 * capacity; + changeCapacity(capacity); } } @@ -46,8 +47,8 @@ public class ArrayStack extends Stack { @Override public void push(E element) { // Lazy approach, we assume size to always be lesser than capacity - this.incrementSize(); - this.arr[this.size++] = element; + incrementSize(); + arr[size++] = element; } @Override @@ -55,26 +56,27 @@ public class ArrayStack extends Stack { if (isEmpty()) return null; else{ - E e = this.arr[--this.size]; - this.arr[this.size] = null; - this.checkResize(); + E e = arr[--size]; + arr[size] = null; + checkResize(); return e; } } private void checkResize() { - if (this.size < this.capacity / 4 && this.capacity > 10){ - this.capacity = this.capacity / 2; - changeCapacity(this.capacity); + if (size < capacity / 4 && capacity >= 40){ + capacity = capacity / 2; + changeCapacity(capacity); } } @Override public int size() { - return this.size; + return size; } @Override + @Nonnull public Iterator iterator() { return new Iterator() { diff --git a/module4/src/main/java/com/hithomelabs/princeton1/module4/Queue.java b/module4/src/main/java/com/hithomelabs/princeton1/module4/Queue.java new file mode 100644 index 0000000..6c4999d --- /dev/null +++ b/module4/src/main/java/com/hithomelabs/princeton1/module4/Queue.java @@ -0,0 +1,10 @@ +package com.hithomelabs.princeton1.module4; + +public abstract class Queue implements Iterable { + + public abstract boolean isEmpty(); + public abstract E dequeue(); + public abstract void enqueue(E element); + public abstract int size(); +} +