用c#List写的一个环形队列

  1     class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             var circle = new CircleArray(5);
  6             while (true)
  7             {
  8                 var firstStr = Console.ReadLine();
  9                 switch (firstStr)
 10                 {
 11                     case "a":
 12                         Console.WriteLine("请输入添加的值:");
 13                         var Str = Console.ReadLine();
 14                         circle.addQueue(int.Parse(Str));
 15                         break;
 16                     case "g":
 17                         circle.GetQueue();
 18                         break;
 19                     case "l":
 20                         circle.ListQueue();
 21                         break;
 22 
 23                 }
 24             }
 25 
 26         }
 27     }
 28 
 29     public class CircleArray
 30     {
 31 
 32         /// <summary>
 33         /// 该数据用于存放数据,模拟队列
 34         /// </summary>
 35         public List<int> list { get; set; }
 36 
 37         public int MaxSize { get; set; }
 38 
 39 
 40         /// <summary>
 41         /// 创建队列的构造器
 42         /// </summary>
 43         /// <param name="MaxSize">输入这个队列的大小</param>
 44         public CircleArray(int maxSize)
 45         {
 46             MaxSize = maxSize;
 47             list = new List<int>();
 48         }
 49         /// <summary>
 50         /// 判断队列是否满
 51         /// </summary>
 52         /// <returns></returns>
 53         public bool IsFull()
 54         {
 55             return list.Count() == MaxSize;
 56         }
 57 
 58         /// <summary>
 59         /// 判断队列是否为空
 60         /// </summary>
 61         /// <returns></returns>
 62         public bool IsEmpty()
 63         {
 64             return list.Count() == 0;
 65         }
 66 
 67         /// <summary>
 68         /// 添加队列
 69         /// </summary>
 70         /// <returns></returns>
 71         public void addQueue(int n)
 72         {
 73             if (IsFull())
 74             {
 75                 Console.WriteLine("队列满,不能加入数据!");
 76                 return;
 77             }
 78             list.Add(n);
 79             Console.WriteLine("队列队列添加数据成功!");
 80         }
 81 
 82         /// <summary>
 83         /// 取出队列值
 84         /// </summary>
 85         /// <returns></returns>
 86         public void GetQueue()
 87         {
 88             if (IsEmpty())
 89             {
 90                 Console.WriteLine("队列为空,没有可获取的数据!");
 91                 return;
 92             }      
 93             Console.WriteLine("获取队列值:" + list[0]);
 94             list.RemoveAt(0);
 95         }
 96 
 97         /// <summary>
 98         /// 查看所有队列值
 99         /// </summary>
100         /// <returns></returns>
101         public void ListQueue()
102         {
103             if (IsEmpty())
104             {
105                 Console.WriteLine("队列为空,没有可查看的数据!");
106             }
107             foreach (var item in list)
108             {
109                 Console.WriteLine("值:"+ item);
110             }
111         }
112     }

 

posted @ 2020-03-08 22:08    阅读(584)  评论(0)    收藏  举报