C#实现顺序队列和链队列的代码实例
程序员文章站
2023-12-15 12:25:40
和基本是一个思路:
废话不多说,直接写代码吧
//自定义队列接口
namespace 队列
{
interface iqueue...
和基本是一个思路:
废话不多说,直接写代码吧
//自定义队列接口 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(); } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接