目录
一、通过链表实现循环列队。
1、定义一个节点类
public class ListNode
{
//当前值
public int val;
//前一个节点值
public ListNode prev;
//节点值后一个
public ListNode next;
public ListNode(int val){
this.val=val;
}
}
给当前节点赋值时,并同时指向上一个和下一个节点,类似循环链表,
这样我们添加/删除数据时,只需要修改当前节点、上一个节点、下一个节点的指向即可。
2、构造列队初始化
定义一个头结点和尾节点,并且头部节点的上/下指向尾节点,尾节点同样都指向头结点,
这样就形成一个循环,只需要在中间添加数据时改变相对应上下指针指向,就可以实现添加生成数据。
3、在LeetCode—队列&栈 涉及循环列队的实现
public class MyCircularQueue {
int cap;
int count;
ListNode head;
ListNode tail;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
cap=k;
count=0;
head=new ListNode(0);
tail=new ListNode(0);
head.next=tail;
tail.prev=head;
tail.next=head;
head.prev=tail;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public bool EnQueue(int _value) {
if(IsFull())return false;
ListNode prev=tail.prev;
ListNode curr=new ListNode(_value);
prev.next=curr;
tail.prev=curr;
curr.prev=prev;
curr.next=tail;
count++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public bool DeQueue() {
if(IsEmpty()){
return false;
}else
{
ListNode target=head.next;
head.next=target.next;
target.next.prev=head;
target.prev=null;
target.next=null;
count--;
return true;
}
}
/** Get the front item from the queue. */
public int Front() {
if(IsEmpty()){
return -1;
}else{
return head.next.val;
}
}
/** Get the last item from the queue. */
public int Rear() {
if(IsEmpty()){
return -1;
}else{
return tail.prev.val;
}
}
/** Checks whether the circular queue is empty or not. */
public bool IsEmpty() {
return count==0;
}
/** Checks whether the circular queue is full or not. */
public bool IsFull() {
return count==cap;
}
public class ListNode
{
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val){
this.val=val;
}
}
}
二、官方给的用数组对循环列队的实现
在循环队列中,我们使用一个数组
和两个指针(head
和 tail
)。 head
表示队列的起始位置,tail
表示队列的结束位置。
官方给的是JAVA我把他改写成C#
public class MyCircularQueue {
private int[] _items;
private int head;
private int tail;
private int size;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k)
{
_items = new int[k];
head = -1;
tail = -1;
size = k;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public bool EnQueue(int value)
{
if (IsFull() == true)
{
return false;
}
if (IsEmpty() == true)
{
head = 0;
}
tail = (tail + 1) % size;
_items[tail] = value;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public bool DeQueue()
{
if (IsEmpty() == true)
{
return false;
}
if (head == tail)
{
head = -1;
tail = -1;
return true;
}
head = (head + 1) % size;
return true;
}
/** Get the front item from the queue. */
public int Front()
{
if (IsEmpty() == true)
{
return -1;
}
return _items[head];
}
/** Get the last item from the queue. */
public int Rear()
{
if (IsEmpty() == true)
{
return -1;
}
return _items[tail];
}
/** Checks whether the circular queue is empty or not. */
public bool IsEmpty()
{
return head == -1;
}
/** Checks whether the circular queue is full or not. */
public bool IsFull()
{
return ((tail + 1) % size) == head;
}
}