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

猜你喜欢

转载自blog.csdn.net/a962035/article/details/79796634