Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Saturday, December 12, 2015

get InOrder sucessor of a given node in a BST

    private Stack<BinarySearchTree> S;
    public int inOrderSuccessor(int data)    {
        S = new Stack<
BinarySearchTree>();
       
BinarySearchTree Current = getNode(this, data);
        if(Current.right!=null)    {
            return getMin(Current.right).data;
        }    else    {
           
BinarySearchTree parent = S.pop();
            if(parent.left==Current)
                return parent.data;
            else
                return S.pop().data;
        }
    }
    private
BinarySearchTree getMin(BinarySearchTree T)    {
        while(T.left!=null)
            T=T.left;
        return T;
    }
    private
BinarySearchTree getNode(BinarySearchTree T, int data)    {
        if(T==null)    return null;
        else if(data==T.data)        {
            return T;
        }
        else if(data<T.data) {
            S.add(T);
            return getNode(T.left, data);
        }
        else {
            S.add(T);
            return getNode(T.right, data);
        }
    }

Wednesday, December 2, 2015

Stack Implementation using two Queues

import java.util.LinkedList;
import java.util.Queue;

public class Stack {
    Queue<Integer> Q1,Q2;
    public Stack() {
        Q1=new LinkedList<Integer>();
        Q2=new LinkedList<Integer>();
    }
    public void push(int data)    {
        Q1.add(data);
    }
    public int peek()    {
        int size=Q1.size()-1;
        for(int i=0; i<size;i++)
            Q2.add(Q1.remove());
        int peek = Q1.remove();
        Q2.add(peek);
        SwapQueues();
        return peek;
    }
    public int pop()    {
        int size=Q1.size()-1;
        for(int i=0; i<size;i++)
            Q2.add(Q1.remove());
        int pop = Q1.remove();
        SwapQueues();
        return pop;
    }
    private void SwapQueues()    {
        Queue<Integer> temp = Q1;
        Q1=Q2;
        Q2=temp;
    }
}

Queue Implementation using Two Stacks

import java.util.Stack;

public class Queue {
    Stack<Integer> S1;
    Stack<Integer> S2;
    public Queue() {
        S1 = new Stack<Integer>();
        S2 = new Stack<Integer>();
    }
    public void enqueue(int data)    {
        S1.push(data);
    }
    private void ShiftS1toS2()    {
        while(!S1.isEmpty())
            S2.push(S1.pop());
    }
    public int dequeue()    {
        ShiftS1toS2();
        return S2.pop();
    }
    public int lookup()    {
        ShiftS1toS2();
        return S2.peek();
    }
}

Sort a Stack using one additional Stack

    public Stack<Integer> SortStack(Stack<Integer> input)    {
        if(input.isEmpty())    return input;
        Stack<Integer> buffer = new Stack<Integer>(); //Buffer Stack
        while(!input.isEmpty())    {
            int temp = input.pop();
            if(!buffer.isEmpty() && temp<buffer.peek())
                input.push(buffer.pop());
            buffer.push(temp);
        }
        return buffer;
    }



This Code doesn't work all the time
Input: [5, 7, 8, 9, 6, 0, 2]
Output: [0, 2, 6, 8, 7, 5, 9]

Monday, November 30, 2015

Check if Parentheses are Balanced


import java.util.Stack;

class Parenthesis    {
    public boolean isParenthesisCorrect(String s)    {
        char[] c = s.toCharArray();
        int size=c.length;
        Stack S = new Stack<Character>();
        boolean flag = true;
        for(int i=0; i<size; i++)    {
            if(c[i]=='(')
                S.push(c[i]);
            else if(c[i]==')')    {
                if(!((Character)S.pop()=='('))    {
                    flag=false;
                    break;
                }
            }
        }
        if(!S.isEmpty())
            flag=false;
        return flag;
    }
}

public class Main {
    public static void main(String[] args) {
        Parenthesis P = new Parenthesis();
        String s = "((a+b)*(a-b))/c";
        System.out.println(P.isParenthesisCorrect(s));
    }
}

Saturday, November 28, 2015

Implementing 3 fixed size Stacks using same Array

public class ThreeStacks {
    int ArraySize;
    int[] Array;
    int[] topPointers =  {-1,-1,-1};
    public ThreeStacks(int size) {
        this.ArraySize = size;
        this.Array = new int[size * 3];
    }
    public int peek(int StackNumber)    {
        if(topPointers[StackNumber-1]>-1)    {
            return Array[(ArraySize*StackNumber)+topPointers[StackNumber]];
        }    else    {
            System.out.println("Error: "+StackNumber+" is empty");
            return ErrorCode;
        }
    }
    public static final int ErrorCode = -999;
    public int pop(int StackNumber)    {
        StackNumber--;
        if(topPointers[StackNumber]>-1)    {
            int ret = Array[(ArraySize*StackNumber)+topPointers[StackNumber]];
            topPointers[StackNumber]--;
            return ret;
        }    else    {
            System.out.println("Error: "+StackNumber+" is empty");
            return ErrorCode;
        }
    }
    public void push(int data, int StackNumber)    {
        int maxSize = (ArraySize*StackNumber)-1;
        if(topPointers[StackNumber-1]<maxSize)    {
            topPointers[StackNumber-1]++;
            int nextLoc = (ArraySize*(StackNumber-1))+topPointers[StackNumber-1];
            Array[nextLoc]=data;
        }    else    {
            System.out.println("Stack "+StackNumber+" is full");
        }
    }
    public void print3Stacks()    {
        StringBuffer sb =new StringBuffer();
        for(int i=0; i<=topPointers[0]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
        sb =new StringBuffer();
        for(int i=ArraySize; i<=ArraySize+topPointers[1]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
        sb =new StringBuffer();
        for(int i=(ArraySize*2); i<=(ArraySize*2)+topPointers[2]; i++)    {
            sb.append(Array[i]);
        }
        System.out.println(sb.toString());
    }
}

Stack Implementation using Linked List

This is a Stack Implementation using LinkedList in Java. As in Java we don't have pointers and hard to remove head node in a empty list, we are using a dummy head node.

public class Stack {
    Stack next;
    int data;
    private int size;
    public Stack() {
        this.size=-1;
        this.data=-999; //Garbage Value to denote Head
        this.next=null;
    }
    public Stack(int data) {
        this.data=data;
        this.next=null;
    }
    public void push(int... data)    {
        int s = data.length;
        for(int i=0; i<s; i++)
            push(data[i]);
    }
    public void push(int data)    {
        Stack S = this;
        if(size==-1)    {
            S.next=new Stack(data);
        }    else    {
            Stack newNode = new Stack(data);
            newNode.next=S.next; //Head is a dummy node here
            S.next=newNode;
        }
        size++;
    }
    public final static int ErrorCode = -999;
    public int pop()    {
        Stack S = this;
        if(size>-1)    {
            int value = S.next.data;
            S.next=S.next.next;
            size--;
            return value;
        }    else    {
            System.out.println("Error: Stack is Empty");
            return ErrorCode;
        }
    }
    public int size()    {
        return size;
    }
    public int peek()    {
        Stack S = this;
        if(size>-1)    {
            return S.next.data;
        }    else    {
            System.out.println("Error: Stack is Empty");
            return ErrorCode;
        }
    }
    public void printStack()    {
        Stack S = this;
        S=S.next; // first node is dummy
        StringBuffer sb = new StringBuffer();
        while(S!=null)    {
            sb.append(S.data).append(", ");
            S=S.next;
        }
        System.out.println(sb.toString());
    }
}

Friday, November 27, 2015

Stack implementation using Array

public class Stack {
    int[] array;
    private int top;
    private int size;
    public Stack(int size) {
        this.size=size;
        array = new int[size];
        top = -1;
    }
    public void push (int... data)    {
        int size = data.length;
        for(int i=0; i<size; i++)
            push(data[i]);
    }
    public void push (int data)    {
        if (top<size-1)    {
            top++;
            array[top]=data;
        }
    }
    public final static int ErrorCode = -999;
    public int pop()    {
        if(!isEmpty())    {
            int a = array[top];
            top--;
            return a;
        }    else return ErrorCode;
    }
    public int peek()    {
        return array[top];
    }
    public boolean isEmpty()    {
        if(top<0) return true;
        else    return false;
    }
    public void printArray()    {
        int size = top+1;
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<size; i++)
            sb.append(array[i]).append(", ");
        System.out.println(sb.toString());   
    }
}
UA-39217154-2