public class Queue {
//创建两个节点,一个保存数据,一个指向节点
public Node head;
public Node current;
//添加节点
public void add(int data) {
if(head == null) {
head = new Node(data);
current = head;
} else {
//current.next指向新建节点
//current指向current.next
//也就是current指向新建节点
current.next = new Node(data);
current = current.next;
}
}
public Node pop() {
if(head == null) {
return null;
}
Node node = head;
head = head.next;
return node;
}
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Queue queue = new Queue();
for(int i=0; i<5; i++) {
queue.add(i);
}
System.out.println(queue.pop().data);
System.out.println(queue.pop().data);
System.out.println(queue.pop().data);
System.out.println(queue.pop().data);
System.out.println(queue.pop().data);
}
}
算法--列(java实现)
最新推荐文章于 2023-06-27 15:41:50 发布