Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions queue.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
99 changes: 99 additions & 0 deletions stack.java
Original file line number Diff line number Diff line change
@@ -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<stackSize;i++){
newStack[i] = this.stackArr[i];
}
this.stackArr = newStack;
this.stackSize = this.stackSize*2;
}

/**
* This method returns true if the stack is
* empty
* @return
*/
public boolean isStackEmpty() {
return (top == -1);
}

/**
* This method returns true if the stack is full
* @return
*/
public boolean isStackFull() {
return (top == stackSize - 1);
}

public static void main(String[] args) {
MyDynamicStack stack = new MyDynamicStack(2);
for(int i=1;i<10;i++){
stack.push(i);
}
for(int i=1;i<4;i++){
try {
stack.pop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}