数据结构实验 第三单元 队列操作

本文介绍了一种使用链表实现的队列数据结构,并提供了完整的C#代码示例。该队列支持基本操作如初始化、清空、判断是否为空、入队、出队等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

namespace Queue
{
    internal class LinkedQueue
    {
        class Node
        {
            public  object data;
            public  Node next;
        }
        //他们均指向节点,不存数据
        Node front, rear;
        int length = 0;
        public LinkedQueue()
        {
            Initial();
        }
        public LinkedQueue(IEnumerable source)
        {
            Initial();
            foreach (var ele in source)
            {
                Push(ele);
            }
        }
        /// <summary>
        /// 初始化队列
        /// </summary>
        public void Initial()
        {
            front = new Node();
            rear = new Node();
            front.data = null;
            front.next = null;
            rear.data = null;
            rear.next = null;
            length = 0;
        }

        /// <summary>
        /// 置队空
        /// </summary>
        public void  Clear()
        {
            front.next = null;
            length = 0;
        }
        /// <summary>
        /// 判队空
        /// </summary>
        /// <returns></returns>
        public Boolean Empty()
        {
            return front.next == null;
        }
        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Boolean Push(object obj)
        {
            Node node = new Node();
            node.data = obj;
            node.next = null;
            if (Empty())
            {
                front.next = node;
                rear.next = node;
            }
            else
            {
                Node last = rear.next;
                last.next = node;
                rear.next = node;
            }
            length++;
            return true;
        }
        /// <summary>
        /// 出队
        /// </summary>
        /// <returns></returns>
        public object Pop()
        {
            Node first = front.next;
            front.next = first.next;
            if (object.ReferenceEquals(first,rear.next))
            {
                //最后一个元素了
                rear.next = null;
            }
            length--;
            return first.data;
        }
        /// <summary>
        /// 取队头元素
        /// </summary>
        /// <returns></returns>
        public object Front()
        {
            return front.data;
        }
        /// <summary>
        /// 取队尾元素
        /// </summary>
        /// <returns></returns>
        public object Rear()
        {
            return rear.data;
        }

        public List<object> ToList()
        {
            List<object> list = new List<object>();
            Node node = front.next;
            while (node != null)
            {
                list.Add(node.data);
                node = node.next;
            }
            return list;
        }

        public int Length {
            get { return length; }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值