三,数组模拟环形队列

实现思路:

1)设置队列头front=0指向数组第一个元素

2)设置队列尾rear=0指向数组尾部的下一个元素,这时front==rear 队列为空

3)队列首尾留出一个空位,方便程序理解是满还是空。判断队列满条件为(rear+1)%maxSize,留空位置是不断变化的

4)队列有效数据计算公式,rear - front + maxSize % maxSize ,(这地方比较抽象,可以画个环形数组帮组理解)

  1 using System;
  2 
  3 namespace 数据结构
  4 {
  5     public class CircleQueue
  6     {
  7         //队列最大容量
  8         private int maxSize;
  9         //队列头
 10         private int front;
 11         //队列尾
 12         private int rear;
 13         //队列数组
 14         private int[] arrayQueue;
 15         //队列尾下一位置,如果和头部重合说明队列已满
 16         //所以队列尾部会空一个位置,这样判断队列是否为空好处理一些
 17         public bool IsFull { get => (rear + 1) % maxSize == front; }
 18         //头尾同时指向同一位置为空
 19         public bool IsEmpty { get => front == rear; }
 20         //队列剩余数据
 21         public int Size { get => (maxSize + rear - front) % maxSize; }
 22 
 23         public CircleQueue(int maxSize = 1)
 24         {
 25             //由于队列留空了一格,所以最大值要加一
 26             this.maxSize = maxSize + 1;
 27             arrayQueue = new int[this.maxSize];
 28             //指向队列头
 29             this.front = 0;
 30             //指向队列尾的下一个位置,也就三队列头部
 31             this.rear = 0;
 32         }
 33         //入队
 34         public bool AddQueue(int item)
 35         {
 36             if (IsFull)
 37             {
 38                 Console.WriteLine("队列已满...");
 39                 return false;
 40             }
 41             arrayQueue[rear] = item;
 42             //尾指向下一位置
 43             rear = (rear + 1) % this.maxSize;
 44             return true;
 45         }
 46         //出队
 47         public int GetQueue()
 48         {
 49             if (IsEmpty)
 50             {
 51                 throw new IndexOutOfRangeException("队列为空...");
 52 
 53             }
 54             //头指向下一位置
 55             var val = arrayQueue[front];
 56             front = (front + 1) % this.maxSize;
 57             return val;
 58         }
 59     }
 60 
 61     public class CircleQueueDemo
 62     {
 63         static void Main(string[] args)
 64         {
 65             //初始化队列
 66             var queue = new CircleQueue(9);
 67             Console.WriteLine($"当前队列长度为{queue.Size}");
 68             Console.WriteLine("向长度为9的入队10个数字\n");
 69             for (int i = 1; i <= 10; i++)
 70             {
 71 
 72                 if (queue.IsFull)
 73                 {
 74                     Console.Write("队列已满“10”入队失败,");
 75                     break;
 76                 }
 77                 if (queue.AddQueue(i))
 78                 {
 79                     Console.Write($"{i}\t");
 80                 }
 81 
 82             }
 83             Console.WriteLine($"当前队列长度为{queue.Size}\t");
 84 
 85             //-----------------------------------------------------------
 86 
 87             Console.WriteLine("出队5个数字...\n");
 88             for (int i = 1; i <= 5; i++)
 89             {
 90 
 91                 Console.Write($"{queue.GetQueue()}\t");
 92 
 93             }
 94             Console.WriteLine($"当前队列长度为{queue.Size}\n");
 95 
 96             //-----------------------------------------------------------
 97 
 98             Console.WriteLine("向队列队列插入10个数字...\n");
 99             for (int i = 1; i <= 10; i++)
100             {
101                 if (queue.IsFull)
102                 {
103                     Console.Write("队列已满“6 7 8 9 10”入队失败,");
104                     break;
105                 }
106                 if (queue.AddQueue(i))
107                 {
108                     Console.Write($"{i}\t");
109                 }
110 
111             }
112             Console.WriteLine($"当前队列长度为{queue.Size}\n");
113 
114             //-----------------------------------------------------------
115 
116             Console.WriteLine($"打印当前队列");
117             var size = queue.Size;
118             for (int i = 0; i < size; i++)
119             {
120                 Console.Write($"{queue.GetQueue()}\t");
121             }
122             Console.WriteLine($"当前队列长度为{queue.Size}\n");
123         }
124     }
125 }

猜你喜欢

转载自www.cnblogs.com/xiaojvhuang/p/12682225.html