2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/Stacks.java
|
2020-10-07 13:35:40 -06:00
|
|
|
// (c)2020 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Demonstration of Stack Class
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
2017-01-22 16:48:11 -08:00
|
|
|
enum Month { JANUARY, FEBRUARY, MARCH, APRIL,
|
|
|
|
MAY, JUNE, JULY, AUGUST, SEPTEMBER,
|
|
|
|
OCTOBER, NOVEMBER }
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class Stacks {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Stack<String> stack = new Stack<>();
|
|
|
|
for(Month m : Month.values())
|
|
|
|
stack.push(m.toString());
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("stack = " + stack);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Treating a stack as a Vector:
|
|
|
|
stack.addElement("The last line");
|
2016-01-25 18:05:55 -08:00
|
|
|
System.out.println(
|
|
|
|
"element 5 = " + stack.elementAt(5));
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("popping elements:");
|
2015-06-15 17:47:35 -07:00
|
|
|
while(!stack.empty())
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.print(stack.pop() + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
// Using a LinkedList as a Stack:
|
|
|
|
LinkedList<String> lstack = new LinkedList<>();
|
|
|
|
for(Month m : Month.values())
|
|
|
|
lstack.addFirst(m.toString());
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("lstack = " + lstack);
|
2015-06-15 17:47:35 -07:00
|
|
|
while(!lstack.isEmpty())
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.print(lstack.removeFirst() + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
// Using the Stack class from
|
2015-12-15 11:47:04 -08:00
|
|
|
// the Collections Chapter:
|
2015-11-11 20:20:04 -08:00
|
|
|
onjava.Stack<String> stack2 =
|
|
|
|
new onjava.Stack<>();
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Month m : Month.values())
|
|
|
|
stack2.push(m.toString());
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("stack2 = " + stack2);
|
2016-07-05 14:46:09 -06:00
|
|
|
while(!stack2.isEmpty())
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.print(stack2.pop() + " ");
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
stack = [JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
|
|
|
|
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER]
|
2015-06-15 17:47:35 -07:00
|
|
|
element 5 = JUNE
|
|
|
|
popping elements:
|
2017-05-10 11:45:39 -06:00
|
|
|
The last line NOVEMBER OCTOBER SEPTEMBER AUGUST JULY
|
|
|
|
JUNE MAY APRIL MARCH FEBRUARY JANUARY lstack =
|
|
|
|
[NOVEMBER, OCTOBER, SEPTEMBER, AUGUST, JULY, JUNE, MAY,
|
|
|
|
APRIL, MARCH, FEBRUARY, JANUARY]
|
|
|
|
NOVEMBER OCTOBER SEPTEMBER AUGUST JULY JUNE MAY APRIL
|
|
|
|
MARCH FEBRUARY JANUARY stack2 = [NOVEMBER, OCTOBER,
|
|
|
|
SEPTEMBER, AUGUST, JULY, JUNE, MAY, APRIL, MARCH,
|
2015-06-15 17:47:35 -07:00
|
|
|
FEBRUARY, JANUARY]
|
2017-05-10 11:45:39 -06:00
|
|
|
NOVEMBER OCTOBER SEPTEMBER AUGUST JULY JUNE MAY APRIL
|
|
|
|
MARCH FEBRUARY JANUARY
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|