C#实现链队

1. 链队

    使用一个结点类Node,包含了数据域和指针域,定义了一个接口,和一个类实现接口。

1.1 Node.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _403_队列
{
    class Node<T>
    {
        private T data;
        private Node<T> next;

        public Node(T data)
        {
            this.data = data;
        }
        public T Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
        public Node<T> Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
    }
}

1.2 IQueue.cs(接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _403_队列
{
    interface IQueue<T>
    {
        int Count { get; }
        int GetLength();
        bool IsEmpty();
        void Clear();
        void Enqueue(T item);
        T Dequeue();
        T Peek();
    }
}

1.3 LinkQueue.cs(实现接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _403_队列
{
    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;
            }
        }
        /// <summary>
        ///清空对列
        /// </summary>
        public void Clear()
        {
            front = rear = null;
            count = 0;
        }

        /// <summary>
        /// 出队,取得队头元素并删除
        /// </summary>
        /// <returns></returns>
        public T Dequeue()
        {
            if(count==0)
            {
                Console.WriteLine("队列为空,无法出队");
                return default(T);
            }
            else if(count==1)
            {
                T temp = front.Data;
                front = rear = null;
                count = 0;
                return temp;
            }
            else
            {
                T temp = front.Data;
                front = front.Next;
                count--;
                return temp;
            }
        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="item"></param>
        public void Enqueue(T item)
        {
            Node<T> newNode = new Node<T>(item);
            if (count==0)
            {
                front = newNode;
                rear = newNode;
                count = 1;
            }
            else
            {
                rear.Next = newNode;
                rear = newNode;
                count++;
            }
            

        }

        /// <summary>
        /// 取得队列长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            return count;
        }

        /// <summary>
        /// 队列是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return count == 0;
        }

        /// <summary>
        /// 取得队首元素,不删除
        /// </summary>
        /// <returns></returns>
        public T Peek()
        {
            return front.Data;
        }
    }
}

1.4 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _403_队列
{
    class Program
    {
        static void Main(string[] args)
        {//3.使用自己定义的链队
            LinkQueue<int> queue = new LinkQueue<int>();
            //入队(添加数据)
            queue.Enqueue(3);//入队(放在队尾)
            queue.Enqueue(4);//入队(放在队尾)
            queue.Enqueue(5);//入队(放在队尾)
            queue.Enqueue(6);//入队(放在队尾)
            queue.Enqueue(7);//入队(放在队尾)

            Console.WriteLine("添加了3 4 5 6 7后队列大小为:" + queue.Count);

            //出队(取得队首数据并删除)
            int i = queue.Dequeue();
            Console.WriteLine("取得队首元素为:" + i);
            Console.WriteLine("出队后队列大小为:" + queue.Count);

            //取得队首元素但不删除
            int j = queue.Peek();
            Console.WriteLine("Peek后取得的元素:" + j);
            Console.WriteLine("Peek后对列大小:" + queue.Count);

            queue.Clear();
            Console.WriteLine("Clear后队列大小:" + queue.Count);
            Console.ReadKey();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大匣

你的鼓励是我创作最大动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值