Showing posts with label Stack and Queue In Java. Show all posts
Showing posts with label Stack and Queue In Java. Show all posts

Monday, December 23, 2013

Simple Example Program For Queue In Java Using Array and Class

Queue Wiki Definition:


In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “dequeue” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Simple Example Program For Queue In Java Using Array and Class




import java.io.*;


class QueueAction {


    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

    int items[];

    int i, front = 0, rear = 0, noOfItems, item, count = 0;


    void getdata() {

        try {

            System.out.println("Enter the Limit :");

            noOfItems = Integer.parseInt(is.readLine());

            items = new int[noOfItems];

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }


    void enqueue() {

        try {

            if (count < noOfItems) {

                System.out.println("Enter Queue Element :");

                item = Integer.parseInt(is.readLine());

                items[rear] = item;

                rear++;

                count++;

            } else {

                System.out.println("Queue Is Full");

            }

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }


    void dequeue() {

        if (count != 0) {

            System.out.println("Deleted Item :" + items[front]);

            front++;

            count--;

        } else {

            System.out.println("Queue IS Empty");

        }

        if (rear == noOfItems) {

            rear = 0;

        }

    }


    void display() {

        int m = 0;

        if (count == 0) {

            System.out.println("Queue IS Empty");

        } else {

            for (i = front; m < count; i++, m++) {

                System.out.println(" " + items[i]);

            }

        }

    }

}


class QueueProgram {


    public static void main(String arg[]) {

        DataInputStream get = new DataInputStream(System.in);

        int choice;

        QueueAction queue = new QueueAction();

        queue.getdata();

        System.out.println("Queue\n\n");

        try {

            do {

                System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");

                System.out.println("Enter the Choice : ");

                choice = Integer.parseInt(get.readLine());

                switch (choice) {

                    case 1:

                        queue.enqueue();

                        break;

                    case 2:

                        queue.dequeue();

                        break;

                    case 3:

                        queue.display();

                        break;

                }

            } while (choice != 4);

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }

}


Sample Output:


Enter the Limit :

4

Queue


1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

45

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

67

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

89

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Enter Queue Element :

567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

1

Queue Is Full

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

3

 45

 67

 89

 567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :45

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :67

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :89

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Deleted Item :567

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

2

Queue IS Empty

1.Enqueue

2.Dequeue

3.Display

4.Exit


Enter the Choice :

4

Simple Example Program For Queue In Java

Queue Wiki Definition:


In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “dequeue” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Simple Example Program For Queue In Java




import java.io.IOException;

import java.util.LinkedList;

import java.util.Queue;


public class QueueExample {


    public static void main(String arg[]) throws IOException {

        Queue<Integer> q = new LinkedList<Integer>();

        

        // Add in Queue

        q.add(23);

        q.add(33);

        System.out.println(q);

        System.out.println("Queue Element :"+q.element());

        // Offer In Queue 

        q.offer(34);

        q.offer(98);

        q.offer(77);


        System.out.println(q);

        System.out.println("Queue Element :"+q.element());

        q.poll();

        System.out.println("Queue Element :"+q.element());

        System.out.println("After poll : " + q);

    }

}


Sample Output:


[23, 33]

Queue Element :23

[23, 33, 34, 98, 77]

Queue Element :23

Queue Element :33

After poll : [33, 34, 98, 77]

Sunday, December 22, 2013

Simple Example Program For Stack in Java Using Array and Class

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack in Java Using Array and Class




// Simple Example Program For Stack in Java Using Array and Class

// Coded By Thiyagaraaj M.P


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


class Stack {


    private int top;

    private int item[];


    Stack(int size) {

        top = -1;

        item = new int[size];

    }


    void pushItem(int data) {

        if (top == item.length - 1) {

            System.out.println("Stack is Full");

        } else {

            item[++top] = data;

            System.out.println("Pushed Item :" + item[top]);

        }

    }


    int popItem() {

        if (top < 0) {

            System.out.println("Stack Underflow");

            return 0;

        } else {

            System.out.println("Pop Item : " + item[top]);

            return item[top--];

        }

    }

}


class StackExample {


    public static void main(String[] args) throws IOException {

        Stack stk = new Stack(5);

        boolean yes=true;

        int choice;

        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

        

        do{

            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");

            choice = Integer.parseInt(is.readLine());

            

            switch(choice)

            {

                case 1: System.out.println("Enter Push Item: ");

                        stk.pushItem(Integer.parseInt(is.readLine()));

                        break;

                case 2: stk.popItem();break;

                case 3: yes = false;break;

                default: System.out.println("Invalid Choice");

            }

        }while(yes==true);

        

    }

}


Sample Output:


1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

14

Pushed Item :14

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

567

Pushed Item :567

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

67

Pushed Item :67

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

789

Pushed Item :789

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

56

Pushed Item :56

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

99

Stack is Full

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 56

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 789

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 67

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 567

1).Push

2).Pop

3).Exit


Enter Choice

2

Pop Item : 14

1).Push

2).Pop

3).Exit


Enter Choice

2

Stack Underflow

1).Push

2).Pop

3).Exit


Enter Choice

3

Simple Example Program For Stack in Java Stack Utils

Definition:


A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack in Java Stack Utils




// Simple Example Program For Stack in Java Stack Utils

// Coded By Thiyagaraaj M.P


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Stack;


class StackProgram {


    public static void main(String[] args) throws IOException {

        Stack stk = new Stack();

        stk.setSize(5);

        

        boolean yes=true;

        int choice;

        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));

        

        do{

            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");

            choice = Integer.parseInt(is.readLine());

            

            switch(choice)

            {

                case 1: System.out.println("Enter Push Item: ");

                        stk.push(Integer.parseInt(is.readLine()));

                        break;

                case 2: System.out.println("Poped Item : "+stk.pop());break;

                case 3: yes = false;break;

                default: System.out.println("Invalid Choice");

            }

        }while(yes==true);

        

    }

}


Sample Output:


run:

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

23

1).Push

2).Pop

3).Exit


Enter Choice

1

Enter Push Item:

45

1).Push

2).Pop

3).Exit


Enter Choice

2

Poped Item : 45

1).Push

2).Pop

3).Exit


Enter Choice

2

Poped Item : 23

1).Push

2).Pop

3).Exit

Simple Example Program For Stack in Java( Simple Way Of Use )

Definition:


A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack In Java


// Simple Example Program For Stack ( Simple Way Of Use )

// Coded By Thiyagaraaj M.P

import java.util.Stack;


public class SimpleStack {


    public static void main(String[] args) {

        Stack stack = new Stack();


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());


        System.out.println("Stack Push \n");

        stack.push("A");

        stack.push(new Integer(20));

        stack.push("Example");


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());


        System.out.println("Stack Pop \n");

        System.out.println("Pop Data :" + stack.pop());

        System.out.println("Pop Data " + stack.pop());

        System.out.println("Pop Data " + stack.pop());


        System.out.println("Stack Items \n" + stack);

        System.out.println("Stack Size :" + stack.size());

    }

}

Sample Output:


Stack Items

[]

Stack Size :0

Stack Push


Stack Items

[A, 20, Example]

Stack Size :3

Stack Pop


Pop Data :Example

Pop Data 20

Pop Data A

Stack Items

[]

Stack Size :0