Here decimal to binary/octa/hexa decimal conversions are shown.

Algorithm is simple provided we should make use of the stack (stack implementation) to put the coefficients.

```public class DecimalConversion {
TNStack st = new TNStack(30);
private static HashMap<Integer, String> hm = new HashMap<Integer, String>();

/**
* converts number to given base
* @param number
* @param base
*/
public void decimalToXXXConversion(int number, int base) {
System.out.println("Number "+number+" in base "+base+" is: ");
while(number>0) {
st.push(number%base);
number = number/base;
}

while(!st.isEmpty()) {
int n = st.pop();
if(n>9) {
String str = hm.get(n);
System.out.print(str);
} else {
System.out.print(n);
}
}
System.out.println();
}

/**
* @param args
*/
public static void main(String[] args) {
DecimalConversion dc = new DecimalConversion();

// decimal to binary conversion
dc.decimalToXXXConversion(100, 2);

// decimal to octal conversion
dc.decimalToXXXConversion(100, 8);

hm.put(10, "A");
hm.put(11, "B");
hm.put(12, "C");
hm.put(13, "D");
hm.put(14, "E");
hm.put(15, "F");
dc.decimalToXXXConversion(10012, 16);
}
}
```

Output:
Number 100 in base 2 is:
1100100
Number 100 in base 8 is:
144
Number 10012 in base 16 is:
271C

Queue implementation in Java

Queues operate in FIFO model.

Queues are also used in various other data structures, some of them are

• searching graphs
• printer queues in our machines
• keystroke data that is typed onto the keyboard

Efficiency of queue is that it performs insert and remove operations in O(1) complexity.

```package algorithm.queue;

/**
* @author ntallapa
*
*/
public class TNQueue {
private int size;
private int[] queueArr;
private int front = -1;
private int rear = -1;
private int totalItems;

public TNQueue(int s) {
size = s;
queueArr = new int[s];
}

public void insert(int i) {
rear++;
System.out.println("Inserting "+i);
queueArr[rear] = i;
totalItems++;
}

public int remove() {
front++;
totalItems--;
System.out.println("Removing "+queueArr[front]);
return queueArr[front];
}

public boolean isFull() {
return (totalItems == size);
}

public boolean isEmpty() {
return (totalItems == 0);
}
}

package algorithm.queue;
/**
* @author ntallapa
*
*/
public class TNQueueClient {

/**
* @param args
*/
public static void main(String[] args) {
TNQueue tnq = new TNQueue(3);
if(!tnq.isFull())
tnq.insert(1);
if(!tnq.isFull())
tnq.insert(2);
if(!tnq.isFull())
tnq.insert(3);
if(!tnq.isFull())
tnq.insert(4);
else
System.out.println("Queue is full, cannot insert element");

if(!tnq.isEmpty())
tnq.remove();
if(!tnq.isEmpty())
tnq.remove();
if(!tnq.isEmpty())
tnq.remove();
if(!tnq.isEmpty())
tnq.remove();
else
System.out.println("Queue is empty, cannot remove element");
}
}
```

Output:
Inserting 1
Inserting 2
Inserting 3
Queue is full, cannot insert element
Removing 1
Removing 2
Removing 3
Queue is empty, cannot remove element

Stack implementation in Java

Stacks operate in LIFO model.

Stack plays vital role in many data structures, some of them are

• in parsing arithmetic expressions
• to help traverse nodes of binary tree
• searching vertices of a graph
• in java, every method’s return type and arguments are pushed on to a stack and when method returns they are popped off.

Efficiency of stack is that it performs push and pop operations in O(1) complexity.

```package algorithm.stack;
/**
* @author ntallapa
*
*/
public class TNStack {
private int size;
private int[] stackArr;
private int top = -1;

public TNStack(int size) {
this.size = size;
stackArr = new int[size];
}

/**
* increment the ctr and push element into stack
* @param i element to be pushed
*/
public void push(int i) {
top++;
System.out.println("Pushing "+i);
stackArr[top] = i;
}

/**
* pop the element from stack and decrement the ctr
* @return the popped element
*/
public int pop() {
int i = stackArr[top];
top--;
System.out.println("Popping "+i);
return i;
}

public int peek() {
System.out.println("Peek "+stackArr[top]);
return stackArr[top];
}

public boolean isFull() {
return (top == size-1);
}

public boolean isEmpty() {
return (top == -1);
}
}

package algorithm.stack;
/**
* @author ntallapa
*
*/
public class TNStackClient {

/**
* @param args
*/
public static void main(String[] args) {
TNStack tns = new TNStack(3);
// push some elements
if(!tns.isFull())
tns.push(4);
if(!tns.isFull())
tns.push(5);
if(!tns.isFull())
tns.push(3);
if(!tns.isFull())
tns.push(6);
else
System.out.println("Stack is full, cannot push element");

// pop some elements
if(!tns.isEmpty())
tns.pop();
if(!tns.isEmpty())
tns.pop();
if(!tns.isEmpty())
tns.pop();
if(!tns.isEmpty())
tns.pop();
else
System.out.println("Stack is empty, cannot pop element");

//reinsert to verify peek method
if(!tns.isFull())
tns.push(6);

// peek couple of times; result should be same
tns.peek();
tns.peek();
}
}

```

Output:
Pushing 4
Pushing 5
Pushing 3
Stack is full, cannot push element
Popping 3
Popping 5
Popping 4
Stack is empty, cannot pop element
Pushing 6
Peek 6
Peek 6

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey