본문 바로가기
프로그래밍/Algorithm

C# [백준 BAEKJOON] 10866번 덱

by bantomak 2024. 5. 28.

문제

정수를 저장하는 덱(Deque)를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 여덟 가지이다.

  • push_front X: 정수 X를 덱의 앞에 넣는다.
  • push_back X: 정수 X를 덱의 뒤에 넣는다.
  • pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • size: 덱에 들어있는 정수의 개수를 출력한다.
  • empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
  • front: 덱의 가장 앞에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • back: 덱의 가장 뒤에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.

 

입력

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

 

출력

출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.

 

 

풀이 코드

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

public partial class Program
{
    static void Main(string[] args)
    {
        var sw = new StreamWriter(Console.OpenStandardOutput());
        var sr = new StreamReader(Console.OpenStandardInput());

        var sb = new StringBuilder();

        var deque = new MyDeque();

        var input = Console.ReadLine();
        var count = Int32.Parse(input);

        for (int i = 1; i <= count; i++)
        {
            var input2 = Console.ReadLine().Split();
            switch (input2[0]) 
            {
                case "push_back": deque.PushBack(Int32.Parse(input2[1])); break;
                case "push_front": deque.PushFront(Int32.Parse(input2[1])); break;
                case "pop_front":
                    {
                        sb.AppendLine(deque.PopFront().ToString());
                        break;
                    }
                case "pop_back":
                    {
                        sb.AppendLine(deque.PopBack().ToString());
                        break;
                    }
                case "size":
                    {
                        sb.AppendLine(deque.Size().ToString());
                        break;
                    }
                case "empty":
                    {
                        sb.AppendLine(deque.IsEmpty() ? "1" : "0");
                        break;
                    }
                case "front":
                    {
                        if (deque.IsEmpty())
                        {
                            sb.AppendLine("-1");
                        }
                        else
                        {
                            sb.AppendLine(deque.Front().ToString());
                        }

                        break;
                    }
                case "back":
                    {
                        if (deque.IsEmpty())
                        {
                            sb.AppendLine("-1");
                        }
                        else
                        {
                            sb.AppendLine(deque.Back().ToString());
                        }

                        break;
                    }
                default: new InvalidEnumArgumentException(); break;
            }
        }

        sw.Write(sb.ToString());
        sw.Close();
        sr.Close();
    }

    public class MyDeque
    {
        private List<int> q;

        public MyDeque()
        {
            q = new List<int>();
        }

        public void PushFront(int item)
        {
            var newQueue = new int[q.Count + 1];
            newQueue[0] = item;

            q.CopyTo(newQueue, 1);
            q = newQueue.ToList();
        }

        public void PushBack(int item)
        {
            q.Add(item);
        }

        public int PopFront()
        {
            if (q.Count <= 0)
            {
                return -1;
            }

            var front = q[0];

            var array = new int[q.Count - 1];
            q.CopyTo(1, array, 0, q.Count - 1);

            q = array.ToList();

            return front;
        }

        public int PopBack()
        {
            if (q.Count <= 0)
            {
                return -1;
            }

            var back = q.Last();

            var array = new int[q.Count - 1];
            q.CopyTo(0, array, 0, q.Count - 1);

            q = array.ToList();

            return back;
        }

        public int Size()
        {
            return q.Count;
        }

        public bool IsEmpty() 
        {
            return (q.Count <= 0 ? true : false);
        }

        public int Front()
        {
            return q[0];
        }

        public int Back()
        {
            return q[q.Count - 1];
        }
    }
}

댓글