概述

看了一个数据结构的教程,是用C++写的,可自己C#还是一个菜鸟,更别说C++了,但还是大胆尝试用C#将其中的环形队列的实现写出来,先上代码:

     public class MyQueue<T> : IDisposable
{
private T[] queue;
private int length;
private int capacity;
private int head = ;
private int tail = ; public MyQueue(int capacity) {
this.capacity = capacity;
this.head = ;
this.tail = ;
this.length = ;
this.queue = new T[capacity];
} public void Clear() {
head = ;
tail = ;
length = ;
} public bool IsEmpty() {
return length == ;
} public bool IsFull() {
return length == capacity;
} public int Length() {
return length;
} public bool EnQueue(T node) {
if (!IsFull()) {
queue[tail] = node;
tail = (++tail) % capacity;
length++;
return true;
}
return false;
} public T DeQueue() {
T node = default(T);
if (!IsEmpty()) {
node = queue[head];
head = (++head) % capacity;
length--;
}
return node;
} public void Traverse() {
for (int i = head; i < length + head; i++) {
Console.WriteLine(queue[i % capacity]);
Console.WriteLine($"前面还有{i - head}个");
}
} public void Dispose() {
queue = null;
}
}

为了能够通用,所以用的是泛型来实现环形队列类。这里最重要的是进队(EnQueue)和出队(DeQueue)两个方法,进队或出队后头和尾的位置都要通过取模运算来获得,因为是环形队列嘛,你懂的。

一、简单类型队列

好了,测试下入队:
     class Program
{
static void Main(string[] args) {
MyQueue<int> queue = new MyQueue<int>();
queue.EnQueue();
queue.EnQueue();
queue.EnQueue();
queue.EnQueue();
queue.Traverse();
Console.Read();
}
}
显示结果:

再测试下出队:
     class Program
{
static void Main(string[] args) {
MyQueue<int> queue = new MyQueue<int>();
queue.EnQueue();
queue.EnQueue();
queue.EnQueue();
queue.EnQueue();
queue.Traverse(); Console.WriteLine("弹两个出去");
queue.DeQueue();
queue.DeQueue();
Console.WriteLine();
queue.Traverse();
Console.Read();
}
}

运行结果:

 

二、复杂类型队列

之前也说了,这个队列类是用的泛型写的,对应于C++的模板了,那就意味着任何类型都可以使用这个队列类,来测试个自定义的类试试,如下先定义一个Customer类:
     public class Customer
{
public string Name { get; set; } public int Age { get; set; } public void PringInfo() {
Console.WriteLine("姓名:" + Name);
Console.WriteLine("年龄:" + Age);
Console.WriteLine();
}
}

然后进行入队,如下:

     class Program
{
static void Main(string[] args) {
MyQueue<Customer> queue = new MyQueue<Customer>();
queue.EnQueue(new Customer() { Name = "宋小二", Age = });
queue.EnQueue(new Customer() { Name = "陈小三", Age = });
queue.EnQueue(new Customer() { Name = "王小四", Age = });
queue.EnQueue(new Customer() { Name = "朱小五", Age = });
for (int i = ; i < queue.Length(); i++) {
queue[i].PringInfo();
}
Console.Read();
}
}

上面的代码 queue[i].PringInfo();是通过索引来实现,所以我们得在队列类中实现索引,添加如下代码到MyQueue.cs类中,如下:

         public T this[int index] {
get {
return queue[index];
}
}

感觉用for循环来遍历还是不够好,想用foreach,那就给MyQueue类加个遍历接口,如下:

然后实现这个接口,如下:
         public IEnumerator<T> GetEnumerator() {
foreach(T node in queue) {
if(node != null) {
yield return node;
}
}
} IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}

这样遍历的地方就可以改成foreach了,如下:

执行结果:
 

总结:

编程的思想才是最重要的,无关语言。
因为自己的水平有限,所以写的代码都很简单,如果哪写的不好或需要改进的,希望路过的高手不吝指教,如果能推荐就更完美了,谢谢阅读。
 
 

C#实现环形队列的更多相关文章

  1. 【转】C#环形队列

    概述 看了一个数据结构的教程,是用C++写的,可自己C#还是一个菜鸟,更别说C++了,但还是大胆尝试用C#将其中的环形队列的实现写出来,先上代码: 1 public class MyQueue< ...

  2. Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列

    Atitit.提升软件稳定性---基于数据库实现的持久化  循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...

  3. 队列(Queue)--环形队列、优先队列和双向队列

    1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...

  4. 环形队列C++实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下鄙人用C++实现了环形队列 /******************************** ...

  5. 数据结构-环形队列 C和C++的实现

    队列: 含义:是一种先入先出(FIFO)的数据结构. 当我们把数据一个一个放入队列中.当我们需要用到这些数据时,每次都从队列的头部取出第一个数据进行处理.就像排队进场一样,先排队的人先进场. 结构如下 ...

  6. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  7. ucos-iii串口用信号量及环形队列中断发送,用内建消息队列中断接收

    串口发送部分代码: //通过信号量的方法发送数据 void usart1SendData(CPU_INT08U ch) { OS_ERR err; CPU_INT08U isTheFirstCh; O ...

  8. 1-关于单片机通信数据传输(中断发送,大小端,IEEE754浮点型格式,共用体,空闲中断,环形队列)

    补充: 程序优化 为避免普通发送和中断发送造成冲突(造成死机,复位重启),printf修改为中断发送 写这篇文章的目的呢,如题目所言,我承认自己是一个程序猿.....应该说很多很多学单片机的对于... ...

  9. uvaoj 133 - The Dole Queue(逻辑,环形队列数数)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. Hadoop 2.x(YARN)安装配置LZO

    今天尝试在Hadoop 2.x(YARN)上安装和配置LZO,遇到了很多坑,网上的资料都是基于Hadoop 1.x的,基本没有对于Hadoop 2.x上应用LZO,我在这边记录整个安装配置过程 1. ...

  2. zk set 方法

    [root@wx03 zook]# cat a4.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; my $zk = ZooKeepe ...

  3. 流行的Python项目汇总

    年有哪些流行的Python项目呢?下面,我们一起来看下. 一.测试和调试 python_koans :Python Koans 算 “Ruby Koans” 的一部分,作为交互式教程,可以学习 TDD ...

  4. CUSPARSE 第三章 CUSPARAE索引和数据格式

    (纯属自学笔记,部分翻译,不会翻译的不翻译) 3.1 索引基本格式 该函数库支持 zero- and one-based 索引. The index base 是通过 cusparseIndexBas ...

  5. Debian为程序添加一个开始菜单,debian添加sublime开始菜单.

    下了一个 '绿色' 的程序,想要加到开始菜单里面. 怎么做呢? 我这里以sublime2做例 去http://www.sublimetext.com/2 下载了linux 64位, 解压放到了下面的文 ...

  6. Oracle 11gR2的完全卸载

    首先停止oracle服务,卸载oracle,其次删除oracle文件夹,最后删除oracle服务和清理注册表. 以下是详细教程 1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: ...

  7. JVM调优总结(七)-典型配置举例1

    以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理 ...

  8. 一步一步重写 CodeIgniter 框架 -- 原因和思路

    CodeIgniter 是一个非常轻量级的 PHP 框架,说是轻量级,最新版的代码只有不到2M. 其最重要的特点就是 MVC 模式来编写代码,如果大家看过一些用 PHP 来编写网站的书籍或教程,无一例 ...

  9. HNCU1741:算法3-2:行编辑程序

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1741 题目描述 一个简单的行编辑程序的功 ...

  10. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

    题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...