Showing posts with label Stacks. Show all posts
Showing posts with label Stacks. Show all posts

Monday, 11 July 2016

Tower of Hanoi

Following Java program shows  demo of famous "Tower of Hanoi" Problem.

Tower of Hanoi     :

import java.util.*;
class TowerOfHonaoi
{

  public static void main(String[] args) {
    System.out.println("Enter No. of discs");
    Scanner sc = new Scanner(System.in);
    int no = sc.nextInt();
    move(no,"A","B","C");
  }

  public static void move(int n , String src, String inter, String dest)
  {
    if(n==1)
    {
        System.out.println(src+" -> "+dest);
    }else{
      move(n-1,src,dest,inter);
      System.out.println(src+" -> "+dest);
      move(n-1,inter,src,dest);
    }
  }

}

Thursday, 30 June 2016

Convert InFix Expression to PostFix Expression using stacks

InFix Expression to PostFix Expression using stacks   :

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostFix {
    public static void main(String args[]) {
        Stack<Character> stack = new Stack<Character>();
        Scanner sc = new Scanner(System.in);
        // Read InFix String
        char inFixArray[] = sc.next().toCharArray();
        String postFixString = "";
        for (char e : inFixArray) {
            // check for operand
            if (e >= 'A' && e <= 'Z') {
                postFixString = postFixString + e;
            } else if (e >= 'a' && e <= 'z') {
                postFixString = postFixString + e;
            } else if (e == '(') {
                stack.push(e);
            } else if (e == ')') {
                while (!stack.isEmpty() && stack.peek() != '(') {
                    postFixString = postFixString + stack.pop();
                }
                stack.pop();
            } else {
                // check for operator
                while (!stack.isEmpty() && Prec(stack.peek()) >= Prec(e)) {
                    postFixString = postFixString + stack.pop();
                }
                stack.push(e);

            }
        }
        while (stack.isEmpty()) {
            postFixString = postFixString + stack.pop();
        }
        System.out.println(postFixString);

    }

    static int Prec(char e) {
        switch (e) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;

        case '^':
            return 3;

        }
        return -1;
    }
}

Wednesday, 29 June 2016

Evaluate PostFix Expression using stack

Evaluate PostFix Expression using stack   :

import java.util.Scanner;
import java.util.Stack;

public class EvalPostFix {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Stack<Integer> stack = new Stack<Integer>();
        char exp[] = sc.next().toCharArray();
        for (char e : exp) {
            if (Character.isDigit(e)) {
                stack.push(Character.getNumericValue(e));
            } else {
                int var1 = stack.pop();
                int var2 = stack.pop();
                switch (e) {
                case '+':
                    stack.push((var1 + var2));
                    break;
                case '-':
                    stack.push((var1 - var2));
                    break;
                case '*':
                    stack.push((var1 * var2));
                    break;
                case '/':
                    stack.push((var1 / var2));
                    break;
                }

            }
        }
        System.out.println("" + stack.peek());

    }

}
 

Stack Implementation using LinkedList

Stack Implementation using LinkedList in Java     :

import java.util.*;

public class DynamicStack {
 static class Node {
  int data;
  Node next;
  Node(int e) {
   this.data = e;
   this.next = null;
  }
 }
 LinkedList < Node > list;
 DynamicStack() {
  this.list = new LinkedList < Node > ();
 }
 public void push(int data) {
  list.add(new Node(data));
 }
 public int pop() {
  if (list.size() < 1)
   return -1;
  return list.remove().data;
 }
 public int Top() {
  return list.getLast().data;
 }
 public boolean isEmpty() {
  return list.size() < 1 ? true : false;
 }
 public static void main(String[] args) {
  DynamicStack stack = new DynamicStack();
  stack.push(1);
  stack.push(2);
  stack.push(3);
  stack.push(4);
  stack.push(5);
  while (!stack.isEmpty()) {
   System.out.println(stack.pop());
  }

 }
}

Stack Implementation

Stack Implementation in Java :
Following example shows how to implement stack in Java .

/**
 *
 * @author Anurag
 *
 *
 */
public class ArrayStack{

    private int array[];
    private int top;
    private int capacity;

ArrayStack(int capacity)
{
    this.capacity=capacity;
    this.array=new int[capacity];
    top=-1;
}
public boolean isEmpty()
{
    return top==-1?true:false;
}
public boolean isFull()
{
    return top==capacity-1?true:false;
}
public int Top()
{
    if (top==-1)
    {
        System.out.println("Stack is Empty");
    return -1;
}
    return array[top];
}

public void push(int e)
{
    if (top==capacity-1)
    {
        System.out.println("Stack is Full !");
        return;
    }
    array[++top]=e;
    return;
}

public int pop()
{
    if (top==-1)
    {
        System.out.println("Stack is Empty !");
        return -1;
    }
    return array[top--];
}
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);
        stack.push(1);stack.push(2);stack.push(3);stack.push(4);stack.push(5);stack.push(6);
        System.out.println("Stack is Full :: "+stack.isFull());
        while(!stack.isEmpty())
        {
            System.out.println(""+stack.pop());
        }
        System.out.println("Stack is Empty :: "+stack.isEmpty());

    }
}