队列为循环队列,使用循环队列的原因是:防止假溢出。

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace 队列应用
  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
posted on 2009-03-11 20:39  甲乙丙丁  阅读(1029)  评论(0编辑  收藏  举报