Question

In: Computer Science

Show how to implement three stacks in one array (in Java)

Show how to implement three stacks in one array (in Java)

Solutions

Expert Solution

// Java program to demonstrate the implementation of k stacks in a single array  

public class Stac
{
   // A Java class to represent k stacks in a single array of size n
   static class KStack
   {
       int arr[]; // Array of size n to store actual content to be stored in stacks
       int top[]; // Array of size k to store indexes of top elements of stacks
       int next[]; // Array of size n to store next entry in all stacks
                   // and free list
       int n, k;
       int free; // To store beginning index of free list

       //constructor to create k stacks in an array  
       KStack(int k1, int n1)
       {
           // Initialize n and k, and allocate memory for all arrays
           k = k1;
           n = n1;
           arr = new int[n];
           top = new int[k];
           next = new int[n];

           // Initialize all stacks as empty
           for (int i = 0; i < k; i++)
               top[i] = -1;

           // Initialize all spaces as free
           free = 0;
           for (int i = 0; i < n - 1; i++)
               next[i] = i + 1;
           next[n - 1] = -1; // -1 is used to indicate end of free list
       }

       // A utility function to check if there is space available
       boolean isFull()
       {
           return (free == -1);
       }

       // To push an item in stack number 'sn' where sn is from 0 to k-1
       void push(int item, int sn)
       {
           // Overflow condition
           if (isFull())
           {
               System.out.println("Stack Overflow");
               return;
           }

           int i = free; // i Stores the index of first free slot

           // Update index of free slot to index of next slot in free list
           free = next[i];

           // Update next of top and then top for stack number 'sn'
           next[i] = top[sn];
           top[sn] = i;

           // Put the item in array
           arr[i] = item;
       }


       int pop(int sn)
       {
           if (isEmpty(sn))
           {
               System.out.println("Stack Underflow");
               return Integer.MAX_VALUE;
           }

           // Find index of top item in stack number 'sn'
           int i = top[sn];

           top[sn] = next[i]; // Change top to store next of previous top

           // Attach the previous top to the beginning of free list
           next[i] = free;
           free = i;

           // Return the previous top item
           return arr[i];
       }

       // check if stack number 'sn' is empty or not
       boolean isEmpty(int sn)
       {
           return (top[sn] == -1);
       }

   }

   //main method
   public static void main(String[] args)
   {
       // create 3 stacks in an array of size 10
       int k = 3, n = 10; //k denotes the total number of array it can vary from 1 to n
      
       KStack ks = new KStack(k, n);

       ks.push(15, 2);
       ks.push(45, 2);

       // Let us put some items in stack number 1
       ks.push(17, 1);
       ks.push(49, 1);
       ks.push(39, 1);

       // put some items in stack number 0
       ks.push(11, 0);
       ks.push(9, 0);
       ks.push(7, 0);

       System.out.println("element popped from stack 2 is " + ks.pop(2));
       System.out.println("element popped from stack 1 is " + ks.pop(1));
       System.out.println("element popped from stack 0 is " + ks.pop(0));
   }
}

Hope the code is clear to you,try to implement it in your system


Related Solutions

Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Java language Exercise #2: Design a Lotto class with one array instance variable to hold three...
Java language Exercise #2: Design a Lotto class with one array instance variable to hold three random integer values (from 1 to 9). Include a constructor that randomly populates the array for a lotto object. Also, include a method in the class to return the array. Use this class in the driver class (LottoTest.java) to simulate a simple lotto game in which the user chooses a number between 3 and 27. The user runs the lotto up to 5 times...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT