使用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()
下面给出一个实现顺序栈的源代码
- using System;
- class Queue
- {
- object[] data; //数据元素
- int maxsize; //最大容量
- int front; //指向队头
- int rear; //指向队尾
- //初始化队列
- public Queue(int size)
- {
- this.maxsize = size;
- data = new object[size];
- front = rear = -1;
- }
- //最大容量属性
- public int MaxSize
- {
- get
- {
- return this.maxsize;
- }
- set
- {
- this.maxsize = value;
- }
- }
- //队尾属性
- public int Rear
- {
- get
- {
- return this.rear;
- }
- }
- //队头属性
- public int Front
- {
- get
- {
- return this.front;
- }
- }
- //数据属性
- public object this[int index]
- {
- get
- {
- return data[index];
- }
- }
- //获得队列的长度
- public int GetQueueLength()
- {
- return rear - front;
- }
- //判断队列是否满
- public bool IsFull()
- {
- if (GetQueueLength() == maxsize)
- return true;
- else
- return false;
- }
- //判断队列是否为空
- public bool IsEmpty()
- {
- if (rear == front)
- return true;
- else
- return false;
- }
- //清空队列
- public void ClearQueue()
- {
- rear = front = -1;
- }
- //入队
- public void In(object e)
- {
- if (IsFull())
- {
- Console.WriteLine("队列已满!");
- return;
- }
- data[++rear] = e;
- }
- //出队
- public object Out()
- {
- if (IsEmpty())
- {
- Console.WriteLine("队列为空!");
- return null;
- }
- if (rear - front > 0)
- {
- object tmp = data[++front];
- return tmp;
- }
- else
- {
- Console.WriteLine("全出队了!");
- ClearQueue();
- return null;
- }
- }
- //获得队头元素
- public object GetHead()
- {
- if (IsEmpty())
- {
- Console.WriteLine("队列为空!");
- return null;
- }
- return data[front + 1];
- }
- }
- class Test
- {
- static void Main()
- {
- Queue q = new Queue(5);
- int rdNum;
- Random rd = new Random();
- while (!q.IsFull())
- {
- rdNum = rd.Next(10, 100);
- q.In(rdNum);
- Console.WriteLine("{0}入队,队列元素个数:{1}", rdNum, q.GetQueueLength());
- }
- Console.WriteLine("***************************");
- while (!q.IsEmpty())
- {
- Console.WriteLine("{0}出队,队列元素个数:{1}", q.Out(), q.GetQueueLength());
- }
- }
- }
using System;
class Queue
{
object[] data; //数据元素
int maxsize; //最大容量
int front; //指向队头
int rear; //指向队尾
//初始化队列
public Queue(int size)
{
this.maxsize = size;
data = new object[size];
front = rear = -1;
}
//最大容量属性
public int MaxSize
{
get
{
return this.maxsize;
}
set
{
this.maxsize = value;
}
}
//队尾属性
public int Rear
{
get
{
return this.rear;
}
}
//队头属性
public int Front
{
get
{
return this.front;
}
}
//数据属性
public object this[int index]
{
get
{
return data[index];
}
}
//获得队列的长度
public int GetQueueLength()
{
return rear - front;
}
//判断队列是否满
public bool IsFull()
{
if (GetQueueLength() == maxsize)
return true;
else
return false;
}
//判断队列是否为空
public bool IsEmpty()
{
if (rear == front)
return true;
else
return false;
}
//清空队列
public void ClearQueue()
{
rear = front = -1;
}
//入队
public void In(object e)
{
if (IsFull())
{
Console.WriteLine("队列已满!");
return;
}
data[++rear] = e;
}
//出队
public object Out()
{
if (IsEmpty())
{
Console.WriteLine("队列为空!");
return null;
}
if (rear - front > 0)
{
object tmp = data[++front];
return tmp;
}
else
{
Console.WriteLine("全出队了!");
ClearQueue();
return null;
}
}
//获得队头元素
public object GetHead()
{
if (IsEmpty())
{
Console.WriteLine("队列为空!");
return null;
}
return data[front + 1];
}
}
class Test
{
static void Main()
{
Queue q = new Queue(5);
int rdNum;
Random rd = new Random();
while (!q.IsFull())
{
rdNum = rd.Next(10, 100);
q.In(rdNum);
Console.WriteLine("{0}入队,队列元素个数:{1}", rdNum, q.GetQueueLength());
}
Console.WriteLine("***************************");
while (!q.IsEmpty())
{
Console.WriteLine("{0}出队,队列元素个数:{1}", q.Out(), q.GetQueueLength());
}
}
}
运行结果:
23入队,队列元素个数:1
85入队,队列元素个数:2
28入队,队列元素个数:3
94入队,队列元素个数:4
55入队,队列元素个数:5
***************************
23出队,队列元素个数:4
85出队,队列元素个数:3
28出队,队列元素个数:2
94出队,队列元素个数:1
55出队,队列元素个数:0
请按任意键继续. . .
使用C#实现顺序队列的更多相关文章
- 顺序队列C/C++实现
#include <iostream> using namespace std; const int MAXSIZE = 1000; typedef int ELEMTYPE; const ...
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...
- 顺序队列之C++实现
下面介绍下用C++实现的顺序队列,在VC6下调试通过. 1.文件组织形式 2.sq.h顺序队列类的说明 #ifndef _SQ_H_ #define _SQ_H_ typedef int dataTy ...
- Javascript数据结构与算法--队列(顺序队列、优先队列、循环队列)的实现与用法
前言 队列和栈非常类似,前面已经讲过了栈的实现与用法,现在我们来说说队列. 队列介绍 队列遵循FIFO(First In First Out,先进先出)原则的一组有序的项. 队列是一种特殊的线性表,特 ...
- java 实现简单的顺序队列
package com.my; import java.util.Arrays; /** * 顺序队列 * @author wanjn * */ public class ArrayQueue { p ...
- C语言顺序队列
顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...
- java代码实现顺序队列
java实现顺序队列 package xianxinTable; import java.util.ArrayList; import java.util.Iterator; import com.s ...
- 数据结构 - 顺序队列的实行(C语言)
数据结构-顺序队列的实现 1 顺序队列的定义 线性表有顺序存储和链式存储,队列作为一种特殊的线性表,也同样存在这两种存储方式.我们先来看队列的顺序存储结构. 队列的顺序储存结构:用数组存储队列,为了避 ...
- 数据结构之顺序队列(C实现)
一.队列是什么 队列是一种可以实现“先进先出”的存储结构. 队列通常可以分为两种类型: 一.顺序队列,采用顺序存储,当长度确定时使用. 顺序队列又有两种情况: ①使用数组存储队列的称为静态顺序队列. ...
- 数据结构----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...
随机推荐
- MFC调试小技巧
今天看acl源码的时候看到一个函数AllocConsole().百度一下感觉这个函数对于调试非常不错,当然对于MFC里面的调试信息,我都是用TRACE打印自己感兴趣的消息的,而且仅仅有在DEBUG里面 ...
- jquery 判断当前上传文件大小限制上传格式 搭配thinkphp实现上传即预览(模拟异步上传)
在web开发中,最纠结的一项就是文件上传,最近由于项目需要前后摸索了四天在这里分享给大家.如有不足,望指出!! 前台:jquery.easyui.html 后台:thinkphp 主要涉及语言:jqu ...
- Search Bars(一个)
A search bar provides an interface for text-based searches with a text box and buttons such as searc ...
- 图表引擎AChartEngine 二
这几天项目涉及到android图表绘制,网上找的Demo都是大同小异,也就如上篇博文所写的一样是基本函数的实现.可是所有能找到的Demo都和设计图不符.木有办法,Leader发话啦,万不得已的情况下不 ...
- System.Threading.ThreadStateException
异常:"System.Threading.ThreadStateException"在未处理的异常类型 System.Windows.Forms.dll 发生 其它信息: 在能够调 ...
- TI C66x DSP 系统events及其应用 - 5.8(ISTP)
中断服务表指针ISTP(Interrupt Service Table Pointer)位置寄存器用于定位的中断服务例程,那ISTP去哪里找要运行的程序,ISTP(当中的ISTB字段)就是指向IST表 ...
- STL源代码剖析(一) - 内存分配
Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...
- ASP.NET分页正品—分页真
承接上篇博文<ASP.NET真假分页-假分页>:http://blog.csdn.net/u010773667/article/details/38845009,继续解说ASP.NE ...
- 文章之间的基本总结:Activity生命周期
孔子:温故而知新.它可以作为一个教师.<论语> 同样的学习技巧.对于技术文件或书籍的经典技术,期待再次看到它完全掌握,这基本上是不可能的,所以,我们常常回来几次,然后仔细研究,为了理解作者 ...
- 【Swift】学习笔记(四)——设置(Collection)
Swift和其他语言也提供了两种类型的集合:数组和字典 数组:数组用来按顺序存储同样类型的数据,swift规定它是类型安全的,每个数组都有自己的类型也就是其它语言所说的泛型. 创建数组: 1.var ...