创建自己的Queue底层实现是链表

时间:2021-09-09 17:38:56
package queueListedList;


import java.util.LinkedList;
import java.util.List;


public class QLNode<T> {


private List<T> list;


public List<T> getList() {
return list;
}


public void setList(List<T> list) {
this.list = list;
}


public QLNode(){
this.list=new LinkedList<T>();
}
public QLNode(List<T> list) {
super();
this.list = list;
}

}


package queueListedList;


public class QueueByLinkedList<T> {


private QLNode<T> queue;



public QLNode<T> getQueue() {
return queue;
}






public void setQueue(QLNode<T> queue) {
this.queue = queue;
}






public QueueByLinkedList(QLNode<T> queue) {
super();
this.queue = queue;
}
public QueueByLinkedList(){
this.queue=new QLNode<T>();
}

//队列是否为空
public boolean isEmpty(){
return queue.getList().isEmpty();
}

//插入元素
public void offer(T data){
queue.getList().add(data);
}

//删除元素
public T take(){
return queue.getList().remove(0);
}


//队列中元素的个数
public int size(){
return queue.getList().size();
}
//将队列清除
public void clear(){
queue.getList().clear();
}






public static void main(String[] args) {
QueueByLinkedList<String> queue=new QueueByLinkedList<String>();
queue.offer("a");
queue.offer("b");
queue.offer("c");
// queue.clear();
System.out.println(queue.size());
while(!queue.isEmpty()){
String v=queue.take();
System.out.print(v+" ");
}
System.out.println();
System.out.println(queue.size());


}


}