Advertisement
pexea12

Khanh Queue

Oct 1st, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. public class Queue {
  2.     private String[] s = new String[1000];
  3.     private int first = 0, last = -1;
  4.     public boolean isEmpty(){
  5.         return (first > last) ;
  6.     }
  7.     public void enqueue(String item){
  8.         if (last == 1000) {
  9.             System.out.println("Stackoverflow");
  10.             return;
  11.         }
  12.         s[++last] = item;
  13.     }
  14.     public String dequeue(){
  15.         if (isEmpty()) {
  16.             System.out.println("Empty Stack");
  17.             return null;
  18.         }
  19.         return s[first++];
  20.     }
  21.     public int size() {
  22.         if (isEmpty()) return 0;
  23.         return last - first + 1;
  24.     }
  25.    
  26.     public static void main(String[] args){
  27.         System.out.println("Chao cac ban");
  28.         Queue q = new Queue();
  29.         q.enqueue("chi");
  30.         q.enqueue("anh");
  31.         q.enqueue("moi");
  32.         q.enqueue("biet");
  33.         q.enqueue("chi");
  34.         q.enqueue("anh");
  35.         q.enqueue("moi");
  36.         q.enqueue("hieu");
  37.         q.enqueue("duoc");
  38.         q.enqueue("em");
  39.        
  40.         while (!q.isEmpty()) {
  41.             System.out.println("size = " + q.size());;
  42.             System.out.println(q.dequeue());
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement