// 请空链队列
public void clear() {
// 将front、rear两个节点赋为null
front = null;
rear = null;
size = 0;
}
public String toString() {
// 链队列为空队列时
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
for (Node current = front; current != null; current = current.next) {
sb.append(current.data.toString() + ", ");
}
int len = sb.length();
return sb.delete(len - 2, len).append("]").toString();
}
}
}
测试类:
package com.ietree.basic.datastructure.queue;
/**
* Created by ietree
* 2017/4/30
*/
public class LinkQueueTest {
public static void main(String[] args) {
LinkQueue<String> queue = new LinkQueue<String>("aaaa");
// 依次将4个元素加入到队列中
queue.add("bbbb");
queue.add("cccc");
queue.add("dddd");
System.out.println(queue);
// 删除一个元素后
queue.remove();
System.out.println("删除一个元素后的队列:" + queue);
// 再添加一个元素
queue.add("eeee");
System.out.println("再次添加元素后的队列:" + queue);
}
}
程序输出:
[aaaa, bbbb, cccc, dddd]
删除一个元素后的队列:[bbbb, cccc, dddd]
再次添加元素后的队列:[bbbb, cccc, dddd, eeee]