LinkQueue by java

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

posted on 2012-09-02 18:54  robin.he  阅读(339)  评论(0)    收藏  举报

导航