Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using the interface below create a simple stack class. Use a node based structure to implement the stack USE YOU STACK TO SOLVE THE STOCK
Using the interface below create a simple stack class.
Use a node based structure to implement the stack
USE YOU STACK TO SOLVE THE STOCK SPAN
PROBLEM
The stock span problem
is a financial problem
where we have a series
of daily price quotes
for a stock and we need
to calculate span of
stock's price for all
days.
public interface StackInterface
Insert a new item into the stack.
@param item the item to insert.
public void pushItem item;
Remove the most recently inserted item from the stack.
public void pop;
Get the most recently inserted item in the stack. Does not alter the stack.
public Item top;
Return and remove the most recently inserted item from the stack.
@return the most recently inserted item in the stack.
Item topAndPop;
Test if the stack is logically empty.
@return true if empty, false otherwise.
public boolean isEmpty;
Make the stack logically empty.
public void makeEmpty;
Return the size of the stack.
public int size;
import java.util.ArrayList;
public class StockSpan
private ArrayList prices new ArrayList;
private ArrayList spans new ArrayList;
public StockSpan
super;
Fill the spans arrayList with the span for the day or the same index in prices.
###### Use Brute Force ######
void calculateSpansBruteForce
Fill the spans arrayList with the span for the day or the same index in prices.
###### Use better algorithm Stack based algorithm?! ######
private void calculateSpans
public void addPricesint price
prices.addprice;
public void print
calculateSpansBruteForce;
for int i ; i spans.size; i
System.out.printf D:d S:d i spans.geti;
System.out.println;
public int getSpanint day
calculateSpansBruteForce;
ifday spans.size
return ;
return spans.getday;
public class Main
public static void mainString args
int prices;
StockSpan stockSpan new StockSpan;
forint i ; i prices.length; i
stockSpan.addPricespricesi;
stockSpan.print;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started