接口定义
interface IQueue<T>
{
int Count { get; }//取得队列长度的属性
int GetLength();//求队列的长度
bool IsEmpty();//判断是否为空
void Clear();//清空队列
void Enqueue(T item);//入队
T Dequeue();//出队 并删除元素
T Peek();//出队 不删除元素
}
顺序队列
class SeqQueue<T> : IQueue<T>
{
private T[] data;
private int front;//队首
private int rear;//队尾
private int count;//元素个数
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 void Clear()
{
front = -1;
rear = -1;
count = 0;
}
public T Dequeue()
{
if (count == 0)
{
Console.WriteLine("队列为空,无法取得队首数据");
return default(T);
}
if (front + 1 >= data.Length)
{
front = 0;
}
else
{
front++;
}
count--;
return data[front];
}
public void Enqueue(T item)
{
if (count == data.Length)
{
Console.WriteLine("队列已满,不可以再添加新的数据");
return;
}
if (rear + 1 >= data.Length)
{
rear = 0;
}
else
{
rear++;
}
data[rear] = item;
count++;
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public T Peek()
{
if (count == 0)
{
Console.WriteLine("队列为空,无法取得队首数据");
return default(T);
}
if (front + 1 >= data.Length)
{
return data[0];
}
else
{
return data[front+1];
}
}
}
链队列节点
class Node<T>
{
private T data;//存储数据
private Node<T> next;//指针 用来指向下一个元素
public Node()
{
this.data = default(T);
this.next = null;
}
public Node(T Value)
{
this.data = Value;
this.next = null;
}
public Node(T Value, Node<T> next)
{
this.data = Value;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
}
public T Data
{
get { return this.data; }
set { this.data = value; }
}
public Node<T> Next
{
get { return this.next; }
set { this.next = value; }
}
}
链队列实现代码
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 void Clear()
{
front = null;
rear = null;
count = 0;
}
public T Dequeue()
{
Node<T> tempNode;
if (count == 0)
{
Console.WriteLine("队列为空,无法取得队首数据");
return default(T);
}
tempNode = front;
front = front.Next;
count--;
return tempNode.Data;
}
public void Enqueue(T item)
{
Node<T> tempNode = new Node<T>(item);
if (count == 0)
{
rear = tempNode;
front = tempNode;
}
else
{
rear.Next = tempNode;
rear = tempNode;
}
count++;
}
public int GetLength()
{
return count;
}
public bool IsEmpty()
{
return count == 0;
}
public T Peek()
{
if (count == 0)
{
Console.WriteLine("队列为空,无法取得队首数据");
return default(T);
}
return front.Data;
}
}
测试代码
static void Main(string[] args)
{
//1、使用BCL中的Queue<T>
//Queue<int> queue = new Queue<int>();
//2、使用自定义表顺序队列
//SeqQueue<int> queue = new SeqQueue<int>();
//3、使用自定义栈队列
LinkQueue<int> queue = new LinkQueue<int>();
//入队 添加数据
queue.Enqueue(23);
queue.Enqueue(45);
queue.Enqueue(67);
queue.Enqueue(89);
Console.WriteLine("添加了23 45 67 89之后的队列长度为:" + queue.Count);
//出队 取得队首的数据并删除
int Temp = queue.Dequeue();
Console.WriteLine("Dequeue取得队首的数据为:" + Temp);
Console.WriteLine("Dequeue之后队列的大小为:" + queue.Count);
//取得队首的数据但是不删除
Temp = queue.Peek();
Console.WriteLine("Peek得到的数据为:" + Temp);
Console.WriteLine("Peek之后队列的大小为:" + queue.Count);
queue.Clear();
Console.WriteLine("Clear之后队列的大小为:" + queue.Count);
Console.Read();
}