본문으로 바로가기

단순 overflow 큐

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




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 i)
        {
            if(_arr.Length <= _rear)
            {
                Console.WriteLine("Full");
                Environment.Exit(-1);
                //return;
            }
            _arr[_rear] = i;
            _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.print();
        }
    }
}
 
cs








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

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