import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; /* * * Simple program demonstrating the use of a stack
Question:
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
/*
*
* Simple program demonstrating the use of a stack for undo operations
*/
public class StackUndo
{
static LinkedList list = new LinkedList();
static Stack actions = new Stack();
public static void main(String [] args)
{
// Build a list of words by performing a sequence of actions, where each action has a word parameter
String commands = "addLast cat addLast bear addLast porcupine addFirst lion addFirst eagle";
String action = "";
String word = "";
Scanner scanner = new Scanner(commands);
while (scanner.hasNext())
{
action = scanner.next();
if (action.equals("addLast"))
{
word = scanner.next();
list.addLast(word);
actions.push(action);
}
if (action.equals("addFirst"))
{
word = scanner.next();
list.addFirst(word);
actions.push(action);
}
System.out.println(list);
}
System.out.println("Undo");
undo();
System.out.println(list);
System.out.println("Undo");
undo();
System.out.println(list);
System.out.println("Undo");
undo();
System.out.println(list);
System.out.println("Undo");
undo();
System.out.println(list);
System.out.println("Undo");
undo();
System.out.println(list);
undo();
System.out.println("Expected:Undo[lion, cat, bear, porcupine]Undo[cat, bear, porcupine]Undo[cat, bear]");
System.out.println("Undo[cat]Undo[]");
}
/*
* Undo the last action performed on a list. Possible actions are "addFirst" and "addLast"
*/
static void undo()
{
//-----------Start below here. To do: approximate lines of code = 6
// Use the actions stack to undo the last action applied to the list of words
//an action is either "addFirst" or "addLast" Hint: check to ensure the stack is not empty
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
P.S: Make sure the output is same as expected with well formatted code
Accounting Principles
ISBN: 978-0470533475
9th Edition
Authors: Jerry J. Weygandt, Paul D. Kimmel, Donald E. Kieso