고정배열을 구현한 Queue

Algorithm 2020. 10. 7. 18:03
반응형
using System;
using System.Collections.Generic;

namespace test_01
{
  public class App
  {
    //생성자
    public App()
    {
      // for (int i = 0; i < 11; i++)
      // {
      //   Console.WriteLine(i % 10);
      // }

      Queue queue = new Queue();

      queue.Enqueue(1);
      queue.Enqueue(2);

      var data = queue.Dequeue();
      Console.WriteLine("Dequeue: " + data);

      queue.Enqueue(3);

      queue.Print();

      // queue.Enqueue(4);
      // queue.Enqueue(5);
      // queue.Enqueue(6);
      // queue.Enqueue(7);
      // queue.Enqueue(8);
      // queue.Enqueue(9);
      // queue.Enqueue(10);
      // queue.Enqueue(11);

      // var data = queue.Dequeue();
      // Console.WriteLine("Dequeue: " + data);

      // data = queue.Dequeue();
      // Console.WriteLine("Dequeue: " + data);

      // data = queue.Dequeue();
      // Console.WriteLine("Dequeue: " + data);
      // queue.EnQueue(3);
      // queue.EnQueue(4);


    }
  }
}
using System;

namespace test_01
{
  public class Queue
  {
    private int size;
    private int front;
    private int rear;
    private object[] arr;

    //생성자 
    public Queue(int size = 10)
    {
      this.size = size;
      this.front = 0;
      this.rear = -1;
      this.arr = new object[this.size];
    }

    public void Enqueue(object item)
    {
      //check full
      if (this.rear == this.size - 1)
      {
        throw new ApplicationException("Full");
      }
      this.arr[++this.rear] = item;
      Console.WriteLine("Enqueue: " + item);
    }

    public object Dequeue()
    {
      //check empty
      if (this.front > this.rear)
      {
        throw new ApplicationException("Empty");
      }
      return this.arr[front++];
    }

    public void Print()
    {
      foreach (var item in this.arr)
      {
        Console.WriteLine("-> {0}", item);
      }
    }

  }
}
반응형

'Algorithm' 카테고리의 다른 글

선택정렬 (Selection Sort) C#  (0) 2020.10.23
selection sort in c#  (0) 2020.10.14
원형배열로 구현한 Queue  (0) 2020.10.07
C#으로 구현한 링크드리스트  (0) 2020.04.16
c# | 백준 | 2953  (0) 2019.10.18
: