You are on page 1of 10

ADS LAB-4

1.)

class stack
{
    static int cap=10;
    int arr[]= new int[cap]; 
    int top=-1;
    stack()
    {   
        int size=cap;
    }
    void push(int val)
    {
        if(top>=cap-1)
            System.out.print("Stack Overflow");
        else{
            arr[++top]=val;
            System.out.println(val+ " is inserted to stack");
        }
    }
    int pop()
    {
        if(top==-1){
            System.out.print("Stack Underflow");
            return 0;
        }
        else{
            int val = arr[top--];
            return val;
        }           
    }
    int peek()
    {
        if(top==-1)
        {
            System.out.print("Underflow");
            return 0;
        }
        else{
            int val = arr[top];
            return val;
        }               
    }
    int size()
    {
        return cap;
    }
    boolean isEmpty()

202112036 1
    {
        if(top<0)
            return true;
        else
            return false;
    }
    boolean isFull()
    {
        if(top >= size()-1)
            return true;
        else
            return false;
    } 
}
class q4_1
{
    public static void main(String args[])
    {
        stack s = new stack();
        s.push(10);
        s.push(20);
        s.push(30);
        s.push(40);
        System.out.println(s.pop()+" is poped out");    
        System.out.println("Top element : "+s.peek());  
    }
}

2)

class stack
{
    int size;
    int top=-1;
    stack(int n)
    {
        size=n;

202112036 2
    }
    char a[]=new char[100];
    
    int push(char x)
    {
        if(top>=size-1)
        {
            System.out.println("Overflow");
        }
        else
        {
            a[++top]=x;
        }
        return 0;
    }
    char pop()
    {
        char tmp=' ';       
        if(top<0)
            System.out.println("Underflow");
        else{
            tmp=a[top--];
        }
        return tmp;
    }
    String Reverse(String str)
    {
        for(int i=0;i<size;i++)
        {
            push(str.charAt(i));
        }
        String tmp = "";
        for(int i=0;i<size;i++)
        {
            tmp+=pop();
        }
        return tmp;
    }
}
class q4_2
{
    public static void main(String args[])
    {
        String str="Gujarat";
        int len=str.length();
        stack s = new stack(len);   
        System.out.println("Input : "+  str);
        System.out.println("Output : "+ s.Reverse(str));
    }
}

202112036 3
3)

import java.util.Arrays;

public class q4_3 
{
    int size=4;
    int top=-1;
    int hold[]=new int[size];
    
    void push(int data)
    {
        hold[++top]=data;
    }

    void pop()
    {
        top--;
    }
    
    public static void main(String[] args) 
    {
        q4_3 inst = new q4_3();
        int arr[]=new int[]{1,5,7,25};
        int result[]=new int[arr.length];
        Arrays.fill(result, -1);
        int tmp=0;
        
        for(int i=0;i<arr.length;i++)
        {     
            if(inst.top==-1)
            {
                inst.push(arr[i]);
            }
            else
            {
                tmp=inst.top;
                if(inst.hold[tmp]<arr[i])
                {
                    while(tmp!=-1 && inst.hold[tmp]<arr[i])
                    {

202112036 4
                        inst.pop();
                        for(int n=0;n<arr.length;n++)
                        {
                            if(arr[n]==inst.hold[tmp])
                            {
                                result[n]=arr[i];
                            }
                        }
                       tmp--;
                    }
                    inst.push(arr[i]); 
                }
                else
                {
                    inst.push(arr[i]);
                }
         
            }
        }
        
        for(int j=0;j<arr.length;j++)
        {
            System.out.print(arr[j]+" ");
        }
        System.out.println();       
        
        for(int k=0;k<arr.length;k++)
        {
            System.out.print(result[k]+" ");
        }
    }    
}

4)

class stack
{
    int mx=100,top=-1;
    char arr[]=new char[mx];

202112036 5
    void push(char c)
    {
        if(top>=mx-1)
            System.out.print("Satck Overflow");
        else{
            arr[++top]=c;
        }
    }
    char pop()
    {
        char c=' ';
        if(top<0)
            System.out.print("Stack Underflow");
        else{
            c=arr[top--];       
        }
        return c;
    }
    char peek()
    {
        char c=' ';
        if(top<0)
        {
            System.out.print("Stack Underflow");
            return c;
        }
        else    
            return arr[top];
    }
    boolean isEmpty()
    {
        return ( top < 0 ? true : false );
    }
}
class q4_4
{
    static int precd(char ch)
    {
        switch(ch)
        {
            case '+': case '-':
                return 1;
            case '*': case '/':
                return 2;
            case '^':
                return 3;
        }
        return -1;  
    }
    static String inToPost(String str)
    {

202112036 6
        int n=str.length();
        String exp="";
        stack s = new stack();
        for(int i=0;i<n;i++)
        {
            char c=str.charAt(i);
            if(Character.isLetterOrDigit(c))
                exp+=c;
            else if(c=='(')
                s.push(c);
            else if(c==')')
            {
                while(!s.isEmpty() && s.peek() !='(')
                    exp+=s.pop();
                s.pop();
            }
            else
            {
                while(!s.isEmpty() && precd(c)<=precd(s.peek()))
                    exp+=s.pop();
                s.push(c);
            }
        }
        while(!s.isEmpty())
        {
            if(s.peek() == '(')
                        return "Invalid Expression";
            exp += s.pop();
        }
        return exp;
    }
    public static void main(String args[])
    {
        String str="a*b+c/d+e";
        System.out.println(inToPost(str));
    }
}

202112036 7
5)

import java.util.*;
class stack
{
        int size=10,top1=-1,top2=10;
        int arr[]=new int[10];
        void push(int stackno,int val)
        {
            if(top2-top1==1)
                System.out.println("Stack is Overflow");
            else
            {
                switch(stackno)
                {
                    case 1:
                        arr[++top1]=val;
                        break;
                    case 2:
                        arr[--top2]=val;
                        break;
                    default:
                        break;
                }
            }
        }
        int pop(int stackno)
        {
            switch(stackno)
            {
                case 1:
                    if(top1<0)
                    {   
                        System.out.println("Stack "+ stackno +" is Underflow");
                        return 0;
                    }
                    else{
                        int x=arr[top1];
                        arr[top1--]=0;
                        return x;
                    }
                case 2:
                    if(top2>9)
                    {   
                        System.out.println("Stack "+ stackno +" is Underflow");
                        return 0;
                    }
                    else{
                        int x=arr[top2];
                        arr[top2++]=0;

202112036 8
                        return x;
                    }
            }
            return 0;
        }
        void display()
        {
            for(int x : arr)
                System.out.print(x + " ");
            System.out.println();   
        }
}
class q4_5
{
    public static void main(String args[])
    {
        stack s =new stack();
        int ch=0,value;
        Scanner sc= new Scanner(System.in);
        do{
            System.out.println("1. Push in Stack 1");
            System.out.println("2. Push in Stack 2");
            System.out.println("3. Pop from Stack 1");
            System.out.println("4. Pop from Stack 2");
            System.out.println("5. Exit");
            System.out.print("Enter Choice : ");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    System.out.print("\nEnter Value : ");
                    value=sc.nextInt();
                    s.push(1,value);
                    s.display();
                    break;
                case 2:
                    System.out.print("\nEnter Value : ");
                    value=sc.nextInt();
                    s.push(2,value);
                    s.display();
                    break;
                case 3:
                    System.out.println(s.pop(1) + " popped out from Stack 1");
                    s.display();
                    break;
                case 4:
                    System.out.println(s.pop(2) + " popped out from Stack 2");
                    s.display();
                    break;
                case 5:
                    break;

202112036 9
                default:
                    break;
            }
        }while(ch!=5);
    }
}

202112036 10

You might also like