C# 代码实现 顺序队列和链队列
【摘要】 和上篇栈的实现基本是一个思路: 废话不多说,直接写代码吧,,,
//自定义队列接口
namespace 队列
{ interface IQueue<T> { int Count { get; } int GetLength(); bool IsEmpty(); void Clear(); void Enqueue(T item); T Dequeue()...
和上篇栈的实现基本是一个思路:
废话不多说,直接写代码吧,,,
//自定义队列接口
namespace 队列
{ interface IQueue<T> { int Count { get; } int GetLength(); bool IsEmpty(); void Clear(); void Enqueue(T item); T Dequeue(); T Peek(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
//顺序队列的实现类
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; } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
//链队列的节点类
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; } } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
//链队列的实现类
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); } } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
//以上两个实现类的测试类
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(); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/78077876
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)