基于链表实现队列
public class LinkQueue {// front的next指向首部结点private Node front;// rear记录尾部结点private Node rear;private int size;public LinkQueue() {this.front = new Node(0);this.rear = new Node(0);}// 入队public void push(int value) {Node newNode = new Node(value);Node temp = front;while (temp.next != null) {temp = temp.next;}temp.next = newNode;rear = newNode;size++;}// 出队public int pull() {if (front.next == null) {System.out.println("队列为空");}Node firstNode = front.next;front.next = firstNode.next;size--;return firstNode.data;}// 遍历public void traverse() {Node temp = front.next;while (temp != null) {System.out.println(temp.data + "\t");temp = temp.next;}}// 结点结构static class Node {public int data;public Node next;public Node(int data) {this.data = data;}}
}
如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ♥
算法通关村专栏:不易|算法通关村 ♥