博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构-循环顺序队列&链队列
阅读量:4362 次
发布时间:2019-06-07

本文共 7337 字,大约阅读时间需要 24 分钟。

 

队列接口实现:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _003_队列{    interface IQueue
{ int Count { get; } int GetLength(); bool IsEmpty(); void Clear(); void Enqueue(T item); T Dequeue(); T Peek(); }}

 

顺序循环队列实现:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _003_队列{    ///     /// 顺序队列    ///     /// 
class SeqQueue
: IQueue
{ private T[] data; private int count;//当前元素数量 private int front;//队首 (队首元素索引-1) private int rear;//队尾 (队尾元素索引) public SeqQueue(int size) { data = new T[size]; count = 0; front = -1; rear = -1; } public SeqQueue() : this(10) { } public int Count { get { return count; } } public void Clear() { count = 0; front = rear = -1; } public int GetLength() { return count; } public bool IsEmpty() { return count == 0; } ///
/// 出队,并删除数据 /// ///
public T Dequeue() { if (!IsEmpty()) { T temp = data[front + 1]; front++; count--; return temp; } else { Console.WriteLine("队列为空"); return default(T); } } ///
/// 入队 /// ///
public void Enqueue(T item) { if (count == data.Length) { Console.WriteLine("队列已满,不可以再添加新的数据"); } else { if (rear == data.Length - 1) //判断是否在尾部,是就把元素放在0位置 { data[0] = item; rear = 0; } else//不是就继续添加 { data[rear + 1] = item; rear++; } count++; } } //取得队首数据 public T Peek() { if (IsEmpty()) { return default(T); } else { return data[front + 1]; } } }}

 

链结点:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _003_队列{    class Node
{ private T data; private Node
next; public Node(T data) { this.data = data; } public T Data { get { return data; } set { data = value; } } public Node
Next { get { return next; } set { next = value; } } }}

 

 

链队列实现:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _003_队列{    ///     /// 链队列    ///     /// 
class LinkQueue
: IQueue
{ 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 bool IsEmpty() { return count == 0; } public void Clear() { count = 0; front = null; rear = null; } ///
/// 出队 /// ///
public T Dequeue() { if (IsEmpty()) { Console.WriteLine("队列为空,无法出队"); return default(T); } else if (count == 1) //等于1 说明 头和尾一样 { T temp = front.Data; front = rear = null; count = 0; return temp; } else //直接删除头 头变成下一个结点 { T temp = front.Data; front = front.Next; count--; return temp; } } ///
/// 入队 /// ///
public void Enqueue(T item) { Node
newNode = new Node
(item); if (IsEmpty()) { front =rear = newNode; } else { rear.Next = newNode; rear = newNode; } count++; } public T Peek() { if (front!=null) { return front.Data; } else { return default(T); } } }}

 

栈和队列的应用

///         /// 使用栈和队列 判断是否为回文        ///         /// 
public static bool IsPalindrome(string temp) { Stack
stack = new Stack
();//先进后出 LinkQueue
queue = new LinkQueue
();//先进先出 string str = temp; //进栈 进队 for (int i = 0; i < str.Length; i++) { stack.Push(str[i]); queue.Enqueue(str[i]); } //判断前后是否一样 bool flag = false; for (int i = 0; i < str.Length/2; i++) { if (queue.Dequeue() == stack.Pop()) { flag = true; } else { flag = false; } } return flag; }

 

///         /// 变大写        ///         ///         public static void ToUpper(string s)        {            Queue
que = new Queue
(); char w; for (int i = 0; i < s.Length; i++) { if(s[i] > 96 && s[i] < 123) { w = Convert.ToChar(s[i] - 32); } else { w = s[i]; } que.Enqueue(w); } for (int i = 0; i < s.Length; i++) { Console.Write(que.Dequeue()); } } ///
/// 变小写 /// ///
public static void ToLower(string s) { Queue
que = new Queue
(); char w; for (int i = 0; i < s.Length; i++) { if (s[i] > 64 && s[i] < 91) { w = Convert.ToChar(s[i] + 32); } else { w = s[i]; } que.Enqueue(w); } for (int i = 0; i < s.Length; i++) { Console.Write(que.Dequeue()); } }

 

转载于:https://www.cnblogs.com/rongweijun/p/8093230.html

你可能感兴趣的文章
java.util.zip.ZipException: duplicate entry(重复依赖多版本的类库)
查看>>
Run MVC in older version of IIS
查看>>
Ajax 监听
查看>>
隐藏"站长统计"图标
查看>>
Oracle select 中case 的使用以及使用decode替换case
查看>>
创建一个dynamics 365 CRM online plugin (十二) - Asynchronous Plugins
查看>>
Eclipse 常用快捷键 (动画讲解)
查看>>
233 Matrix(矩阵快速幂+思维)
查看>>
Leetcode-Unique Binary Search Trees II
查看>>
Centos7系统下安装Docker
查看>>
PostgreSQL 序列(SEQUENCE)
查看>>
Missing Number
查看>>
Ionic3 demo TallyBook 实例3
查看>>
laravel服务容器
查看>>
Entity Framework的查询
查看>>
ZH奶酪:Python按行读取文件
查看>>
07-使用循环进行遍历数组(运算符)
查看>>
控件布局通用解决方案
查看>>
scala流程控制语句以及方法和函数
查看>>
MySQL的sql_mode模式
查看>>