diff --git a/queue.java b/queue.java new file mode 100644 index 0000000..cadf1ee --- /dev/null +++ b/queue.java @@ -0,0 +1,91 @@ +public class QueueImpl { + + private int capacity; + int queueArr[]; + int front = 0; + int rear = -1; + int currentSize = 0; + + public QueueImpl(int queueSize){ + this.capacity = queueSize; + queueArr = new int[this.capacity]; + } + + /** + * this method adds element at the end of the queue. + * @param item + */ + public void enqueue(int item) { + if (isQueueFull()) { + System.out.println("Overflow ! Unable to add element: "+item); + } else { + rear++; + if(rear == capacity-1){ + rear = 0; + } + queueArr[rear] = item; + currentSize++; + System.out.println("Element " + item+ " is pushed to Queue !"); + } + } + + /** + * this method removes an element from the top of the queue + */ + public void dequeue() { + if (isQueueEmpty()) { + System.out.println("Underflow ! Unable to remove element from Queue"); + } else { + front++; + if(front == capacity-1){ + System.out.println("Pop operation done ! removed: "+queueArr[front-1]); + front = 0; + } else { + System.out.println("Pop operation done ! removed: "+queueArr[front-1]); + } + currentSize--; + } + } + + /** + * This method checks whether the queue is full or not + * @return boolean + */ + public boolean isQueueFull(){ + boolean status = false; + if (currentSize == capacity){ + status = true; + } + return status; + } + + /** + * This method checks whether the queue is empty or not + * @return + */ + public boolean isQueueEmpty(){ + boolean status = false; + if (currentSize == 0){ + status = true; + } + return status; + } + + public static void main(String a[]){ + + QueueImpl queue = new QueueImpl(4); + queue.enqueue(4); + queue.dequeue(); + queue.enqueue(56); + queue.enqueue(2); + queue.enqueue(67); + queue.dequeue(); + queue.dequeue(); + queue.enqueue(24); + queue.dequeue(); + queue.enqueue(98); + queue.enqueue(45); + queue.enqueue(23); + queue.enqueue(435); + } +} diff --git a/stack.java b/stack.java new file mode 100644 index 0000000..773be3c --- /dev/null +++ b/stack.java @@ -0,0 +1,99 @@ +public class MyDynamicStack { + + private int stackSize; + private int[] stackArr; + private int top; + + /** + * constructor to create stack with size + * @param size + */ + public MyDynamicStack(int size) { + this.stackSize = size; + this.stackArr = new int[stackSize]; + this.top = -1; + } + + /** + * This method adds new entry to the top + * of the stack + * @param entry + * @throws Exception + */ + public void push(int entry){ + if(this.isStackFull()){ + System.out.println(("Stack is full. Increasing the capacity.")); + this.increaseStackCapacity(); + } + System.out.println("Adding: "+entry); + this.stackArr[++top] = entry; + } + + /** + * This method removes an entry from the + * top of the stack. + * @return + * @throws Exception + */ + public int pop() throws Exception { + if(this.isStackEmpty()){ + throw new Exception("Stack is empty. Can not remove element."); + } + int entry = this.stackArr[top--]; + System.out.println("Removed entry: "+entry); + return entry; + } + + /** + * This method returns top of the stack + * without removing it. + * @return + */ + public long peek() { + return stackArr[top]; + } + + private void increaseStackCapacity(){ + + int[] newStack = new int[this.stackSize*2]; + for(int i=0;i