当前位置:
首页 > Python基础教程 >
-
C#教程之实现 数据结构 链队列
namespace _003_队列
{
interface IQueueDS
{
int Count// count属性
{
get;
}
int GetLength();//取得队长度
void Enqueue(T item);//入队
T Dequeue();//出队
T Peek();//取值
void Clear();//清空
bool IsEmpty();//判断队是否为空
}
}

namespace _003_队列
{
class Node<T>
{
private T data; //节点的值
private Node<T> next; //节点的引用
public Node(T data) //构造
{
this.data = data;
Next = null;
}
public T Data //节点的值的属性
{
get { return data; }
set { data = value; }
}
public Node<T> Next //节点的引用的属性
{
get{ return next; }
set{ next = value; }
}
}
}

namespace _003_队列
{
class LinkQueue:IQueueDS
{
private Node front; //头节点
private Node rear; //尾节点
private int count; //记录队列中数据个数
public LinkQueue() //构造
{
front = null;
rear = null;
count = 0;
}
public int Count { get { return count; } } //属性
public int GetLength() //取得队列数据个数方法
{
return count;
}
public void Enqueue(T item) //入队
{
Node newNode = new Node(item);
if (count == 0) //空队时入队,首尾节点都指向新节点
{
front = newNode;
rear = newNode;
count = 1;
}
else //非空队入队,首节点无需操作,尾节点指向新节点
{
rear.Next = newNode;
rear = newNode;
count++;
}
}
public T Dequeue() //出队
{
T temp = front.Data; //temp 存头节点值
if (count == 0) //空队无法出队
{
Console.WriteLine("空队无法出队!!!!!");
return default(T);
}
else if (count == 1) //队列仅有的一个数据时,出队后,首尾节点都置空(null)
{
front = rear = null;
count = 0;
return temp;
}
else //队列有一个以上数据时,出队后,头节点指向下一个节点
{
front = front.Next;
count--;
return temp;
}
}
public T Peek() //取得头节点值得方法
{
if (front != null) //非空队时直接返回头节点值
{
return front.Data;
}
else //空队时返回该类型默认值
{
Console.WriteLine("空队无法Peek!!!!!!!!");
return default(T);
}
}
public void Clear() //清空
{
count = 0;
front = rear = null;
}
public bool IsEmpty() //判断是否为空
{
return count == 0;
}
}
}



namespace _003_队列
{
class Program
{
static void Main(string[] args)
{
//Queue testQueue = new Queue(); //1.使用BCL中的队
//SeqQueue testQueue = new SeqQueue(); //2.使用自己编写的顺序队
LinkQueue testQueue = new LinkQueue(); //3.使用自己编写的链队
Console.WriteLine(testQueue.IsEmpty());//true
testQueue.Enqueue(1);
testQueue.Enqueue(2);
testQueue.Enqueue(3);
Console.WriteLine("1,2,3,入队后,队中数据个数是" + testQueue.Count);//3
Console.WriteLine(testQueue.IsEmpty());//false
int i = testQueue.Dequeue();
Console.WriteLine(i + "出队了");//1
Console.WriteLine("1出队之后,队中数据个数是" + testQueue.Count);//2
int k = testQueue.Dequeue();
Console.WriteLine(k + "出队了");//2
Console.WriteLine("2出队之后,队中数据个数是" + testQueue.Count);//1
int j = testQueue.Peek();
Console.WriteLine(j + "peek 了");//3
Console.WriteLine("3peek之后,队中数据个数是" + testQueue.Count);//1
testQueue.Clear();
Console.WriteLine("Clear 之后队中数据个数是" + testQueue.Count);//0
Console.ReadKey();
{
interface IQueueDS
{
int Count// count属性
{
get;
}
int GetLength();//取得队长度
void Enqueue(T item);//入队
T Dequeue();//出队
T Peek();//取值
void Clear();//清空
bool IsEmpty();//判断队是否为空
}
}

namespace _003_队列
{
class Node<T>
{
private T data; //节点的值
private Node<T> next; //节点的引用
public Node(T data) //构造
{
this.data = data;
Next = null;
}
public T Data //节点的值的属性
{
get { return data; }
set { data = value; }
}
public Node<T> Next //节点的引用的属性
{
get{ return next; }
set{ next = value; }
}
}
}

namespace _003_队列
{
class LinkQueue:IQueueDS
{
private Node front; //头节点
private Node rear; //尾节点
private int count; //记录队列中数据个数
public LinkQueue() //构造
{
front = null;
rear = null;
count = 0;
}
public int Count { get { return count; } } //属性
public int GetLength() //取得队列数据个数方法
{
return count;
}
public void Enqueue(T item) //入队
{
Node newNode = new Node(item);
if (count == 0) //空队时入队,首尾节点都指向新节点
{
front = newNode;
rear = newNode;
count = 1;
}
else //非空队入队,首节点无需操作,尾节点指向新节点
{
rear.Next = newNode;
rear = newNode;
count++;
}
}
public T Dequeue() //出队
{
T temp = front.Data; //temp 存头节点值
if (count == 0) //空队无法出队
{
Console.WriteLine("空队无法出队!!!!!");
return default(T);
}
else if (count == 1) //队列仅有的一个数据时,出队后,首尾节点都置空(null)
{
front = rear = null;
count = 0;
return temp;
}
else //队列有一个以上数据时,出队后,头节点指向下一个节点
{
front = front.Next;
count--;
return temp;
}
}
public T Peek() //取得头节点值得方法
{
if (front != null) //非空队时直接返回头节点值
{
return front.Data;
}
else //空队时返回该类型默认值
{
Console.WriteLine("空队无法Peek!!!!!!!!");
return default(T);
}
}
public void Clear() //清空
{
count = 0;
front = rear = null;
}
public bool IsEmpty() //判断是否为空
{
return count == 0;
}
}
}



namespace _003_队列
{
class Program
{
static void Main(string[] args)
{
//Queue testQueue = new Queue(); //1.使用BCL中的队
//SeqQueue testQueue = new SeqQueue(); //2.使用自己编写的顺序队
LinkQueue testQueue = new LinkQueue(); //3.使用自己编写的链队
Console.WriteLine(testQueue.IsEmpty());//true
testQueue.Enqueue(1);
testQueue.Enqueue(2);
testQueue.Enqueue(3);
Console.WriteLine("1,2,3,入队后,队中数据个数是" + testQueue.Count);//3
Console.WriteLine(testQueue.IsEmpty());//false
int i = testQueue.Dequeue();
Console.WriteLine(i + "出队了");//1
Console.WriteLine("1出队之后,队中数据个数是" + testQueue.Count);//2
int k = testQueue.Dequeue();
Console.WriteLine(k + "出队了");//2
Console.WriteLine("2出队之后,队中数据个数是" + testQueue.Count);//1
int j = testQueue.Peek();
Console.WriteLine(j + "peek 了");//3
Console.WriteLine("3peek之后,队中数据个数是" + testQueue.Count);//1
testQueue.Clear();
Console.WriteLine("Clear 之后队中数据个数是" + testQueue.Count);//0
Console.ReadKey();
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式