循环队列的链式存储实现

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();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值