Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Simulator / DOVEBrowser / Queue.java
blobffa15a882a15d3494aa5d9be19c9ac130ea3f06f
2 import java.util.Enumeration;
4 public class Queue
6 // Friendly?
7 Queue_Node head_ = null;
8 Queue_Node tail_ = null;
10 private int length_ = 0;
12 public Queue()
16 public Enumeration forward_iterator()
18 return new Queue_Iterator(this, Queue_Iterator.FORWARD);
21 public Enumeration reverse_iterator()
23 return new Queue_Iterator(this, Queue_Iterator.REVERSE);
26 public void enqueue_tail(Object new_data)
28 Queue_Node new_node = new Queue_Node(new_data);
30 if (tail_ == null)
32 tail_ = new_node;
33 head_ = new_node;
35 else
37 new_node.prev_ = tail_;
38 tail_.next_ = new_node;
39 tail_ = new_node;
42 length_++;
45 public void enqueue_head(Object new_data)
47 Queue_Node new_node = new Queue_Node(new_data);
49 if (head_ == null)
51 tail_ = new_node;
52 head_ = new_node;
54 else
56 new_node.next_ = head_;
57 head_.prev_ = new_node;
58 head_ = new_node;
61 length_++;
64 public Object dequeue_head()
66 Object return_value = null;
68 if (head_ == null)
69 return_value = null;
70 else if (tail_ == head_)
72 return_value = head_.data_;
73 tail_ = null;
74 head_ = null;
76 else
78 return_value = head_.data_;
79 head_ = head_.next_;
80 head_.prev_ = null;
83 length_--;
85 return return_value;
88 public Object dequeue_tail()
90 Object return_value = null;
92 if (tail_ == null)
93 return_value = null;
94 else if (tail_ == head_)
96 return_value = tail_.data_;
97 tail_ = null;
98 head_ = null;
100 else
102 return_value = tail_.data_;
103 tail_ = tail_.prev_;
104 tail_.next_ = null;
107 length_--;
109 return return_value;
112 public int length()
114 return length_;
117 public Object head()
119 if (head_ != null)
120 return head_.data_;
121 else
122 return null;
125 public Object tail()
127 if (tail_ != null)
128 return tail_.data_;
129 else
130 return null;
134 class Queue_Node
136 public Queue_Node prev_ = null;
137 public Queue_Node next_ = null;
138 public Object data_;
140 public Queue_Node(Object data)
142 data_ = data;
146 class Queue_Iterator implements Enumeration
148 public static final boolean FORWARD = true;
149 public static final boolean REVERSE = false;
151 private Queue queue_;
152 private Queue_Node queue_ptr_;
153 private boolean direction_ = FORWARD;
155 public Queue_Iterator(Queue queue)
157 queue_ = queue;
158 queue_ptr_ = queue.head_;
161 public Queue_Iterator(Queue queue, boolean direction)
163 queue_ = queue;
164 direction_ = direction;
166 if (direction_)
167 queue_ptr_ = queue_.head_;
168 else
169 queue_ptr_ = queue_.tail_;
172 public Object nextElement()
174 Object data = queue_ptr_.data_;
176 if (direction_)
177 queue_ptr_ = queue_ptr_.next_;
178 else
179 queue_ptr_ = queue_ptr_.prev_;
181 return data;
184 public boolean hasMoreElements()
186 return queue_ptr_ != null;