c#队列的实现
2015-02-04 02:00
176 查看
/*
用一片连续的存储空间来存储队列中的数据元素,这样的队列称为顺序队列
(Sequence Queue)。类似于顺序栈,在这里我就不做介绍了,我们直接用列表实现一个队列
*/
/*
队列的应用举例
编程判断一个字符串是否是回文。回文是指一个字符序列以中间字符
为基准两边字符完全相同,如字符序列“ACBDEDBCA”是回文
*/
好了,今天就到这里了
用一片连续的存储空间来存储队列中的数据元素,这样的队列称为顺序队列
(Sequence Queue)。类似于顺序栈,在这里我就不做介绍了,我们直接用列表实现一个队列
*/
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 数据结构Linked_List { public class LinkQueue<T> { private Node<T> front;//队列头指示器 internal Node<T> Front { get { return front; } set { front = value; } } private Node<T> rear;//队列尾指示器 internal Node<T> Rear { get { return rear; } set { rear = value; } } private int nCount;//队列结点个数 public int NCount { get { return nCount; } set { nCount = value; } } public LinkQueue() { front = rear = null; nCount = 0; } public int GetLength() { return nCount; } public void Clear() { front = rear = null; nCount = 0; } public bool IsEmpty() { if ( front == rear && 0 == nCount ) { return true; } return false; } public void Enqueue( T item ) { Node<T> p = new Node<T>(item); if ( IsEmpty() ) { front = rear = p;// 这步很重要 当第一个元素入队的时候,必须给front赋值,否则front一直都是null } else { rear.Next = p; rear = p; } ++nCount; } public T Dqueue() { if ( IsEmpty() ) { Console.WriteLine("队列为空"); return default(T); } Node<T> p = front;//从队列头出对 front = front.Next; if ( front == null ) { rear = null; } --nCount; return p.Data; } //获取链队列头结点的值 public T GetFront() { if (IsEmpty()) { Console.WriteLine("队列为空"); return default(T); } return front.Data; } } }
/*
队列的应用举例
编程判断一个字符串是否是回文。回文是指一个字符序列以中间字符
为基准两边字符完全相同,如字符序列“ACBDEDBCA”是回文
*/
public static bool IsPalindromic() { SeqStack<char> stack = new SeqStack<char>(50); LinkQueue<char> queue = new LinkQueue<char>(); //string str = Console.ReadLine();// 这里随便输入 string str = "ACBDEDBCA";// 测试 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 >> 1; ++i ) { if (queue.Dqueue() == stack.Pop()) { flag = true; } else { flag = false; } } return flag; }
好了,今天就到这里了
相关文章推荐
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- 用 C# 实现的一个队列源代码
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- 队列的顺序存储结构_C#实现1[ArrayList实现]
- 使用微软消息队列实现C#进程间通信(三)
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- [转]使用微软消息队列实现C#进程间通信
- c#实现队列和栈
- C#实现循环顺序队列(队列)
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- c#实现队列(Queue)
- 使用微软消息队列实现C#进程间通信
- 使用微软消息队列实现C#进程间通信(转)
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- 基于最小二叉堆的优先级队列-C#实现,以此为基础的K路合并排序算法
- 使用微软消息队列实现C#进程间通信(二)
- 基于无锁的C#并发队列实现
- 使用微软消息队列实现C#进程间通信(一)
- C#定时任务采用线程和队列实现