何为队列
队列(queue):是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
队列(Queue)是插入操作限定在表的尾部而其它操作限定在表的头部进行的线性表。把进行插入操作的表尾称为队尾(Rear),把进行其它操作的头部称为队头(Front)。当队列中没有数据元素时称为空队列(Empty Queue)。
操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _003_队列 {
interface IQueue<T> {
int Count { get; }
int GetLength();
bool IsEmpty();
void Clear();
void Enqueue(T item);//入队
T Dequeue(); //出队,并删除数据
T Peek(); //取得队头的数据,不删除
}
}
顺序队列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _003_队列 {
class SeqQueue<T>:IQueue<T>
{
private T[] data;
private int count;//表示当前有多少个元素
private int front;//队首 (队首元素索引-1)
private int rear;//队尾(=队尾元素索引)
//构造
public SeqQueue(int size)
{
data = new T[size];
count = 0;
front = -1;
rear = -1;
}
public SeqQueue() : this(10)
{
}
public int Count
{
get { return count; }
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public void Clear()
{
count = 0;
front = rear = -1;
}
//入队
public void Enqueue(T item)
{
if (count == data.Length)
{
Console.WriteLine("队列已满,不可以再添加新的数据");
}
else
{
if (rear == data.Length - 1)
{
//当队列为空时,rear为-1,有一个数据时为0
//rear相当于从0开始计数
//rear==data.Length-1 即为队列最后一个元素
//循环队列实现
data[0] = item;
rear = 0;
count++;
}
else
{
data[rear + 1] = item;
rear++;
count++;
}
}
}
public T Dequeue()
{
if (count > 0)
{
T temp = data[front + 1];//取得队首的元素
front++; //引用上移,即删除了当前的元素,指向下一个元素
count--;
return temp;
}
else
{
Console.WriteLine("队列为空,无法取得队首的数据");
return default(T);
}
}
public T Peek()
{
T temp = data[front + 1];
return temp;
}
}
}
链队列
结点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _003_队列 {
class Node <T>
{
private T data;//数据
private Node<T> next;//指针
public Node(T data)
{
this.data = data;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
链队列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace _003_队列 {
class LinkQueue <T>:IQueue<T>
{
private Node<T> front;//头节点
private Node<T> rear;//尾结点
private int count;//表示元素的个数
//构造
public LinkQueue()
{
front = null;
rear = null;
count = 0;
}
public int Count
{
get { return count; }
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public void Clear()
{
front = null;
rear = null;
count = 0;
}
//入队
public void Enqueue(T item)
{
Node<T> newNode = new Node<T>(item);
if (count == 0)
{
front = newNode;
rear = newNode;
count = 1;
}
else
{
rear.Next = newNode;
rear = newNode;
count++;
}
}
//出队
public T Dequeue()
{
if (count == 0)
{
Console.WriteLine("队列为空,无法出队");
return default(T);
}else if (count == 1)
{
T temp = front.Data;
front = rear = null;
count = 0;
return temp;
}
else
{
T temp = front.Data;
front = front.Next;
count--;
return temp;
}
}
//取得队首的值
public T Peek()
{
if (front != null)
{
return front.Data;
}
else
{
return default(T);
}
}
}
}