Stack Using Array - Datastructure - JAVA

Program:
import java.io.*;
class stackarray
{   
    private int maxsize;
    private int st[];
    public int top;
    public stackarray(int size)
    {
        maxsize=size;
        st=new int[maxsize];
        top=-1;
    }

    public boolean isempty()
    {
        return(top==-1);
    }
    public boolean isfull()
    {
        return(top==(maxsize-1));
    }
    public void push(int data)
    {
        if(isfull())
            System.out.println("Error:stack overflow");
        else
            {
                st[++top]=data;
                System.out.println("\tpushing to stack.... "+data);
            }

    }
    public int pop()
    {
        if(isempty())
        {
            System.out.print("\nError:empty");
            return 0;
        }
        else
        {
            return st[top--];
        }
    }
    public void display()
    {
        System.out.println("\n--STACK ITEMS-- ");
        for(int i=0;i<=top;i++)       
            System.out.print(st[i]+" ");
    }
}
class stack_array
{
public static void main(String arg[])throws IOException
{
    System.out.print("\n\n\t\t<<<STACK USING ARRAY>>>\nEnter the size of array:");
    DataInputStream x= new DataInputStream(System.in);
    stackarray ar1=new stackarray(Integer.parseInt(x.readLine()));
    int proceed=1;
    System.out.print("Enter the number of elements to push:");
    int p=Integer.parseInt(x.readLine());   
    System.out.println("Enter the elements:");
    for(int k=0;k<p;k++)
        ar1.push(Integer.parseInt(x.readLine()));
    ar1.display();
    System.out.print("\nEnter the number of elements to pop:");
    int q=Integer.parseInt(x.readLine());   
    for(int k=0;k<q;k++)
        System.out.println("\nPoping.... "+ar1.pop());
   
    do{
        ar1.display();
        System.out.println("\n---MENU---\n1.Push an element to stack.\n2.Pop an element from stack\n3.Check Full\n4.Check Empty\n5.Display Stack Items \n");
        int s1=Integer.parseInt(x.readLine());   
        switch(s1)
        {
            case 1:
                System.out.print("---PUSH AN ELEMENT---\nEnter the element:");
                ar1.push(Integer.parseInt(x.readLine()));
                break;
            case 2:
                System.out.print("---POP AN ELEMENT---\nPoping.... "+ar1.pop());
                break;
            case 3:
                if(ar1.isfull())
                    System.out.println("Stack is full.");
                else
                    System.out.println("Stack is not full.");
                break;
            case 4:
                if(ar1.isempty())
                    System.out.println("Stack is empty.");
                else
                    System.out.println("Stack is not empty.");
            case 5:
                    ar1.display();
            default:
                break;
        }
        System.out.println("\nPress 1 to continue...");
        proceed=Integer.parseInt(x.readLine());   
    }while(proceed==1);


}   
}

Output:
nn@linuxmint ~ $ javac stack_array.java
Note: stack_array.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_array


        <<<STACK USING ARRAY>>>
Enter the size of array:3
Enter the number of elements to push:3
Enter the elements:
1
    pushing to stack.... 1
2
    pushing to stack.... 2
3
    pushing to stack.... 3

--STACK ITEMS--
1 2 3
Enter the number of elements to pop:1

Poping.... 3

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

1
---PUSH AN ELEMENT---
Enter the element:4
    pushing to stack.... 4

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

3
Stack is full.

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

2
---POP AN ELEMENT---
Poping.... 4
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

4
Stack is not empty.

--STACK ITEMS--
1 2
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

5

--STACK ITEMS--
1 2
Press 1 to continue...
2
nn@linuxmint ~ $

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...