1: public class SequenceQueue<T> : IQueue<T> {
2: private const int Step = 16;
3: private int _capacity;
4: private int _front = -1;
5: private int _rear = -1;
6: private T[] _datas;
7:
8: public SequenceQueue() : this(Step) { }
9:
10: public SequenceQueue(int capacity) {
11: this._capacity = capacity;
12: this._datas = new T[this._capacity];
13: }
14:
15: public int Count {
16: get {
17: if(this._front==-1 && this._rear==-1) {
18: return 0;
19: }
20: if(this._front <= this._rear) {
21: return this._rear - this._front + 1;
22: }
23: return this._datas.Length - this._front + this._rear + 1;
24: }
25: }
26:
27: public void Clear() {
28: this._front = -1;
29: this._rear = -1;
30: }
31:
32: public void Add(T value) {
33: if(this.Count==this._datas.Length) {
34: this._capacity += Step;
35: T[] temps = new T[this._capacity];
36: for(int i = 0; i < this._datas.Length; i++) {
37: temps[i] = this.Dequeue();
38: }
39: this._datas = temps;
40: this._front = 0;
41: this._rear = this._datas.Length - 1;
42: }
43: if(this._rear+1==this._datas.Length) {
44: this._rear = 0;
45: }
46: else {
47: this._rear++;
48: }
49: this._datas[this._rear] = value;
50: }
51:
52: public T Dequeue() {
53: if(this.Count==0) {
54: throw new Exception("Queue is Empty");
55: }
56: T result = this._datas[this._front];
57: if(this.Count==1) {
58: this._front = -1;
59: this._rear = -1;
60: }
61: else {
62: if(this._front+1==this._datas.Length) {
63: this._front = 0;
64: }
65: else {
66: this._front++;
67: }
68: }
69: return result;
70: }
71:
72: public T Peek() {
73: if(this.Count == 0) {
74: throw new Exception("Queue is Empty");
75: }
76: return this._datas[this._front];
77: }
78: }