Stacks are often used to provide “undo” support in applications like a Web brows
ID: 3726710 • Letter: S
Question
Stacks are often used to provide “undo” support in applications like a Web browser or text editor. While support for undo can be implemented with an unbounded stack, many applications provide only limited support for such an undo history, with a fixed-capacity stack. When push is invoked with the stack at full capacity, rather than throwing an exception, a more typical semantic is to accept the pushed element at the top while “leaking” the oldest element from the bottom of the stack to make room. Give an implementation of such a LeakyStack abstraction, using an array. Input Format: Read input from a file “in2.txt”. The first line contains T number of test cases. The first line of each test case is the capacity of the LeakyStack. Second line contains the consecutive operation strings in the browser. Output Format: Write output in console. Print the content of the stack.
Input:
1
3
op_one op_two op_three op_four op_five
Output:
op_five op_four op_three
Here is my code. what's wrong with it
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class LeakyStack {
public static void main(String[] arg) throws IOException{
String currentLine;
BufferedReader br= new BufferedReader(new FileReader("C:\Users\chenxing he\Desktop\in2.txt"));
int caseNum=0;
int capacity=0;
int index=0;
String[] temp;
while((currentLine=br.readLine())!=null){
if(index==0){
caseNum=Integer.valueOf(currentLine);
}else if(index%2==1){
capacity=Integer.valueOf(currentLine);
}else{
temp=currentLine.split(" ");
System.out.println("case: "+ Integer.toString(caseNum));
outputOperation(temp,capacity);
}
index++;
}
}
public static void outputOperation(String[] a, int capacity){
StackOfArray output=new StackOfArray(capacity);
int n=0;while(n<a.length){
if(n<capacity){
output.push(a[n++]);
}else{
StackOfArray output1=new StackOfArray(capacity);
output1.push(output.pop());
output1.pop();
output.push(output1.pop());
output.push(a[n]);
}
}
for(int i=0;i<a.length;i++){
output.push(a[i]);
}
while(!output.isEmpty()){
System.out.println(output.pop()+" ");
}
}
}
Explanation / Answer
There are a few things missing in your code:
1. There was no import java.util.Stack;.
So I added
2. there was StackOfArray(capacity instead of just tackOfArray() So i changed it
3. To differentiate the code a bit I have replaced Stackof Array with just Stack.
This ocde works for me:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class LeakyStack {
public static void main(String[] arg) throws IOException{
String currentLine;
BufferedReader br= new BufferedReader(new FileReader("C:\Program Files\Java\jdk1.8.0_161
in\in2.txt"));
int caseNum=0;
int capacity=0;
int index=0;
String[] temp;
while((currentLine=br.readLine())!=null){
if(index==0){
caseNum=Integer.valueOf(currentLine);
}
else if(index%2==1){
capacity=Integer.valueOf(currentLine);
}
else {
temp=currentLine.split(" ");
System.out.println("case: "+ Integer.toString(caseNum));
outputOperation(temp,capacity);
}
index++;
}
}
public static void outputOperation(String[] a, int capacity)
{
Stack output = new Stack();
int n=0;while(n<a.length){
if(n<capacity){
output.push(a[n++]);
} else{
Stack output1=new Stack();
output1.push(output.pop());
output1.pop();
output.push(output1.pop());
output.push(a[n]);
}
}
for(int i=0;i<a.length;i++){
output.push(a[i]);
}
while(!output.isEmpty()){
System.out.println(output.pop()+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.