카테고리 없음

[백준 10828번 c#] 스택

Heeyeon Choi 2022. 10. 6. 17:21
728x90
728x90
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project2
{
    class Class1
    {
        public static int[] stack;
        public static int indexSize = 0;
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput());
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();
            //[1]입력
            int num = int.Parse(reader.ReadLine());
            stack = new int[num];
            while (num-- > 0)
            {
                string str = reader.ReadLine();
                List<string> li = str.Split().ToList(); 
                string str1 = li[0];
                int str2=0 ;
                if (li.Count ==2)
                {
                    str2 = int.Parse(li[1]);
                }

                switch (str1)
                {

                    case "push":
                        push(str2);
                        break;

                    case "pop":
                        sb.Append(pop()).Append('\n');
                        break;

                    case "size":
                        sb.Append(size()).Append('\n');
                        break;

                    case "empty":
                        sb.Append(empty()).Append('\n');
                        break;

                    case "top":
                        sb.Append(top()).Append('\n');
                        break;
                }
            }
            writer.WriteLine(sb.ToString());
            writer.Close();
            reader.Close();
            
        }
        public static void push(int item)
        {
            //스택에 값을 넣는다.
            stack[indexSize] = item;
            //값을 넣은 후, 인덱스 값을 하나 올려준다.
            indexSize++;
        }

        public static int pop()
        {
            //만약 아무것도 들지 않은 상태에서 지우라면, -1을 반환한다.
            if (indexSize == 0)
            {
                return -1;
            }
            //지울 값의 전 인덱스의 값을 저장한후, 지울 값을 지운 후, 인덱스를 하나 감소한다.
            //이 때, 저장해둔 전 인덱스의 값을 반환한다. 
            else
            {
                int res = stack[indexSize - 1];
                stack[indexSize - 1] = 0;
                indexSize--;
                return res;
            }
        }
        //스택의 사이즈를 반환합니다.
        public static int size()
        {
            return indexSize;
        }
        //스택이 비어있는지 검사합니다.
        public static int empty()
        {
            if (indexSize == 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //스택의 제일 마지막 인덱스를 검사합니다.
        public static int top()
        {
            if (indexSize == 0)
            {
                return -1;
            }
            else
            {
                return stack[indexSize - 1];
            }
        }

    }

}
728x90