Web browsers commonly allow you to navigate through a \"history\" of web pages t
ID: 3907816 • Letter: W
Question
Web browsers commonly allow you to navigate through a "history" of web pages that have previously been visited. The mechanism is somewhat like a stack, in that the most recently visited pages are at the top of the history and revisited when the "back" button is pressed.
However, the history does not necessarily have unbounded capacity. In reality, there may exist a fixed limit on the size of the history. The issue arises as to what should happen when the capacity is exhausted and a new item is pushed onto the stack. One possibility is to throw an exception. But this is not how a Web browser behaves. If it only has room to save 50 pages in its history and yet you visit more pages, it will make room in the history for a new page by throwing away the page that is on the very bottom of the history (i.e., the least recently visited page). The formal Stackinterface does not help, as it gives us no way to directly access or remove the object on the bottom of the stack.
Your Task:
Implement the following MyLeakyStack<E> class
Using Array and using Singly Linked List
import net.datastructures.*;
public class MyLeakyStack<E> implements Stack<E> {
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void push(E e) {
// TODO Auto-generated method stub
}
@Override
public E top() {
// TODO Auto-generated method stub
return null;
}
@Override
public E pop() {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
import net.datastructures.*; public class MyLeakyStack implements Stack { private Object[] arr; private int count; public MyLeakyStack() { arr = new Object[100]; count = 0; } @Override public int size() { return count; } @Override public boolean isEmpty() { return count == 0; } @Override public void push(E e) { if(count == arr.length) { Object[] copy = new Object[2 * count]; for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.