一、基本介绍
- 队列是一个有序列表,可以用数组或是链表来实现。
- 遵循先入先出的原则。即:先存入队列的数据,要先取出。
二、数组实现
public class Node {
private int id;
public Node(int value) {
this.id = value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class CircleArrayUtil {
private int maxSize;
private int front;
private int rear;
private Node[] arr;
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize + 1;
arr = new Node[maxSize];
}
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
public boolean isEmpty() {
return rear == front;
}
public void addQueue(Node n) {
if (isFull()) {
System.out.println("队列已满");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
public Node getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列已空");
}
Node value = arr[front];
front = (front + 1) % maxSize;
return value;
}
public void showQueue() {
if (isEmpty()) {
System.out.println("队列空的,没有数据~~");
return;
}
for (int i = front; i < front + size() ; i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize].getId());
}
}
public int size() {
return (rear + maxSize - front) % maxSize;
}
}
二、链表实现
- 链表是以节点的方式来存储,是链式存储,每个节点包含 data 域、next 域;链表的各个节点不一定是连续存储。
- 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定。
1. 单向链表实现队列效果
class Node {
private int id;
public Node next;
public Node(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class SingleLinkedList {
private Node head = new Node(0);
public Node getFirstNode() {
Node temp = head.next;
if (temp == null) {
System.out.println("链表为空");
return null;
}
head.next = temp.next;
return temp;
}
public void add(Node node) {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = node;
}
public void list() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
Node temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp.getId());
temp = temp.next;
}
}
public int size() {
if (head.next == null) {
System.out.println("链表为空");
return 0;
}
int count = 0;
Node temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp.getId());
temp = temp.next;
count++;
}
return count;
}
public void addByOrder(Node node) {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
} else if (temp.next.getId() >= node.getId()) {
break;
}
temp = temp.next;
}
node.next = temp.next;
temp.next = node;
}
public void del(int id) {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.getId() == id) {
temp.next = temp.next.next;
}
temp = temp.next;
}
}
}
2. 双向链表实现队列效果
class Node {
private int id;
public Node next;
public Node pre;
public Node(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class DoubleLinkedList {
private Node head = new Node(0);
public Node getFirstNode() {
Node temp = head.next;
if (temp == null) {
System.out.println("链表为空");
return null;
}
head.next = temp.next;
if (temp.next != null) {
temp.next.pre = temp.pre;
}
return temp;
}
public void list() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
Node temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp.getId());
temp = temp.next;
}
}
public void add(Node node) {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = node;
head.pre = temp;
}
public void del(int id) {
if (head.next == null) {
System.out.println("链表为空,无法删除");
return;
}
Node temp = head.next;
while (true) {
if (temp == null) {
break;
}
if (temp.getId() == id) {
temp.pre.next = temp.next;
if (temp.next != null) {
temp.next.pre = temp.pre;
}
}
temp = temp.next;
}
}
}
3. 小结
- 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
- 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除。