[C#] 큐 FIFO

반응형

 

- FIFO, 선입 선출 방식

 

- enqueue : Queue 객체를 추가

 

- dequeue : Queue 객체를 제거

 

- Count : 객체의 갯수

 

- Clear : 모든 객체 제거

 

 

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace queue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Queue<string> myQueue = new Queue<string>();
            myQueue.Enqueue("첫째 줄");
            myQueue.Enqueue("둘째 줄");
            myQueue.Enqueue("셋째 줄");
            myQueue.Enqueue("마지막 줄");
 
            string takeALook = myQueue.Peek(); // peek() 메서드로 큐의 첫 번째 아이템을 제거하지 않은 상태에서 들여다 볼 수 있습니다.
            string getFirst = myQueue.Dequeue(); //FiFo return
            string getNext = myQueue.Dequeue();
 
            int howMany = myQueue.Count;
            myQueue.Clear();
 
            MessageBox.Show("Peek() returned : " + takeALook + "\n"
                + "The first Dequeue()  returned " + getFirst + "\n"
                + "The second Dequeue()  returned " + getNext + "\n"
                + "Count before Clear() was " + howMany + "\n"
                + "Count after Clear is now " + myQueue.Count);
 
        }
 
    }
}
 
cs

 

728x90
반응형