LeetCode 622的单链表实现
class Node{//定义一个节点类
public int value;//存放值
public Node next;//存放下一个节点
public Node(int value){
this.value = value;
this.next = null;
}
}
class MyCircularQueue {
public Node front,rear;//定义头节点和尾节点
public int size;//链表内节点的有效个数
public int maxSize;
public MyCircularQueue(int maxSize) {
front = rear = null;
this.maxSize = maxSize;
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
Node temp = new Node(value);
if(isEmpty()){//如果为空,将首尾节点都指向temp
front = rear = temp;//此时front和rear在同一个地址
}
rear.next = temp;//将temp接在rear后面,此时front的next中也存放了temp
rear = temp;//将rear指向temp,即rear指向最后一个节点
size++;//增加一个有效个数
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = front.next;//将头节点后移
size--;//有效个数减少
return true;
}
public int Front() {
if(isEmpty()){
return -1;
}
return front.value;
}
public int Rear() {
if(isEmpty()){
return -1;
}
return rear.value;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == maxSize;
}
}