Data Structures and Algorithm
This commit is contained in:
89
Java/data-structure-with-java/queue/MyQueue.java
Normal file
89
Java/data-structure-with-java/queue/MyQueue.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
84
Java/data-structure-with-java/stack/MyStack.java
Normal file
84
Java/data-structure-with-java/stack/MyStack.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user