队列-链队列

队列的另外一种存储方式是链式存储,这样的队列称为链队列(Linked Queue)。 同链栈一样,链队列通常用单链表来表示,它的实现是单链表的简化。

namespace 队列
{
    /// <summary>
    /// 链队列的 节点类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    class QueueNode<T>
    {
        public T data;
        public QueueNode<T> next;
        public QueueNode(T _data) {
            this.data = _data;
        }
        public override string ToString()
        {
            return data.ToString();
        }
    } 
}
namespace 队列
{
    interface IQueueDS<T>
    {
        int Count { get; }//获取元素个数
        int GetLength();
        bool IsEmpty(); //是否为空队列
        void Clear(); //清空队列
        void Enqueue(T item); //入队
        T Dequeue(); //出队
        T Peek(); //取队头元素
    } 
}
using System; 
namespace 队列
{
    class LinkedQueue<T> : IQueueDS<T>
    {
        public QueueNode<T> front;      //队头
        public QueueNode<T> rear;       //队尾
        private int count;       //元素的个数
        public LinkedQueue() {
            Clear();
        }
        public int Count {
            get {
                return count;
            }
        }

        public void Clear()
        {
            front = null;
            rear = null;
            count = 0;
        }
        //出队操作
        public T Dequeue()
        {
            if (this.front == null)
            {
                throw new ArgumentOutOfRangeException("队列下溢,队列里面没有数据");
            }
            T t = this.front.data;
            if (count == 1)
            {
                this.front = null;
                this.rear = null;
            }
            else {
                this.front = this.front.next;
            }
            count--;
            return t;
        }
        //入队操作
        public void Enqueue(T item)
        {
            QueueNode<T> newNode = new QueueNode<T>(item);
            if (this.rear == null)
            {
                this.front = newNode;
                this.rear = newNode;
            }
            else {
                this.rear.next = newNode;
                this.rear = newNode;
            }
            count++;
        }

        public int GetLength()
        {
            return count;
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public T Peek()
        {
            if (this.front == null) {
                throw new ArgumentOutOfRangeException("队列下溢,队列里面没有数据");
            }
            return this.front.data;
        }
    } 
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 队列
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个队列对象
            //Queue<int> queue = new Queue<int>();
            //创建一个循环顺序列表
            //IQueueDS<int> queue = new CSeqQueue<int>();
            创建一个链队列
            IQueueDS<int> queue = new LinkedQueue<int>();
            queue.Enqueue(10);  //入队操作,  队头位置
            queue.Enqueue(20);
            queue.Enqueue(30);
            queue.Enqueue(40);  //队尾
            Console.WriteLine("入队数据是 10,20,30,40,队列元素个数为 :"+queue.Count);
            int dq = queue.Dequeue();    //出队,返回出队数据
            Console.WriteLine("Dequeue 出队操作,数据为:"+dq+",队列元素个数为: "+queue.Count);

            int pk = queue.Peek();//只获取队头数据,不做出队操作
            Console.WriteLine("Peek 获取数据为:" + pk + ",队列元素个数为: " + queue.Count);

            queue.Clear();      //清空队列
            Console.WriteLine("Clear 清空操作,队列元素个数为: " + queue.Count);



            Console.WriteLine("=================================华丽分割线====================================");
            IQueueDS<string> cQueue = new CSeqQueue<string>();
            for (int i = 1; i < 8; i++)
            {
                cQueue.Enqueue("a"+i);  //1,2
            }
            for (int i = 0; i < 4; i++)
            {
                cQueue.Dequeue();
            }
            for (int i = 8; i < 13; i++)
            {
                cQueue.Enqueue("a" + i);  //efg
            }
            for (int i = 0; i < 4; i++)
            {
                cQueue.Dequeue();
            }
            Console.WriteLine("cQueue 获取数据为:" + cQueue.Peek() + ",队列元素个数为: " + cQueue.Count);
            Console.ReadKey();
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值