Queue data structure using Array in Java

Posted By: Java Examples - 3:52 PM
A queue is a linear list of elements in which deletions can take place only at one end, called the front of the stack, and insertion can take place only at the other end, called the rear. The term FRONT and REAR are used in describing a linear list only when it is implanted as queue. Queues are also called First in First out (FIFO) Lists. This makes the queue a First-In-First-Out (FIFO) data structure. 
In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked.

Generic Type in Java

Posted By: Java Examples - 10:16 PM
The term generics here means parameterized types. Parameterized types are important because they enable you to create
classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter. 
Using
generics, it is possible to create a single class, for example, that automatically works with different types of data. A class,
interface, or method that operates on a parameterized type is called generic, as in generic class or generic method.

Convert infix to postfix using stack in Java

Posted By: Java Examples - 9:14 PM

Infix Expression :

Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.

Postfix Expression :

The Postfix(Postorder) form of the above expression is "23*45/-".

Example : 

  • infix (1+2)*(3+4)
    • with parentheses: ((1+2)*(3+4))
    • in postfix: 12+34+*
    • in prefix: *+12+34
  • infix 1^2*3-4+5/6/(7+8)
    • paren.: ((((1^2)*3)-4)+((5/6)/(7+8)))
    • in postfix: 12^3*4-56/78+/+
    • in prefix: +-*^1234//56+78
  • Scan the Infix string from left to right.
  • Initialise an empty stack.
  • If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character tostack.
  • If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack(topStack). If topStack has higher precedence over the scanned character Popthe stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
  • (After all characters are scanned, we have to add any character that the stackmay have to the Postfix string.) If stack is not empty add topStack to Postfix stringand Pop the stack. Repeat this step as long as stack is not empty.
  • Return the Postfix string.

Infix to Postfix Conversion : 

In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. 
The algorithm for the conversion is as follows :
Repeat this step till all the characters are scanned.


Java version of infix to postfix  is given bellow :

/**
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package stack;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class Infix2Postfix extends Stack {

    public Infix2Postfix(int x) {
        super(x);
    }

    /**
     *@return postfixString value
     */
   
    public String InToPost(String infixString) {
        String postfixString = " ";

        for (int index = 0; index < infixString.length(); ++index) {
            char chValue = infixString.charAt(index);
            if (chValue == '(') {
                push('(');
            } else if (chValue == ')') {
                Character oper = peek();
                while (!(oper.equals('(')) && !(isEmpty())) {
                    postfixString += oper.charValue();
                    pop();
                    oper = peek();
                }
                pop();
            } else if (chValue == '+' || chValue == '-') {
                //Stack is empty
                if (isEmpty()) {
                    push(chValue);
                    //current Stack is not empty
                } else {
                    Character oper = peek();
                    while (!(isEmpty() || oper.equals(new Character('(')) || oper.equals(new Character(')')))) {
                        pop();
                        postfixString += oper.charValue();
                    }
                    push(chValue);
                }
            } else if (chValue == '*' || chValue == '/') {
                if (isEmpty()) {
                    push(chValue);
                } else {
                    Character oper = peek();
                    while (!oper.equals(new Character('+')) && !oper.equals(new Character('-')) && !isEmpty()) {
                        pop();
                        postfixString += oper.charValue();
                    }
                    push(chValue);
                }
            } else {
                postfixString += chValue;
            }
        }
        while (!isEmpty()) {
            Character oper = peek();
            if (!oper.equals(new Character('('))) {
                pop();
                postfixString += oper.charValue();
            }
        }
        return postfixString;
    }

    public static void main(String[] args) {
        Infix2Postfix mystack = new Infix2Postfix(10);
        System.out.println("Type in an expression like (1+2)*(3+4)/(12-5)\n "
                + "with no monadic operators like in-5 or +5 followed by key");
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        System.out.println("The Expression you have typed in infix form :\n"+str);
        System.out.println("The Equivalent Postfix Expression is :\n"+mystack.InToPost(str));
    }
}
Output of this program :

run:
Type in an expression like (1+2)*(3+4)/(12-5)
 with no monadic operators like in-5 or +5 followed by <ENTER>key
(1+2)*(3+4)
The Expression you have typed in infix form :
(1+2)*(3+4)
The Equivalent Postfix Expression is :
 12+34+*
BUILD SUCCESSFUL (total time: 28 seconds)

Convert Decimal to octal using stack in java

Posted By: Java Examples - 1:04 PM
You all are aware of Stacks. It performs two basic operations push and pop. The push operation adds an element to the top of the list, or initializing the stack if it is empty and the pop operation removes an item from the top of the list. Using these operations, we have converted a decimal number into Octal.
  • Divide the decimal number by 8
  • Treat the division as an integer division. Write down the remainder (in Octal).
  • Divide the result again by 8.
  • Treat the division as an integer division. Repeat step 2 and 3 until result is 0.
  • The Oct value is the digit sequence of the remainders from the last to first.

Convert Decimal to Hexadecimal using stack in java

Posted By: Java Examples - 12:46 PM
You all are aware of Stacks. It performs two basic operations push and pop. The push operation adds an element to the top of the list, or initializing the stack if it is empty and the pop operation removes an item from the top of the list. Using these operations, we have converted a decimal number into Hexadecimal .
  • Divide the decimal number by 16
  •  Treat the division as an integer division. Write down the remainder (in hexadecimal). 
  • Divide the result again by 16. 
  •  Treat the division as an integer division. Repeat step 2 and 3 until result is 0. 
  • The hex value is the digit sequence of the remainders from the last to first.

From National Identity Card(NIC) print date of birth in java

Posted By: Java Examples - 11:09 AM
The following numbering system is followed in order to assign this number:
  • Digits 1 and 2: The year of birth
  • Digits 3-5: The number of the day in the year on which the person's birth date falls.
    • A male would be assigned the number 1-366 and a female the number 501-866. This is a way of confirming if the person's listed birth date is accurate. A male's birth date would be represented by the exact number of days, a female's birth date would be represented by the exact number of days + 500.
      •    Example: The first five digits of the NIC for a male born on 1 January 1997   would be 97001; however, a female born on that same date would be 97501.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .