summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/DOVEBrowser/Queue.java
blob: 7ffaba15c52b844aa98c2fb2972db760426f27db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// $Id$


import java.util.Enumeration;

public class Queue
{
  // Friendly?
  Queue_Node head_ = null;
  Queue_Node tail_ = null;
  
  private int length_ = 0;
  
  public Queue()
    {     
    }

  public Enumeration forward_iterator()
    {
      return new Queue_Iterator(this, Queue_Iterator.FORWARD);
    }

  public Enumeration reverse_iterator()
    {
      return new Queue_Iterator(this, Queue_Iterator.REVERSE);
    }
  
  public void enqueue_tail(Object new_data)
    {
      Queue_Node new_node = new Queue_Node(new_data);

      if (tail_ == null)
	{
	  tail_ = new_node;
	  head_ = new_node;
	}
      else
	{
	  new_node.prev_ = tail_;
	  tail_.next_ = new_node;
	  tail_ = new_node;
	}

      length_++;
    }

  public void enqueue_head(Object new_data)
    {
      Queue_Node new_node = new Queue_Node(new_data);
	    
      if (head_ == null)
	{
	  tail_ = new_node;
	  head_ = new_node;
	}
      else
	{
	  new_node.next_ = head_;
	  head_.prev_ = new_node;
	  head_ = new_node;
	}

      length_++;
    }
  
  public Object dequeue_head()
    {
      Object return_value = null;
      
      if (head_ == null)
	return_value = null;
      else if (tail_ == head_)
	{
	  return_value = head_.data_;
	  tail_ = null;
	  head_ = null;
	}
      else
	{
	  return_value = head_.data_;
	  head_ = head_.next_;
	  head_.prev_ = null;	  
	}

      length_--;

      return return_value;
    }
  
  public Object dequeue_tail()
    {
      Object return_value = null;
      
      if (tail_ == null)
	return_value = null;
      else if (tail_ == head_)
	{
	  return_value = tail_.data_;
	  tail_ = null;
	  head_ = null;
	}
      else
	{	  
	  return_value = tail_.data_;
	  tail_ = tail_.prev_;
	  tail_.next_ = null;	  
	}

      length_--;

      return return_value;
    }

  public int length()
    {
      return length_;
    }

  public Object head()
    {
      if (head_ != null)
	return head_.data_;
      else
	return null;
    }

  public Object tail()
    {
      if (tail_ != null)
	return tail_.data_;
      else
	return null;
    }
}

class Queue_Node
{
  public Queue_Node prev_ = null;
  public Queue_Node next_ = null;;
  public Object data_;

  public Queue_Node(Object data)
    {
      data_ = data;
    }
}

class Queue_Iterator implements Enumeration
{
  public static final boolean FORWARD = true;
  public static final boolean REVERSE = false;
  
  private Queue queue_;
  private Queue_Node queue_ptr_;
  private boolean direction_ = FORWARD;
  
  public Queue_Iterator(Queue queue)
    {
      queue_ = queue;
      queue_ptr_ = queue.head_;
    }

  public Queue_Iterator(Queue queue, boolean direction)
    {
      queue_ = queue;
      direction_ = direction;

      if (direction_)
	queue_ptr_ = queue_.head_;
      else
	queue_ptr_ = queue_.tail_;
    }

  public Object nextElement()
    {
      Object data = queue_ptr_.data_;
      
      if (direction_)
	queue_ptr_ = queue_ptr_.next_;
      else
	queue_ptr_ = queue_ptr_.prev_;

      return data;
    }

  public boolean hasMoreElements()
    {
      return queue_ptr_ != null;
    }
}