System.Collections.Generic.Queue を使った例:
C#
1using System;
2using System.Collections.Generic;
3
4class BoundedQueue<T> {
5 private Queue<T> body_ = new Queue<T>();
6 private int size_; // 要素数の上限
7 public BoundedQueue(int size) { size_ = size; }
8 public void Enque(T data) {
9 // 末尾に挿入ののち、size_を越えないように先頭から削除する
10 body_.Enqueue(data);
11 while ( body_.Count > size_ ) body_.Dequeue();
12 }
13 public void print() {
14 foreach ( T item in body_) Console.Write("{0} ", item);
15 Console.WriteLine();
16 }
17}
18
19/* おためし */
20public class Program {
21 public static void Main() {
22 BoundedQueue<int> bq = new BoundedQueue<int>(3);
23 for ( int i = 0; i < 10; ++i ) {
24 bq.Enque(i);
25 bq.print();
26 }
27 }
28}
29
30/* 実行結果
310
320 1
330 1 2
341 2 3
352 3 4
363 4 5
374 5 6
385 6 7
396 7 8
407 8 9
41*/
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。