C#数据结构篇(3 队列类)

** C#数据结构篇(3 队列类) **

**** 在实现堆栈类之后,我们来实现队列类,其实队列也是链表的扩展,它其实是一种特殊的链表,如堆栈一样,它和堆栈的不同在于,堆栈采用的是先进后出原则,而对列采用的是先进先出原则。什么是先进后出呢?在日常生活中也到处可见它,比如:买火车票,大家都要排队,先来的先买票,后来的在后面排队,在队伍中随便插入是不合法的。等到前面买完之后,自己才可以买票。当然这只是个小实例,队列在程序设计中是基础的编程技术,现在我们来用才C#实现它。如:下图是队列入队和出队的操作过程。

1 在入队过程中: 就是在队列的尾部添加数据,队列数据个数加一,尾指针后移。

2 在出队过程中:就是在队列的头部取的数据后,然后删除该数据,头指针后移。

![](http://dev.csdn.net/article/13/D:/Documents and Settings\Killertang\My Documents\My Pictures\DataStruct\QueueGraph.jpg)

在下面的程序中用到前面我们编写的list 类 ( C#数据结构篇(1 链表类)),对链表进行操作就可以轻松的实现队列,具体实现如下:

using System;

namespace List
{
///

1<summary>   
2/// 队列类   
3/// </summary>

public class CQueue
{
private Clist m_List;

public CQueue()
{
//构造函数

//这里使用到前面编写的List
m_List=new Clist ();

}

///

1<summary>   
2/// 入队   
3/// </summary>

public void EnQueue(int DataValue)
{
//功能:加入队列,这里使用List 类的Append 方法:

//尾部添加数据,数据个数加1

m_List.Append (DataValue);
}

///

1<summary>   
2/// 出队   
3/// </summary>

public int DeQueue()
{
//功 能:出队

//返回值: 2147483647 表示为空队列无返回

int QueValue;

if (! IsNull())
{
//不为空的队列

//移动到队列的头

m_List.MoveFrist ();

//取得当前的值

QueValue= m_List.GetCurrentValue ();

//删除出队的数据

m_List.Delete ();

return QueValue;

}

return 2147483647;

}

///

1<summary>   
2/// 判断队列是否为空   
3/// </summary>

public bool IsNull()
{
//功能:判断是否为空的队列

return m_List.IsNull ();

}

///

1<summary>   
2/// 清空队列   
3/// </summary>

public void Clear()
{

//清空链表
m_List.Clear ();

}

///

1<summary>   
2/// 取得队列的数据个数   
3/// </summary>

public int QueueCount
{
get
{
//取得队列的个数
return m_List.ListCount ;
}
}

}
}

好了,只要我们编写一个链表类,我们就可以轻松的实现队列了,可以省很多的代码呀。

在Vs.net IDE 英文版编译通过。 好了,就写到这里了,886。(To Be Continu)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus