您的位置:首页 > 编程语言 > Java开发

java队列的链表实现

2016-03-21 13:13 525 查看

    java的队列实现

public class LinkQueue {

Node head = null;
Node tail = null;

boolean isEmpty(){
if(head==null)
return true;
else
return false;
}

void put(T data){
Node newNode = new Node(data);
if(head==tail && head==null){
newNode.next = tail;
tail = head = newNode;
}else{
tail.next = newNode;
tail = newNode;
}
}
T pop(){

if(!isEmpty()){
Node popNode= head;
head = head.next;
return popNode.data;
}else{
tail=head=null;
return null;
}
}
public static void main(String[] args) {
LinkQueue linkQueue = new LinkQueue();
linkQueue.put(1);
linkQueue.put(11);
linkQueue.put(111);
linkQueue.put(1111);
linkQueue.put(11111);
while(!linkQueue.isEmpty()){
System.out.println(linkQueue.pop());
}
}
}
class Node{
E data;
Node next = null;
public Node() {
}
Node(E data){
this.data = data;
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: