CodesJava

Easy learning with example program codes

Java convert a decimal number to binary using stack


A stack is an ADT – Abstract Data Type or a linear data structure. It is a LIFO data structure because it allows all data operations at one end only i.e. elements can be added and removed from the stack only at the top. LIFO stands for Last-in-first-out. The element which is inserted last, is accessed first.

Stack operations

  • push(): Pushing an element on the stack.
  • pop(): Removing an element from the stack.
  • peek(): Get the top data element of the stack, without removing it.
  • isFull(): Check if stack is full.
  • isEmpty(): Check if stack is empty.

Stack states

Overflow state: A stack is in overflow state if it does not contain enough space to accept an entity to be pushed.
Underflow state: A stack is in underflow state if we want to operate stack with pop operation and the stack is empty.

Stacks efficiency

As we discussed, all operations are done at the top of the stack so no comparisons or moves of elements are necessary. Hence it is very fast.

Examples

package com.codesjava;
 
public class Test {
    private int stackSize;
    private int[] stackArray;
    private int top;
 
    /**
     * constructor to create stack with size
     * @param size
     */
    public Test(int size) {
        this.stackSize = size;
        this.stackArray = new int[stackSize];
        this.top = -1;
    }
 
    /**
     * Adds new entry to the top of the stack
     * @param entry
     * @throws Exception 
     */
    public void push(int entry) throws Exception {
    	this.stackArray[++top] = entry;
    }
 
    /**
     * Removes an entry from the top of the stack.
     * @return
     * @throws Exception 
     */
    public int pop() throws Exception {
        if(this.isStackEmpty()){
        	System.out.println("Stack underflow.");
        }
        return this.stackArray[top--];
    }
 
    /**
     * Returns top of the stack without removing it.
     * @return
     */
    public int peek() {
        return stackArray[top];
    }
 
    /**
     * Returns true if the stack is empty
     * @return
     */
    public boolean isStackEmpty() {
        return (top == -1);
    }
 
    /**
     * Returns true if the stack is full
     * @return
     */
    public boolean isStackFull() {
        return (top == stackSize - 1);
    }
 
    public String convertDecialToBinary(int number) throws Exception{        
        StringBuilder binary = new StringBuilder();
        if(number == 0){
            binary.append("0");
        } else {
            while(number != 0){
                push(number%2);
                number = number/2;
            }
        }
        while(!isStackEmpty()){
            try {
                binary.append(pop());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return binary.toString();
    }
 
	public static void main(String args[]){
		try {
			Test test = new Test(10);
			int number = 15;
			System.out.println("Original number: " + number);
			System.out.println("Binary number: " + test.convertDecialToBinary(number));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output

Original number: 15
Binary number: 1111


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap