循環隊列的鏈式存儲實現

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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章