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.display(); 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(); } }

相关文章:

  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-11
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
相关资源
相似解决方案