본문 바로가기

자료구조

Queue 복습

Queue와 Node를 준비

    class Node
    {
        public int data;
        public Node next;

        //생성자
        public Node(int data)
        {
            this.data = data;
        }
    }

    class Queue
    {
        private Node head;
        private Node tail;

        //생성자
        public Queue()
        {

        }
    }

Enqueue

        public void Enqueue(int data)
        {
            if (this.head == null)
            {
                this.head = new Node(data);
                this.tail = this.head;
            }
            else
            {
                this.tail.next = new Node(data);
                this.tail = this.tail.next;
            }
        }

Dequeue

        public int Dequeue()
        {
            if (this.head == null)
            {
                throw new InvalidOperationException("큐가 비어있다.");
            }
            else
            {
                int data = this.head.data;
                if (this.head.next == null)
                {
                    this.head = null;
                    this.tail = null;
                }
                else
                {
                    Node temp = this.head; // 이 작업
                    this.head = this.head.next;
                    temp.next = null; // 해주면 좋음 후에 노드를 재활용할때
                                      // 멤버변수가 바뀌어있는경우의 혼선 방지
                }
                return data;
            }
        }

Peek

        public int Peek()
        {
            if (this.head == null)
            {
                throw new InvalidOperationException("큐가 비어있다.");
            }
            else
            {
                return this.head.data;
            }
        }

Count 반복문

        public int Count()
        {
            int count = 0;
            if (this.head == null)
            {
                return count;
            }
            else
            {
                Node temp = this.head;
                count = 1;

                while (true)
                {
                    if (temp.next == null)
                    {
                        return count;
                    }
                    else
                    {
                        temp = temp.next;
                        count += 1;
                    }
                }
            }
        }

Count 재귀함수

        //호출부
        public int Count()
        {
            int count = 0;
            if (this.head == null)
            {
                return count;
            }
            else
            {
                return CountRecursive(count + 1, this.head);
            }
        }

        //재귀부
        public int CountRecursive(int count, Node node)
        {
            if (node.next == null)
            {
                return count;
            }
            else
            {
                return CountRecursive(count + 1, node.next);
            }
        }

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

LCRS트리  (0) 2021.12.28
트리 LCRS Expression  (0) 2021.12.28
Queue Count  (0) 2021.12.28
Queue Peek  (0) 2021.12.28
Queue Dequeue  (0) 2021.12.28