/*** Stack List Reverser
********************************************
This program inputs words, one after another,
until the user types "quit".
It PUSHES each word into a STACK.
Once the inputting is finished, the program
uses POP to retrieve one word at a time and display it.
As a result, the words are printed in reverse order.
The stack is stored in a Linked-List, consisting of
ListNode Objects created from the ListNode class.
********************************************************************/
public class Stack
{
class ListNode // This
inner class is used as a data structure
{
String data;
ListNode next;
}
ListNode stack = null;
public Stack()
{
String term = "";
while(!term.equals("quit"))
{
term = input("Next
term:");
push(term);
}
while(stack!=null)
{
term = pop();
output(term);
}
}
public String pop()
{
String answer = stack.data;
stack = stack.next;
return answer;
}
public void push(String s)
{
ListNode temp = new ListNode();
temp.data = s;
temp.next = stack;
stack = temp;
}
public static void main(String[] args)
{ new Stack(); }
public void output(String message)
{
javax.swing.JOptionPane.showMessageDialog(null,message); }
public String input(String prompt)
{ return
javax.swing.JOptionPane.showInputDialog(null,prompt); }
}
/***********************************************************\
(1) Run the program, type in several words,
finish by typing "quit", and check that
the program prints the words in reverse order.
(2) Change the program so that it does not save "quit"
in the list.
(3) Change the program so that it stores a NAME and EMAIL
in each ListNode. When it prints the list
backward,
it should display the NAME and EMAIL for each
person.
(4) Add a second stack called BUSINESS. Each time you input
a name and email, the program checks whether the
name
starts with "MR" or "MRS" or "MS". If so,
it saves the
name and email in the BUSINESS stack.
Otherwise, it
saves them in the normal stack. At the end,
it prints
both lists in reverse order.
\***********************************************************/