zoukankan      html  css  js  c++  java
  • C# 数据结构和算法-数组队列

    队列:

    队列是一个有序列表,遵循先入先出原则,可以用数组或链表实现

    使用场景

    用于排队,按顺序执行

    public static void Main(string[] args)
    {
        ArrayQueue<int> queue = new ArrayQueue<int>(1000);
        queue.Push(1);
        queue.Push(2);
        queue.Push(3);
        queue.Push(4);
        queue.Push(5);
    
        Console.WriteLine(queue.Pop());
        Console.WriteLine(queue.Pop());
        queue.Push(6);
        queue.Push(7);
        queue.Print();
        Console.ReadKey();
    }
    

      数组队列

    public class ArrayQueue<T>
    {
        private int _front = -1; //队首
        private int _rear = -1; //队尾
        private int _maxSize = 0;
        private T[] _arr = null;
    
        public ArrayQueue(int maxSize)
        {
            _maxSize = maxSize;
            _arr = new T[maxSize];
        }
    
        public bool IsFull()
        {
            return _rear >= _maxSize - 1;
        }
    
        public bool IsEmpty()
        {
            return _front >= _rear;
        }
    
        public void Push(T n)
        {
            if (this.IsFull())
            {
                throw new Exception("队列已满");
            }
            _arr[++_rear] = n;
        }
    
        public T Pop()
        {
            if (this.IsEmpty())
            {
                throw new Exception("队列已空");
            }
            return _arr[++_front];
        }
    
        public void Print()
        {
            if (this.IsEmpty())
            {
                Console.WriteLine("队列已空");
                return;
            }
            for (int i = _front + 1; i <= _rear; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
    

      

  • 相关阅读:
    HDU 1505 City Game
    HDU 1506 Largest Rectangle in a Histogram
    HDU 1838 Chessboard
    HDU 2512 一卡通大冒险
    HDU 1081 To The Max
    HDU 2845 Beans
    HDU 1422 重温世界杯
    HDU 1231 最大连续子序列
    第6章 接口和实现
    java面向抽象编程样例
  • 原文地址:https://www.cnblogs.com/itsone/p/13392606.html
Copyright © 2011-2022 走看看