StackQueueDemo.java.html

  1  import java.util.LinkedList;
  2  import java.util.PriorityQueue;
  3  import java.util.Queue;
  4  import java.util.Stack;
  5  
  6  /**
  7     This program demonstrates stacks, queues, and priority queues.
  8  */
  9  public class StackQueueDemo
 10  {
 11     public static void main(String[] args)
 12     {
 13        System.out.println("Stack: ");
 14        Stack<String> s = new Stack<String>();
 15        s.push("Tom");
 16        s.push("Diana");
 17        s.push("Harry");
 18        while (s.size() > 0) { System.out.println(s.pop()); }
 19  
 20        System.out.println("Queue: ");
 21        Queue<String> q = new LinkedList<String>();
 22        q.add("Tom");
 23        q.add("Diana");
 24        q.add("Harry");
 25        while (q.size() > 0) { System.out.println(q.remove()); }
 26  
 27        System.out.println("Priority Queue: ");
 28        PriorityQueue<String> pq = new PriorityQueue<String>();
 29        pq.add("Tom");
 30        pq.add("Diana");
 31        pq.add("Harry");
 32        while (pq.size() > 0) { System.out.println(pq.remove()); }
 33     }
 34  }