首页 > 编程语言 > 详细

数据结构和算法基础之队列的链式存储

时间:2019-03-16 21:53:34      阅读:144      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class QueueNode<T>
    {
        public T Data;
        public QueueNode<T> Next;

        public QueueNode()
        {
            Data = default(T);
            Next = null;
        }

        public QueueNode(T data)
        {
            Data = data;
            Next = null;
        }
    }
    /// <summary>
    /// 链式队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkQueue<T>
    {
        public QueueNode<T> Head;
        public QueueNode<T> Front;
        public QueueNode<T> Rear;
        public int Count;

        public LinkQueue()
        {
            Head = null;
            Front = null;
            Count = 0;
        }

        /// <summary>
        /// 入栈
        /// </summary>
        public void Enqueue(T data)
        {
            QueueNode<T> node = new QueueNode<T>(data);
            if(Head==null)
            {
                Head = new QueueNode<T>();
                Front = Head;
                Rear = Head;
            }
            Count++;
            Rear.Next = node;
            Rear = node;
            node.Next = null;
        }

        /// <summary>
        /// 出栈
        /// </summary>
        /// <returns></returns>
        public  T Dequeue()
        {
            if(Count>0)
            {
                T data = Front.Next.Data;
                Head = Front.Next;
                Front = Head;
                Count--;
                return data;

            }
            return default(T);
        }


    }
}

 

数据结构和算法基础之队列的链式存储

原文:https://www.cnblogs.com/weiqiangwaideshijie/p/10544503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!