队列为循环队列,使用循环队列的原因是:防止假溢出。
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![]()
5
namespace 队列应用
6
{
7
class Program
8
{
9
public interface IQueue<T>
10
{
11
//求队列的长度
12
int GetLength();
13
//判断队列是否为空
14
bool IsEmpty();
15
//清空队列
16
void Clear();
17
//入队
18
void In(T item);
19
//出队
20
T Out();
21
//取对头元素
22
T GetFront();
23
}
24
public class SeqQueue<T> : IQueue<T>
25
{
26
//循环顺序队列的容量
27
private int maxsize;
28
//存储顺序队列中的值
29
private T[] data;
30
//队头
31
private int front;
32
//队尾
33
private int rear;
34![]()
35
//索引器
36
public T this[int index]
37
{
38
get
39
{
40
return data[index];
41
}
42
set
43
{
44
data[index] = value;
45
}
46
}
47
//容量属性
48
public int Maxsize
49
{
50
get
51
{
52
return maxsize;
53
}
54
set
55
{
56
maxsize = value;
57
}
58
}
59
//队头属性
60
public int Front
61
{
62
get
63
{
64
return front;
65
}
66
set
67
{
68
front = value;
69
}
70
}
71
//队尾属性
72
public int Rear
73
{
74
get
75
{
76
return rear;
77
}
78
set
79
{
80
rear = value;
81
}
82
}
83
//构造器
84
public SeqQueue(int size)
85
{
86
data=new T[size];
87
maxsize = size;
88
front = rear = -1;
89
}
90
//求长度
91
public int GetLength()
92
{
93
return (rear - front + maxsize) % maxsize;
94
}
95
//清空
96
public void Clear()
97
{
98
front = rear = -1;
99
}
100
//判断队列是否为空
101
public bool IsEmpty()
102
{
103
if (front == rear)
104
{
105
return true;
106
}
107
else
108
{
109
return false;
110
}
111
112
}
113
//判断队列是否已满
114
public bool IsFull()
115
{
116
if ((rear + 1) % maxsize == front)
117
{
118
return true;
119
}
120
else
121
{
122
return false;
123
}
124
}
125
//入队
126
public void In(T item)
127
{
128
if (IsFull())
129
{
130
Console.WriteLine("队列已满!");
131
return;
132
}
133
data[++rear] = item;
134
}
135
//出队
136
public T Out()
137
{
138
T tmp=default(T);
139
if (IsEmpty())
140
{
141
Console.WriteLine("队列是空的!");
142
return tmp;
143
}
144
tmp=data[++front];
145
return tmp;
146
}
147![]()
148
//获取队头元素
149
public T GetFront()
150
{
151
if (IsEmpty())
152
{
153
Console.WriteLine("队列是空的!");
154
return default(T);
155
}
156
return data[front+1];
157
}
158
}
159
static void Main(string[] args)
160
{
161
SeqQueue<int> queue = new SeqQueue<int>(10);
162
int[] str = new int[] {1,2,3,4,5,6};
163
for (int i = 0; i < str.Length; i++)
164
{
165
queue.In(str[i]);
166
}
167
Console.WriteLine(queue.GetFront());
168
}
169
}
170
}
171![]()

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

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171
