队列是线性表的一种,所以队列即可以由数组组织起来,也可以由链表组织起来。所以我将使用单向链表,双向链表(底层的实现方式),循环数组的多种方式来模拟实现队列的“先进先出”的功能
使用单向链表模拟实现
使用头插法出队O(1),使用尾插法入队O(1),如果使用尾插法出队,需要遍历到前一个节点,时间复杂度O(n)
public class MyQueue1 {
/*
使用单向链表实现队列
*/
static class ListNode{
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//头节点
public ListNode tail;//尾巴节点
//在队尾入队,时间复杂度O(1)
public void offer(int val) {
ListNode node = new ListNode(val);
//队列为空
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
}
/*
在队头出队,时间复杂度O(1)
如果在队尾出队,时间复杂度是O(n),
因为要删除最好一个节点,要知道前一个节点,而我们只知道最后一个节点
*/
public int poll() {
//如果队列为空,返回-1
if(head == null) {
return -1;
}
int val = head.val;
//如果队列中只有一个节点
if(head.next == null) {
head = null;
tail = null;
}else {
head = head.next;
}
return val;
}
//查看队头元素
public int peek() {
//如果队列为空,返回-1
if(head == null) {
return -1;
}
//队列不为空,直接返回头节点的值
return head.val;
}
}
使用双向链表模拟实现
这也是jdk8队列的实现方式,双向链表的节点既有prev前驱域也有next后继域,所以无论是在头还是尾进行入队出队时间复杂度都是O(1)。咱们上面说了单向链表 尾删的时间复杂度是O(n),那我们就在双向链表中使用头部入队,尾部删,感受一下双向链表的优势
public class MyQueue2 {
//使用双向链表实现队列
static class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode tail;
/*
双向链表无论从头还是尾,入队或者出队时间复杂度都是O(1)
我们采用从头入从尾出
*/
public void offer(int val) {
ListNode node = new ListNode(val);
if(head == null) {
head = node;
tail = node;
}else {
node.next = head;
head.prev = node;
head = node;
}
}
//尾删,可以找到要删除节点的前驱节点所以时间复杂度O(1)
public int poll() {
//队列为空
if(head == null) {
return -1;
}
int val = tail.val;
ListNode cur = tail.prev;//记录尾节点的前一个节点
cur.next = null;
tail.prev = null;
tail = cur;
return val;
}
public int peek() {
//队列为空
if(head == null) {
return -1;
}
return tail.val;
}
}
使用循环数组模拟实现
采用浪费空间法
public class MyQueue3 {
/*
队列是线性表,即能使用链表实现,也能使用数组实现
咱们使用固定长度的循环数组实现线性表
*/
int[] elem;
public int front;//队头下标
public int rear;//队尾下标
public MyQueue3(int n) {
this.elem = new int[n + 1];
}
//入队
public boolean offer(int val) {
//先判满
if(isFull()) {
return false;
}else {
elem[rear] = val;
//不能使用rear++,
rear = (rear + 1) % elem.length;
return true;
}
}
//出队
public int poll() {
//先判空
if(isEmpty()) {
return -1;
}else {
int val = elem[front];
front = (front + 1) % elem.length;
return val;
}
}
//获取队头元素
public int getFront() {
if(isEmpty()) {
return -1;
}
return elem[front];
}
//获取队尾元素
public int getRear() {
if(isEmpty()) {
return -1;
}
int index = (rear == 0) ? elem.length - 1 : rear - 1;
return elem[index];
}
//判空
public boolean isEmpty() {
return rear == front;
}
//判满,采用浪费空间的方法,最后一格不放
public boolean isFull() {
return (rear + 1) % elem.length == front;
}
}
使用usedSize
使用usedSize统计元素个数就不用浪费一格空间来判满了
public class MyQueue4 {
int[] elem;
public int front;//队头下标
public int rear;//队尾下标
public int usedSize;
public MyQueue4(int n) {
this.elem = new int[n];
}
//入队
public boolean offer(int val) {
//先判满
if(isFull()) {
return false;
}else {
elem[rear] = val;
//不能使用rear++,
rear = (rear + 1) % elem.length;
usedSize++;
return true;
}
}
//出队
public int poll() {
//先判空
if(isEmpty()) {
return -1;
}else {
int val = elem[front];
front = (front + 1) % elem.length;
usedSize--;
return val;
}
}
//获取队头元素
public int getFront() {
if(isEmpty()) {
return -1;
}
return elem[front];
}
//获取队尾元素
public int getRear() {
if(isEmpty()) {
return -1;
}
int index = (rear == 0) ? elem.length - 1 : rear - 1;
return elem[index];
}
//判空
public boolean isEmpty() {
return usedSize == 0;
}
//判满
public boolean isFull() {
return usedSize == elem.length;
}
}