六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 907|回复: 0

使用C#实现顺序队列

[复制链接]
 楼主| 发表于 2013-4-26 14:59:26 | 显示全部楼层 |阅读模式

队列(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.     public Queue(int size)

  10.     {

  11.         this.maxsize = size;

  12.         data = new object[size];

  13.         front = rear = -1;

  14.     }

  15.     //最大容量属性

  16.     public int MaxSize

  17.     {

  18.         get

  19.         {

  20.             return this.maxsize;

  21.         }

  22.         set

  23.         {

  24.             this.maxsize = value;

  25.         }

  26.     }

  27.     //队尾属性

  28.     public int Rear

  29.     {

  30.         get

  31.         {

  32.             return this.rear;

  33.         }

  34.     }

  35.     //队头属性

  36.     public int Front

  37.     {

  38.         get

  39.         {

  40.             return this.front;

  41.         }

  42.     }

  43.     //数据属性

  44.     public object this[int index]

  45.     {

  46.         get

  47.         {

  48.             return data[index];

  49.         }

  50.     }

  51.     //获得队列的长度

  52.     public int GetQueueLength()

  53.     {

  54.         return rear-front;

  55.     }

  56.     //判断队列是否满

  57.     public bool IsFull()

  58.     {

  59.         if(GetQueueLength() == maxsize)

  60.             return true;

  61.         else

  62.             return false;

  63.     }

  64.     //判断队列是否为空

  65.     public bool IsEmpty()

  66.     {

  67.         if(rear==front)

  68.             return true;

  69.         else

  70.             return false;

  71.     }

  72.     //清空队列

  73.     public void ClearQueue()

  74.     {

  75.         rear = front = -1;

  76.     }

  77.     //入队

  78.     public void In(object e)

  79.     {

  80.         if(IsFull())

  81.         {

  82.             Console.WriteLine("队列已满!");

  83.             return;

  84.         }

  85.         data[++rear]=e;

  86.     }

  87.     //出队

  88.     public object Out()

  89.     {

  90.         if(IsEmpty())

  91.         {

  92.             Console.WriteLine("队列为空!");

  93.             return null;

  94.         }

  95.         if(rear-front>0)

  96.         {

  97.             object tmp = data[++front];

  98.             return tmp;

  99.         }

  100.         else

  101.         {

  102.             Console.WriteLine("全出队了!");

  103.             ClearQueue();

  104.             return null;

  105.         }

  106.     }

  107.     //获得队头元素

  108.     public object GetHead()

  109.     {

  110.         if(IsEmpty())

  111.         {

  112.             Console.WriteLine("队列为空!");

  113.             return null;

  114.         }

  115.         return data[front+1];

  116.     }

  117. }

  118. class Test

  119. {

  120.     static void Main()

  121.     {

  122.         Queue q = new Queue(1);

  123.         int rdNum;

  124.         Random rd = new Random();

  125.         while(!q.IsFull())

  126.         {

  127.             rdNum = rd.Next(10,100);

  128.             q.In(rdNum);

  129.             Console.WriteLine("{0}入队,队列元素个数:{1}",rdNum,q.GetQueueLength());

  130.            

  131.         }

  132.         Console.WriteLine("***************************");

  133.         while(!q.IsEmpty())

  134.         {

  135.             Console.WriteLine("{0}出队,队列元素个数:{1}",q.Out(),q.GetQueueLength());

  136.         }      

  137.     }

  138. }
复制代码

运行结果如下:

  


本文转载自:http://blog.163.com/fujl_2008/bl ... 107200810975036509/



该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表