和上篇栈的实现基本是一个思路:
废话不多说,直接写代码吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//自定义队列接口
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
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
|
//顺序队列的实现类
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
|
//链队列的节点类
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
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
|
//链队列的实现类
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
|
//以上两个实现类的测试类
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();
}
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/Czhenya/article/details/78077876