Write a program that uses a stack to reverse its inputs. Your stack must be gene
ID: 3832493 • Letter: W
Question
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: • push, • pop, • isEmpty (returns true if the stack is empty and false otherwise), and • size (returns an integer value for the number of items in the stack).
You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must throw an IndexOutOfBoundsException if the user attempts to “pop” something off an empty stack. Name your stack class “Stack” and put it in a file named Stack.java.
Create a StackTest.java file to demonstrate all of the methods of your stack. Instantiate two stacks; one for String and one for Integer. Push words/integers onto your stack. After you have pushed a series of words/integers on your stack use a while loop to pop and print each item. If your stack is working properly your inputs should print in reverse order. Be sure that you calls to pop are in a try statement and will catch the IndexOutOfBoundsException, if thrown. Note: Java has a Stack class, but you may not use it. You must create your own Stack class and your class must have the following operations (all of which must be demonstrated): push, pop, isEmpty, and size. Here is an example screen shot of a working stack that reverses words and integers:
Explanation / Answer
public interface Stack <Item>
{
Item pop(); // return the top item and removes it from stack
void push(Item item); // adds an item to the stack
boolean isEmpty(); // returns true if stack is empty, false otherwise
int size(); // returns the number of items in stack right now
}
public class LinkedStack<Item> implements Iterable<Item>,Stack <Item> {
private int n; // size of the stack
private Node first; // top of stack
// helper linked list class
private class Node {
private Item item;
private Node next;
}
public LinkedStack() {
first = null;
n = 0;
assert check();
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return n;
}
public void push(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
n++;
assert check();
}
public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = first.item; // save item to return
first = first.next; // delete first node
n--;
assert check();
return item; // return the saved item
}
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
public Iterator<Item> iterator() {
return new ListIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() { return current != null; }
public void remove() { throw new IndexOutOfBoundException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
// check internal invariants
private boolean check() {
// check a few properties of instance variable 'first'
if (n < 0) {
return false;
}
if (n == 0) {
if (first != null) return false;
}
else if (n == 1) {
if (first == null) return false;
if (first.next != null) return false;
}
else {
if (first == null) return false;
if (first.next == null) return false;
}
// check internal consistency of instance variable n
int numberOfNodes = 0;
for (Node x = first; x != null && numberOfNodes <= n; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != n) return false;
return true;
}
}
public calss StackTest{
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-"))
stack.push(item);
else if (!stack.isEmpty())
System.out.println(stack.pop() + " ");
}
System.out.println("(" + stack.size() + " left on stack)");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.