From 9ad62e19c95c4576d150bcc06718d059e9e6ac9d Mon Sep 17 00:00:00 2001 From: Kunwar Date: Sun, 25 Jul 2021 22:53:59 +0530 Subject: [PATCH] Data Structures and Algorithm --- .../queue/MyQueue.java | 89 +++++++++++++++++++ .../stack/MyStack.java | 84 +++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 Java/data-structure-with-java/queue/MyQueue.java create mode 100644 Java/data-structure-with-java/stack/MyStack.java diff --git a/Java/data-structure-with-java/queue/MyQueue.java b/Java/data-structure-with-java/queue/MyQueue.java new file mode 100644 index 0000000..69ec04f --- /dev/null +++ b/Java/data-structure-with-java/queue/MyQueue.java @@ -0,0 +1,89 @@ +package queue; + +class MyQueue { + private int[] array; + private int front; + private int rear; + private int capacity; + private int size; + + MyQueue(int size) { + array = new int[size]; + capacity = size; + front = 0; + rear = -1; + this.size = 0; + } + + /** + * dequeue from queue + */ + public void dequeue() { + if (isEmpty()) { + System.out.println("Can't dequeue, Queue is empty"); + return; + } + System.out.println("Dequeue:" + array[front]); + front = (front + 1) % capacity; + size--; + } + + /** + * enqueue an item to the queue + * @param item + */ + public void enqueue(int item) { + if (isFull()) { + System.out.println("Can't enqueue, Queue is full"); + return; + } + System.out.println("Enqueuing:" + item); + rear = (rear + 1) % capacity; + array[rear] = item; + size++; + } + + /** + * @return the size of the queue + */ + public int size() { + return size; + } + + /** + * checks if queue is empty + * @return + */ + public Boolean isEmpty() { + return (size() == 0); + } + + /** + * check if queue is full + * @return + */ + public Boolean isFull() { + return (size() == capacity); + } + + /** + * main method + * @param args + */ + public static void main(String[] args) { + MyQueue queue = new MyQueue(3); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + queue.enqueue(4); + queue.dequeue(); + queue.dequeue(); + queue.dequeue(); + queue.dequeue(); + System.out.println("The queue size is " + queue.size()); + queue.enqueue(5); + System.out.println("The queue size is " + queue.size()); + queue.dequeue(); + System.out.println("The queue size is " + queue.size()); + } +} \ No newline at end of file diff --git a/Java/data-structure-with-java/stack/MyStack.java b/Java/data-structure-with-java/stack/MyStack.java new file mode 100644 index 0000000..8d086ce --- /dev/null +++ b/Java/data-structure-with-java/stack/MyStack.java @@ -0,0 +1,84 @@ +package stack; + +class MyStack +{ + private int array[]; + private int top; + private int capacity; + + MyStack(int size) + { + array = new int[size]; + capacity = size; + top = -1; + } + + /** + * + * @param item + */ + public void push(int item) + { + if (isFull()) + { + System.out.println("Can't push the element, Stack is full"); + return; + } + System.out.println("Pushing: " + item); + array[++top] = item; + } + + /** + * + * @return + */ + public int pop() + { + if (isEmpty()) { + System.out.println("Can't pop the element, Stack is empty"); + return -1; + } + System.out.println("Popping top element:"+array[top]); + return array[top--]; + } + + /** + * returns the size of stack + * @return + */ + public int size() { + return top + 1; + } + + /** + * checks is stack is empty + * @return + */ + public Boolean isEmpty() { + return size() == 0; + } + + /** + * checks if stack is full + * @return + */ + public Boolean isFull() { + return size() == capacity; + } + + public static void main (String[] args) { + MyStack stack = new MyStack(3); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.pop(); + stack.pop(); + stack.pop(); + stack.pop(); + stack.push(5); + System.out.println("The stack size is " + stack.size()); + stack.pop(); + System.out.println("The stack size is " + stack.size()); + } +}