ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Queue 순서도
    알고리즘/수업내용 2022. 7. 4. 11:50

    Enqueue
    Peak
    Count
    Dequeue


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study13
    {
        public class Node<T>
        {
            public T data;
            public Node<T> next;
            public Node(T data)
            {
                this.data = data;
                this.next = null;
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study13
    {
        public class Queue<T>
        {
            private Node<T> head = null;
            private Node<T> tail = null;
            public Queue()
            {
    
            }
    
            public void Enqueue(T data)
            {
                if (this.head == null)
                {
                    this.head = new Node<T>(data);
                    this.tail = this.head;
                }
                else
                {
                    this.tail.next = new Node<T>(data);
                    this.tail = this.tail.next;
                }
            }
    
            public T Dequeue()
            {
                if (this.head == null)
                {
                    throw new Exception();
                }
    
                T data = head.data;
    
                if (head == tail)
                {
                    head = tail = null;
                }
                else
                {
                    head = head.next;
                }
                return data;
            }
    
            public int Count()
            {
                if(head == null)
                {
                    return 0;
                }
                else
                {
                    int cnt = 1;
                    Node<T> current = head;
                    while (current.next != null)
                    {
                        cnt++;
                        current = current.next;
                    }
                    return cnt;
                }
            }
            public void Peak()
            {
                T data;
            if (head == null)
                {
                    Console.WriteLine("empty");
                }
                else
                {
                    data = head.data;
                    Console.WriteLine(data);
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study13
    {
        public class App
        {
    
            public App()
            {
                Console.WriteLine("app");
                Queue<int> queue = new Queue<int>();
                queue.Enqueue(3);
                queue.Enqueue(5);
                queue.Enqueue(7);
                queue.Enqueue(9);
                queue.Dequeue();
                int a = queue.Count();
                Console.WriteLine(a);
                queue.Peak();
                queue.Peak();
                queue.Peak();
                queue.Peak();
            }
        }
    }

    '알고리즘 > 수업내용' 카테고리의 다른 글

    Stack 순서도  (0) 2022.07.04
    링크드 리스트  (0) 2022.07.01
Designed by Tistory.