C#/C#200제

[C#] 15일차 - 100. Queue<T>의 구현

반나무 2021. 2. 13. 17:20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A100_QueueImplementation
{
    // T형 변수 value와 다음 노드를 가리키는 레퍼런스 next를 갖습니다.
    class Node<T>
    {
        internal T value;
        internal Node<T> next;

        // 생성자 메소드. 
        public Node (T value)
        {
            this.value = value;
            this.next = null;
        }
    }

    // 
    class MyQueue<T>
    {
        internal Node<T> first = null;
        internal Node<T> last = null;

        internal void EnQueue(Node<T> node)
        {
            if (last == null)
                first = last = node;
            else
            {
                last.next = node;
                last = node;
            }
        }

        internal T DeQueue()
        {
            if(first == null)
            {
                Console.WriteLine("Queue Empty");
                return default(T);

            } else
            {
                T value = first.value;
                first = first.next;
                return value;
            }
        }

        internal void Print()
        {
            for (Node<T> t = first; t != null; t = t.next)
                Console.Write(t.value + " -> ");
            Console.WriteLine();
        }
    }
}
반응형