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

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


    }
}

 

posted @ 2019-03-16 21:47  围墙外的世界  阅读(259)  评论(0编辑  收藏  举报