SquenceQueue by java

 1 class SQueue<Type> {
 2     int front;
 3     int rear;
 4     int size;
 5     Type[] data;
 6     
 7     public SQueue(Type data[]) {
 8         // TODO Auto-generated constructor stub
 9         this.data = data;
10         front = 0;
11         rear = 0;
12         size = data.length + 1;
13     }
14     
15     private boolean IsEmpty() {
16         return front == rear;
17     }
18     
19     private boolean IsFull() {
20         return (rear + 1) % size == front;
21     }
22     
23     public boolean EnSQueu(Type d) {
24         if (IsFull())
25             return false;
26         
27         data[rear] = d;
28         rear = (rear + 1) % size;
29         
30         return true;
31     }
32     
33     public Type DeSQueue() {
34         if (IsEmpty())
35             return null;
36         
37         Type tmp = data[front];
38         front = (front + 1) % size;
39         
40         return tmp;
41     }
42     
43     public int GetLength() {
44         return (rear - front + size) % size;
45     }
46 }
47 
48 public class SqueueQueue {
49     public static void main(String[] args) {
50         String data[] = {"hello", " ", "world"};
51         String d;
52         SQueue<String> q = new SQueue<String>(data);
53 
54         for (String string : data) {
55             q.EnSQueu(string);
56         }
57         
58         System.out.println("length = " + q.GetLength());
59         
60         while ((d = q.DeSQueue()) != null) {
61             System.out.print(d + " ");
62         }
63     }
64 }

posted on 2012-09-02 11:46  robin.he  阅读(149)  评论(0)    收藏  举报

导航