Untitled

 avatar
unknown
plain_text
3 years ago
2.5 kB
9
Indexable
Solution: Here I have Implemented a class called Kstacks for stack creation, push and pop function for inserting and deletion of data from stack.


 import java.util.*;

/**
 * Implement k stacks in array
 * Time - O(1)
 * Space- O(n+k)
 */
 
public class KStacks {
    int[] arr;
    int[] top;
    int[] next;
    int free; int k, n;
    
    //k is the number of stacks and n is the length of the array
    KStacks(int k,int n){
        this.k=k;
        this.n=n;
        arr=new int[n];
        top=new int[k];
        next=new int[n];
        for(int i=0;i<k;i++){
            top[i]= -1;
        }
        for(int i=0;i<n-1;i++){
            next[i]= i+1;
        }
        next[n-1]=-1;
        free=0;
    }
    
    /*
    * the data item to be printed
    * kn - magazine number
    */
    void push(int data, int kn){
        if(isFull()){
            System.out.print("\nStack Overflow\n");
            return;
        }
        int i=free;
        free=next[i];
        next[i]=top[kn];
        top[kn]=i;
        
        arr[i]=data;
        System.out.print("\nPush element in stack\t "+kn+"\tdata\t "+data);
    }
    
    
    int pop(int kn){
        if(isEmpty(kn)){
            System.out.print("\nStack overflow\n");
            return Integer.MAX_VALUE;
        }
        int i=top[kn];
        top[kn]=next[i];
        next[i]=free;
        free=i;
        
        return arr[i];
    }
    
    boolean isFull(){
        return free==-1;
    }
    
    boolean isEmpty(int kn){
        return top[kn]==-1;
    }
    
public static void main(String args[]){
    // Let's create 4 stacks in an array of size 10
   
    Scanner sc= new Scanner(System.in); //System.in is the standard input stream
    System.out.print("Enter the number of stacks (so far I have implemented 4 push stack commands-");
     int k= sc.nextInt();
      int n = 10;
    KStacks ks=new KStacks(k, n);
  // Let's put some items on stack number 0
    ks.push(11, 0);
    ks.push(9, 0);
    ks.push(7, 0);
    ks.push(4,3);
    
    // Let's put some items in stack #1
    ks.push(17, 1);
    ks.push(49, 1);
    ks.push(39, 1);
    
    // Let's put some items in stack number 2
    ks.push(15, 2);
    ks.push(45, 2);
 
    
 
   
        // Let's put some items in stack number 4
    ks.push(5,3);
    ks.push(78,3);
 
    System.out.println(" Popped element from stack 2 is\t "+ks.pop(2));
    System.out.println("\t Popped element from stack 1 is \t"+ks.pop(1));
    System.out.println(" Popped element from stack 0 is\t"+ks.pop(0));
}
}
Editor is loading...