使用C#实现顺序队列

队列(Queue)是插入操作限定在表的尾部而其它操作限定在表的头部进行的线性表。把进行插入操作的表尾称为队尾(Rear),把进行其它操作的头部称为队头(Front)。当对列中没有数据元素时称为空对列(Empty Queue)。

队列通常记为:Q= (a1,a2,…,an),a1为队头元素,an为队尾元素。元素按照a1,a2,…,an的次序依次入队,出队的次序与入队相同,即a1第一个出队,an最后一个出队。所以,对列的操作是按照先进先出(First In First Out)或后进后出( Last In Last Out)的原则进行的,因此,队列又称为FIFO表或LILO表。

队列的常用操作有:

1、构造一个空队列:InitQueue()//在C#中可以使用构造函数来实现

2、清空队列:ClearQueue()

3、判断队列是否为空:IsEmpty()

4、判断队列是否已满:IsFull()

5、求队列长度:QueueLength()

6、入队操作:In()

7、出队操作:Out()

8、得到队头元素:GetHead()

 下面给出一个实现顺序栈的源代码

  1. using System;  
  2. class Queue  
  3. {  
  4.     object[] data;  //数据元素  
  5.     int maxsize;    //最大容量  
  6.     int front;      //指向队头  
  7.     int rear;       //指向队尾  
  8.   
  9.     //初始化队列  
  10.     public Queue(int size)  
  11.     {  
  12.   
  13.         this.maxsize = size;  
  14.         data = new object[size];  
  15.         front = rear = -1;  
  16.     }  
  17.   
  18.     //最大容量属性  
  19.     public int MaxSize  
  20.     {  
  21.         get  
  22.         {  
  23.             return this.maxsize;  
  24.         }  
  25.         set  
  26.         {  
  27.             this.maxsize = value;  
  28.         }  
  29.     }  
  30.   
  31.     //队尾属性  
  32.     public int Rear  
  33.     {  
  34.         get  
  35.         {  
  36.             return this.rear;  
  37.         }  
  38.     }  
  39.   
  40.     //队头属性  
  41.     public int Front  
  42.     {  
  43.         get  
  44.         {  
  45.             return this.front;  
  46.         }  
  47.     }  
  48.   
  49.     //数据属性  
  50.     public object this[int index]  
  51.     {  
  52.         get  
  53.         {  
  54.             return data[index];  
  55.         }  
  56.     }  
  57.   
  58.     //获得队列的长度  
  59.     public int GetQueueLength()  
  60.     {  
  61.         return rear - front;  
  62.     }  
  63.   
  64.     //判断队列是否满  
  65.     public bool IsFull()  
  66.     {  
  67.         if (GetQueueLength() == maxsize)  
  68.             return true;  
  69.         else  
  70.             return false;  
  71.     }  
  72.   
  73.     //判断队列是否为空  
  74.     public bool IsEmpty()  
  75.     {  
  76.         if (rear == front)  
  77.             return true;  
  78.         else  
  79.             return false;  
  80.     }  
  81.   
  82.     //清空队列  
  83.     public void ClearQueue()  
  84.     {  
  85.         rear = front = -1;  
  86.     }  
  87.   
  88.     //入队  
  89.     public void In(object e)  
  90.     {  
  91.   
  92.         if (IsFull())  
  93.         {  
  94.             Console.WriteLine("队列已满!");  
  95.             return;  
  96.         }  
  97.         data[++rear] = e;  
  98.     }  
  99.   
  100.     //出队  
  101.     public object Out()  
  102.     {  
  103.   
  104.         if (IsEmpty())  
  105.         {  
  106.             Console.WriteLine("队列为空!");  
  107.             return null;  
  108.         }  
  109.   
  110.         if (rear - front > 0)  
  111.         {  
  112.             object tmp = data[++front];  
  113.             return tmp;  
  114.         }  
  115.         else  
  116.         {  
  117.             Console.WriteLine("全出队了!");  
  118.             ClearQueue();  
  119.             return null;  
  120.         }  
  121.     }  
  122.   
  123.     //获得队头元素  
  124.     public object GetHead()  
  125.     {  
  126.   
  127.         if (IsEmpty())  
  128.         {  
  129.             Console.WriteLine("队列为空!");  
  130.             return null;  
  131.         }  
  132.         return data[front + 1];  
  133.     }  
  134.   
  135. }  
  136.   
  137. class Test  
  138. {  
  139.   
  140.     static void Main()  
  141.     {  
  142.         Queue q = new Queue(5);  
  143.         int rdNum;  
  144.         Random rd = new Random();  
  145.         while (!q.IsFull())  
  146.         {  
  147.             rdNum = rd.Next(10, 100);  
  148.             q.In(rdNum);  
  149.             Console.WriteLine("{0}入队,队列元素个数:{1}", rdNum, q.GetQueueLength());  
  150.         }  
  151.         Console.WriteLine("***************************");  
  152.         while (!q.IsEmpty())  
  153.         {  
  154.             Console.WriteLine("{0}出队,队列元素个数:{1}", q.Out(), q.GetQueueLength());  
  155.         }  
  156.     }  
  157. }  

运行结果:

23入队,队列元素个数:1

85入队,队列元素个数:2

28入队,队列元素个数:3

94入队,队列元素个数:4

55入队,队列元素个数:5

***************************

23出队,队列元素个数:4

85出队,队列元素个数:3

28出队,队列元素个数:2

94出队,队列元素个数:1

55出队,队列元素个数:0

请按任意键继续. . .

原文地址:https://www.cnblogs.com/itjeff/p/5439934.html