본문으로 바로가기

Growable Array 큐

category Programming/C# 2018. 11. 5. 16:42



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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Queue
{
    class Qu
    {
        static int MAX = 5;
        private int _front;
        private int _rear;
        private int [] _arr;
 
        public Qu()
        {
            _front = 0;
            _rear = 0;
            _arr = new int[MAX];
        }
 
        public void emptyQueue()
        {
            Console.WriteLine("Empty");
            Environment.Exit(-1);
            // return;
        }
 
        public void add(int num)
        {
            if(_arr.Length <= _rear)
            {
                int size = _arr.Length * 2;
                int [] temp = new int[size];
 
                for (int i = 0; i < _rear; i++)
                {
                    temp[i] = _arr[i];
                }
                _arr = temp;
            }
            _arr[_rear] = num;
            _rear++;
        }
 
        public int remove()
        {
            if(_front==_rear)
            {
                emptyQueue();
            }
 
            int val = _arr[_front];
 
            for(int i=1; i<_rear; i++)
            {
                _arr[i - 1= _arr[i];
            }
 
            _rear--;
            return val;
        }
 
        public void print()
        {
            for(int i=_front; i<_rear; i++)
            {
                Console.WriteLine(_arr[i]);
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Qu q1 = new Qu();
            q1.add(10);
            q1.add(30);
            q1.add(40);
 
            q1.remove();
 
            q1.add(80);
            q1.add(70);
 
            q1.remove();
 
            q1.add(30);
 
            q1.remove();
            q1.add(100);
 
            q1.add(100);
 
            q1.add(90);
            q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90); q1.add(90);
 
            q1.print();
        }
    }
}
 
cs






'Programming > C#' 카테고리의 다른 글

Linked List Queue  (0) 2018.11.05
단순 overflow 큐  (0) 2018.11.05
[C#] 파일 입출력 정리  (0) 2018.06.06