public class LinkedQueue
{
private static final String TAG = "LinkedQueue";
private int size;
private QueueNode front = null;
private QueueNode rear = null;
class QueueNode
{
Object value;
QueueNode next = null;
QueueNode(Object obj)
{
this.value = obj;
}
}
public boolean isEmpty()
{
return size == 0;
}
public int getSize()
{
return size;
}
public void insert(Object obj)//尾巴进入
{
QueueNode node = new QueueNode(obj);
if (front == null && rear == null)
{
node.next = front;
front = node;
rear = front;
} else
{
rear.next = node;
rear = node;
}
size++;
}
public void delete() throws Exception//头部出来
{
if (front == null) throw new Exception("空队列!");
front = front.next;
size--;
}
public void display() throws Exception//头部出来
{
if (front == null) throw new Exception("空队列!");
QueueNode cur = front;
while (cur != rear)
{
System.out.print(cur.value + "<-");
cur = cur.next;
}
System.out.println(cur.value);
System.out.println("");
}
public static void main(String[] args) throws Exception
{
LinkedQueue lq = new LinkedQueue();
lq.insert("Moab");
lq.insert("123");
lq.insert("zhonguo");
lq.insert("MPY");
lq.insert("中国!");
System.out.println(lq.getSize());
lq.display();
lq.delete();
lq.delete();
lq.delete();
lq.insert("hha");
lq.insert("黑长直");
System.out.println(lq.getSize());
lq.display();
}
}