Posts

Showing posts from November, 2018

stack example in java

Image
Stack:-stack used in data structure to insert and delete element from top. LIFO:-Last In First Out. Following example illustrate Stack. class MyStack { private int SIZE; private int top=-1; private int[] arr; MyStack() { SIZE=5; arr=new int[5]; } MyStack(int SIZE) { this.SIZE=SIZE; arr=new int[SIZE]; }     //empty method public boolean isEmpty() { return top==-1; } //full method public boolean isFull() { return top==SIZE-1; }     //push method public void push(int num) { if(isFull()) { System.out.println("push perform on full Stack"); } else { top++; arr[top]=num; System.out.println(num+" is pushed"); } }     //pop method public int pop() { if(isEmpty()) { System.out.println("pop perform on empty stack"); return -000; } else { int x=arr[top]; top--; System.out.println(x+" is pop"); return x; } } } class TestStack { publi...

Queue in java

Image
Queue:-queue is used in data structure to insert data at the end and remove at top. following are complete example of queue. public class Queue { int rear=-1; int front=-1; int size; int qArr[]; Queue (){ size=10; qArr=new int[size];  } Queue (int a){ size=a; qArr=new int[size];  } public boolean  isEmpty()                    { if(rear==front) return true; else return false;                  } public boolean isfull() { if(rear==size-1) return true; else return false;   } public void enqueue(int n) { if(isfull()) { System.out.println("Queue is full."); } else { if(front==-1) front++; rear++; qArr[rear]=n;        } } public int dequeue() {int temp=-9999; if(isEmpty()) { System.out.println("Stack is empty!"); } else { temp=qArr[fron...