和上篇栈的实现基本是一个思路:
废话不多说,直接写代码吧
//自定义队列接口 namespace 队列 { interface IQueue<T> { int Count { get; } int GetLength(); bool IsEmpty(); void Clear(); void Enqueue(T item); T Dequeue(); T Peek(); } }
//顺序队列的实现类 namespace 队列 { class SeqQueue<T> : IQueue<T> { 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() { data = new T[10]; count = 0; front = -1; rear = -1; } public int Count { get { return count; } } public void Clear() { count = 0; front = -1; rear = -1; } public T Dequeue() { if(count > 0) { 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) //最后一个位置是否有值 { data[0] = item; rear = 0; count++; } else { data[rear + 1] = item; rear += 1; count++; } } } public int GetLength() { return count; } public bool IsEmpty() { return count == 0; } public T Peek() { T temp = data[front + 1]; return temp; } } }
//链队列的节点类 namespace 队列 { class Node<T> { private T data; private Node<T> next; public Node(T data) { this.data = data; } public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } } }
//链队列的实现类 namespace 队列 { class LinkQueue<T> : IQueue<T> { private Node<T> front; //头结点 private Node<T> rear; //尾结点 private int count; //表示元素个数 public LinkQueue() { front = null; rear = null; count = 0; } public int Count { get { return count; } } public void Clear() { front = null; rear = null; count = 0; } public T Dequeue() { if (count == 0) { Console.WriteLine("队列为空"); return default(T); }else if(count == 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<T> newNode = new Node<T>(item); if (count == 0) { front = newNode; rear = newNode; count = 1; } else { rear.Next = newNode; rear = newNode; count++; } } public int GetLength() { return count; } public bool IsEmpty() { return count == 0; } public T Peek() { if (front != null) { return front.Data; } else { return default(T); } } } }
//以上两个实现类的测试类 namespace 队列 { class Program { static void Main(string[] args) { //CRL中的队列 //Queue<int> queue = new Queue<int>(); //自己的写的顺序队列 //SeqQueue<int> queue = new SeqQueue<int>(); //自己的链队列 LinkQueue<int> queue = new LinkQueue<int>(); queue.Enqueue(12); queue.Enqueue(34); queue.Enqueue(56); queue.Enqueue(78); queue.Enqueue(90); Console.WriteLine("添加五个元素后的大小:"+ queue.Count); int i = queue.Dequeue(); //出队(取出并删除) Console.WriteLine("取出的元素是: " + i); Console.WriteLine("出队后队列大小:" + queue.Count); int j = queue.Peek(); Console.WriteLine("Peek取出的元素是: " + j); Console.WriteLine("PeeK后队列大小:" + queue.Count); queue.Clear(); Console.WriteLine("清空后的队列大小:" + queue.Count); Console.ReadKey(); } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对亿速云的支持。如果你想了解更多相关内容请查看下面相关链接
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。