队列-数组实现:

using System;

namespace DS_ALGCode
{
    /// <summary>
    /// 队列-数组实现
    /// </summary>
    public class S_Queue
    {
        private int Rear = -1;
        public int MaxSize;
        public int[] Queue;

        public S_Queue(int size)
        {
            MaxSize = size;
            Queue = new int[MaxSize];
        }

        public bool IsFull { get => Rear == MaxSize - 1; }

        public bool IsEmpty { get => Rear == -1; }

        //入队
        public void Push(int data)
        {
            if (IsFull)
            {
                Console.WriteLine("队列满");
                return;
            }
            Rear++;
            Queue[Rear] = data;
        }

        //出队
        public int Pop()
        {
            if (IsEmpty)
            {
                Console.WriteLine("队列空");
                return 0;
            }
            int data = Queue[0];
            for (int i = 1; i <= Rear; i++)//重置队列
            {
                Queue[i - 1] = Queue[i];
            }
            Rear--;
            return data;
        }
        //列队
        public void Show()
        {
            if (IsEmpty)
            {
                Console.WriteLine("队列空");
                return;
            }
            for (int i = 0; i <= Rear; i++)
            {
                Console.WriteLine($"Queue[{i}] = {Queue[i]}");
            }
        }
    }
}

队列-链表实现:

namespace DS_ALGCode
{

    /// <summary>
    /// 链表节点
    /// </summary>
    public class Node
    {
        public int Data { get; set; }
        public Node Next { get; set; }

        public Node(int data)
        {
            Data = data;
            Next = null;
        }
    }
}

using System;

namespace DS_ALGCode
{
    /// <summary>
    /// 队列-链表实现
    /// </summary>
    public class S_QueueByLink
    {
        private Node Front = null;
        private Node Rear = null;
        private int Size = 0;

        public bool IsEmpty { get => Size == 0; }

        //入队
        public void Push(int data)
        {
            Node node = new Node(data);
            if (IsEmpty)
            {
                Front = node; 
            }
            else
            {
                Rear.Next = node;
            }
            Rear = node;
            Size++;
        }

        //出队
        public int Pop()
        {
            if (IsEmpty)
            {
                Console.WriteLine("队列空");
                return 0;
            }
            int data = Front.Data;
            Front = Front.Next;
            Size--;
            return data;
        }
        //列队
        public void Show()
        {
            if (IsEmpty)
            {
                Console.WriteLine("队列空");
                return;
            }
            Node curNode = Front;
            while (curNode != null)
            {
                Console.WriteLine(curNode.Data);
                curNode = curNode.Next;
            }
        }
    }
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐