Question: ********Both problems are related*************** ********Java Code Required************** Java Problem 1: Java: underlying implementation of Javas ArrayList. It uses a regular Java array to store data

********Both problems are related*************** ********Java Code Required**************

Java Problem 1:

Java: underlying implementation of Javas ArrayList. It uses a regular Java array to store data and resizes to a larger array once it is full. Implement a stack in a class called MyStack that uses an array to store data and resizes the array when necessary. All methods should run in constant time, except when the array must be resized (this is still amortized constant time). NOTE: Your class must implement MyStackInterface for full credit. You are not allowed to use any List object as your instance variable. You are only allowed to use arrays as your data retaining instance variable. Your MyStack must be generic as well. Note: For this problem use the following Interface:-

public interface MyStackInterface { public void push(T x);//insert or push into the stack public T pop(); // Pop public T peek(); // Top public boolean isEmpty();// to check if the array is empty public int size();// returns how many items are im the stack }

Problem 2: in Java Build a queue out of two completely separate stacks, S1 and S2. Enqueue operations happen by pushing the data on to stack 1. Dequeue operations are completed with a pop from stack 2. Obviously you will have to find some way to get the input stack information over to the output stack. Your job is to figure out how and when to do that, using only push and pop operations. Write a class TwoStackQueue that provides the Queue Abstrackt Data Type (by implementing the MyQueueInterface) using two stacks. Your class should explicitly implement the interface provided above. Since the interface is generic, your class should be as well. Provide all methods specified in the interface. Your class should not use any additional memory to store the data in the queue except for the two stacks. For your stacks, use the MyStack class that you implemented in the first problem. All methods must run in constant time, except for one, which is allowed to run in linear time occassionally. Note: For Problem 2 use this Interface:-

public interface TwoStackQueueInterface {

public void enqueue(T x); public T dequeue(); public int size(); public boolean isEmpty(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!