Question

Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the pus

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Assuming the language is Java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// DoubleStack.java

public class DoubleStack {

    private Object[] array;

    // defining two integer variables to denote top indices of each stack (next

    // available slots for new elements)

    private int stack1top;

    private int stack2top;

    // constructor

    public DoubleStack(int n) {

        // creating an Object array of n elements

        array = new Object[n];

        // initializing first index as stack1top and last index as stack2top

        stack1top = 0;

        stack2top = array.length - 1;

    }

    // method to push an element to first or second stack. if i=0, first stack,

    // if i=1, second stack

    public boolean push(int i, Object o) {

        // if stack1top exceeds stack2top, that is when the whole array is full

        if (stack1top > stack2top) {

             return false; // overflow

        }

        // checking if i is 0 or 1

        if (i == 0) {

             // adding to array at stack1top and incrementing stack1top

             array[stack1top] = o;

             stack1top++;

        } else if (i == 1) {

             // adding to array at stack2top and decrementing stack2top

             array[stack2top] = o;

             stack2top--;

        }

        return false; // invalid i

    }

    // removes and returns top element of a given stack

    Object pop(int i) {

        if (i == 0) {

             // if stack1top>0, it means that stack1 is not empty

             if (stack1top > 0) {

                 // decrementing stack1top

                 stack1top--;

                 // returning element at stack1top (previously stack1top-1)

                 return array[stack1top];

             }

        } else if (i == 1) {

             // if stack2top < array.length-1, it means that stack2 is not empty

             if (stack2top < array.length - 1) {

                 // incrementing stack2top

                 stack2top++;

                 // returning element at stack2top (previously stack2top+1)

                 return array[stack2top];

             }

        }

        return null; // stack is empty or i is invalid

    }

    // code for testing, remove if you dont want.

    public static void main(String[] args) {

        // creating a DoubleStack of size 50

        DoubleStack doubleStack = new DoubleStack(50);

        // adding numbers from 1 to 30 to stack1

        for (int i = 1; i <= 30; i++) {

             doubleStack.push(0, i);

        }

        // adding numbers from 1 to 25 to stack2, after 20 the doublestack will

        // be full, so elements from 21 to 25 will not be pushed to stack2

        for (int i = 1; i <= 25; i++) {

             doubleStack.push(1, i);

        }

        // displaying stack 1 element

        System.out.println("Stack 1:");

        // popping first element from first stack, if exists

        Object ob = doubleStack.pop(0); //note the index 0

        //looping until ob is null, printing ob, popping next element

        while (ob != null) {

             System.out.println(ob);

             ob = doubleStack.pop(0);

        }

        //doing the same for stack 2

        System.out.println("Stack 2:");

        ob = doubleStack.pop(1); //note the index 1

        while (ob != null) {

             System.out.println(ob);

             ob = doubleStack.pop(1);

        }

    }

}

/*OUTPUT*/

Stack 1:

30

29

28

27

26

25

24

23

22

21

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

Stack 2:

20

19

18

17

16

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

Add a comment
Know the answer?
Add Answer to:
Define a class DoubleStack which implements two stacks of objects of type Object using a single...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create...

    I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b). A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT