본문 바로가기
c#/수업 내용

자료구조 Queue 복습연습

by 이지훈26 2021. 9. 24.

1.Enqueue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class Queue
    {
        public Node tail;
        public Node head;
        

        public Queue()
        {

        }

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

 

 

2.Dequeue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class Queue
    {
        public Node tail;
        public Node head;
        public int data;
        public Node temp;


        public Queue()
        {

        }

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

        public int Dequeue()
        {
            if (head == null)
            {
                throw new InvalidOperationException();
            }
            else
            {
                data = head.data;
                head = head.next;
                if (head == null)
                {
                    tail = null;
                    head = null;
                }
                return data;
            }
        }
    }
}

 

 

3.Peek

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class Queue
    {
        public Node tail;
        public Node head;
        public int data;
        public Node temp;


        public Queue()
        {

        }

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

        public int Dequeue()
        {
            if (head == null)
            {
                throw new InvalidOperationException();
            }
            else
            {
                data = head.data;
                head = head.next;
                if (head == null)
                {
                    tail = null;
                    head = null;
                }
                return data;
            }
        }
        public int Peek()
        {
            if (head == null)
            {
                throw new InvalidOperationException();
            }
            else
            {
                return head.data;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class Node
    {
        public int data;
        public Node next;

        //생성자
        public Node(int data)
        {
            this.data = data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helloworld1
{
    class App
    {
        public App()
        {
            Queue queue = new Queue();
            queue.Enqueue(3);
            queue.Enqueue(5);
            queue.Enqueue(7);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Peek());

        }
    }
}

 

 

4.Count