본문 바로가기

자료구조

Pop + peek

Pop

        public Node Pop()
        {
            if (this.top == null)
            {
                throw new InvalidOperationException("스택이 비어있다.");   
            }
            else
            {
                Node temp = this.top;
                Console.WriteLine("{0}", this.top.Data);
                this.top = this.top.Next;
                temp.Next = null;
                return temp;
            }
        }

 


Peek

        public Node Peek()
        {
            if (this.top == null)
            {
                throw new InvalidOperationException("스택이 비어있다.");
            }
            else
            {
                Console.WriteLine("{0}", this.top.Data);
                return this.top;
            }
        }

Node를 반환받으려면 Node클래스의 접근제한자를 public으로 바꿀필요가있다.

'자료구조' 카테고리의 다른 글