Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author jiyou_000
  10.  */
  11. import java.util.Stack;
  12.  
  13. public class Queue <T>
  14. {
  15.     public Stack<T> s;
  16.    
  17.     public Queue()
  18.     {
  19.         s = new Stack<>();
  20.     }
  21.    
  22.     public boolean empty()
  23.     {
  24.         return s.empty();
  25.     }
  26.    
  27.     public void enqueue(T a)
  28.     {
  29.         if (s.empty())
  30.         {
  31.             s.push(a);
  32.         }
  33.         else
  34.         {
  35.             Stack<T> tmp = new Stack<>();
  36.             while (!s.empty())
  37.             {
  38.                 tmp.push(s.pop());
  39.             }
  40.            
  41.             s.push(a);
  42.            
  43.             while (!tmp.empty())
  44.             {
  45.                 s.push(tmp.pop());
  46.             }
  47.         }
  48.     }
  49.    
  50.     public void dequeue() throws Exception
  51.     {
  52.         if (empty())
  53.         {
  54.             throw new Exception();
  55.         }
  56.         else
  57.         {
  58.             s.pop();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement