c#队列的实现

c#队列的实现

/*
     用一片连续的存储空间来存储队列中的数据元素,这样的队列称为顺序队列
    (Sequence Queue)。类似于顺序栈,在这里我就不做介绍了,我们直接用列表实现一个队列
     */

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数据结构Linked_List
{
    public class LinkQueue<T>
    {
        private Node<T> front;//队列头指示器

        internal Node<T> Front
        {
            get { return front; }
            set { front = value; }
        }
        private Node<T> rear;//队列尾指示器

        internal Node<T> Rear
        {
            get { return rear; }
            set { rear = value; }
        }
        private int nCount;//队列结点个数

        public int NCount
        {
            get { return nCount; }
            set { nCount = value; }
        }
        public LinkQueue()
        {
            front = rear = null;
            nCount = 0;
        }
        public int GetLength()
        {
            return nCount;
        }

        public void Clear()
        {
            front = rear = null;
            nCount = 0;
        }

        public bool IsEmpty()
        {
            if ( front == rear && 0 == nCount )
            {
                return true;
            }
            return false;
        }

        public void Enqueue( T item )
        {
            Node<T> p = new Node<T>(item);
            if ( IsEmpty() )
            {
                front = rear = p;// 这步很重要 当第一个元素入队的时候,必须给front赋值,否则front一直都是null
            }
            else
            {
                rear.Next = p;
                rear = p;
            }
            ++nCount;
        }

        public T Dqueue()
        {
            if ( IsEmpty() )
            {
                Console.WriteLine("队列为空");
                return default(T);
            }
            Node<T> p = front;//从队列头出对
            front = front.Next;
            if ( front == null )
            {
                rear = null;
            }
            --nCount;
            return p.Data;
        }
        //获取链队列头结点的值
        public T GetFront()
        {
            if (IsEmpty())
            {
                Console.WriteLine("队列为空");
                return default(T);
            }
            return front.Data;
        }
    }
}

复制代码

/*
        队列的应用举例 
        编程判断一个字符串是否是回文。回文是指一个字符序列以中间字符
        为基准两边字符完全相同,如字符序列“ACBDEDBCA”是回文
        */

复制代码

public static bool IsPalindromic()
        {
            SeqStack<char> stack = new SeqStack<char>(50);
            LinkQueue<char> queue = new LinkQueue<char>();
            //string str = Console.ReadLine();// 这里随便输入
            string str = "ACBDEDBCA";// 测试
            for ( int i = 0; i < str.Length; ++i )
            {
                stack.Push(str[i]);
                queue.Enqueue(str[i]);
            }
            bool flag = false;
           // 这里只需循环一半元素即可
            for (int i = 0; i < str.Length >> 1; ++i )
            {
                if (queue.Dqueue() == stack.Pop())
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            return flag;
        }

复制代码

好了,今天就到这里了

原文地址:https://www.cnblogs.com/grj001/p/12225129.html