码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-03-16 22:04:30      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:returns   text   出栈   数据结构   ica   int   数据结构和算法   lin   链式存储   

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


    }
}

 

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

标签:returns   text   出栈   数据结构   ica   int   数据结构和算法   lin   链式存储   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!