티스토리 뷰

앞뒤가 다 열려있는 괴상한 자료구조. 

기존 배열에 front/back index 만들어주고 index값을 관리하였다.

push_front에서 배열의 index는 음수가 될 수 없기때문에 한칸씩 뒤로 옮겨줘야 하는 상황이 발생한다.

정말 피할 수 없을까..? 하고 배열 두개를 관리하는 방법도 생각했는데 그러느니 그냥 한칸씩 뒤로 옮겨줘야겠다 생각하였음.

그 외는 index와 삽입정도고 별 거 없었다.


                switch(str[0])

                {

                    case "push_front":

                        if (frontIndex != 0)

                        {

                            stack[--frontIndex] = int.Parse(str[1]);

                            break;

                        }

                        for (int n = backIndex++; n != 0; n--)

                            stack[n] = stack[n - 1];


                        stack[0] = int.Parse(str[1]);

                        break;

                    case "push_back":

                        stack[backIndex++] = int.Parse(str[1]);

                        break;

                    case "pop_front":

                        if (backIndex == frontIndex)

                        {

                            sb.AppendLine("-1");

                            break;

                        }

                        sb.AppendLine(stack[frontIndex++].ToString());

                        break;

                    case "pop_back":

                        if (backIndex == frontIndex)

                        {

                            sb.AppendLine("-1");

                            break;

                        }

                        sb.AppendLine(stack[--backIndex].ToString());

                        break;

                    case "size":

                        sb.AppendLine((backIndex - frontIndex).ToString());

                        break;

                    case "empty":

                        if (backIndex == frontIndex)

                            sb.AppendLine("1");

                        else

                            sb.AppendLine("0");

                        break;

                    case "front":

                        if (backIndex == frontIndex)

                            sb.AppendLine("-1");

                        else

                            sb.AppendLine(stack[frontIndex].ToString());

                        break;

                    case "back":

                        if (backIndex == frontIndex)

                            sb.AppendLine("-1");

                        else

                            sb.AppendLine(stack[backIndex - 1].ToString());

                        break;

                }


중요한 부분만 첨부.


'■ 알고리즘 > ◻ 백준' 카테고리의 다른 글

[C#]백준 12813번 : 이진수 연산  (0) 2018.08.10
[C#]백준 1076번 : 저항  (0) 2018.08.10
백준 할때 자주 찾는 메소드  (0) 2018.08.10
[C#]백준 1966번 : 프린터 큐  (0) 2018.08.10
[C#] 백준 10845번 : 큐  (0) 2018.08.09
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함