using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{ //循环队列的链式存储
public class LinkQueue
{
NodeQueue front;
NodeQueue rear;
///
/// 构造函数 初始化
///
public LinkQueue()
{ //初始化一个地址,头节点
this.front = new NodeQueue() ;
this.rear = this.front;
}
///
/// 添加一个节点
///
///
public void InsertQueue(NodeQueue nodequeue) {
this.rear.next = nodequeue;
this.rear = nodequeue;//把当前的nodequeue设为队尾节点,并把rear指向nodequeue
}
///
/// 删除队列的第一个节点
///
/// 返回要删除的节点
public NodeQueue DeleteQueue() {
NodeQueue p,nodequeue;
if (this.rear==this.front)
{
return null;
}
p = this.front.next;
nodequeue = p;
this.front.next = p.next;
if (p==this.rear)
{
this.rear = this.front;
}
return nodequeue;
}
/// <summary>
/// 显示队列出列
/// </summary>
public void ShowLinkQueue() {
if (this.rear==this.front)
{
Console.WriteLine("该队列为空");
return;
}
while (this.front!=this.rear)
{
Console.WriteLine(this.front.next.data);
this.front = this.front.next;
}
}
}
/// <summary>
/// 定义节点
/// </summary>
public class NodeQueue
{
public string data { get; set; }
public NodeQueue next { get; set; }
public NodeQueue() { }
public NodeQueue(string data)
{
this.data = data;
this.next = null;
}
}
//测试
class Program
{
static void Main(string[] args)
{
LinkQueue linkQueue = new LinkQueue();
NodeQueue nodequeue1 = new NodeQueue("1");
NodeQueue nodequeue2 = new NodeQueue("2");
NodeQueue nodequeue3 = new NodeQueue("3");
NodeQueue nodequeue4 = new NodeQueue("4");
NodeQueue nodequeue5 = new NodeQueue("5");
linkQueue.InsertQueue(nodequeue1);
linkQueue.InsertQueue(nodequeue2);
linkQueue.InsertQueue(nodequeue3);
linkQueue.InsertQueue(nodequeue4);
linkQueue.InsertQueue(nodequeue5);
linkQueue.DeleteQueue();
linkQueue.ShowLinkQueue();
Console.ReadKey();
}
}
}