๊ด€๋ฆฌ ๋ฉ”๋‰ด

Algo ์“ฐ์ž

22. Queue, Stack, Hashtable ์‚ฌ์šฉ ๋ณธ๋ฌธ

๐Ÿ’ป Programming Language/C#

22. Queue, Stack, Hashtable ์‚ฌ์šฉ

S.Honey 2022. 4. 7. 09:31

Queue(ํ)

  • Queue : ์„ ์ž…์„ ์ถœ(first in first out(FIFO)) ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ
    • Enqueue : ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ
    • Dequque : ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅ
using System.Collections;

namespace QueueEx
{   
    class Program
    {
        static void Main(string[] args)
        {
            Queue q = new Queue();

            q.Enqueue(1);
            q.Enqueue(100);
            q.Enqueue(200);
            q.Dequeue();
            q.Enqueue(22.5);
            int aa = (int)q.Dequeue(); 
            // objํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ casting ํ•„์š”.
            q.Enqueue("๊ฐ€๋‚˜๋‹ค");

            while(q.Count > 0)
            {
                Console.WriteLine(q.Dequeue());
            }
        }
    }
}
Output

200
22.5
๊ฐ€๋‚˜๋‹ค
  • object ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” casting์ด ํ•„์š”ํ•˜๋‹ค.

Stack(์Šคํƒ)

  • ์Šคํƒ(Stack) : ํ›„์ž…์„ ์ถœ(LIFO)์˜ ์ž๋ฃŒ๊ตฌ์กฐ
              ๋‚˜์ค‘์— ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ์ถœ๋ ฅ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
  • Push : ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฉ”์†Œ๋“œ
  • Pop : ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ
using System.Collections;

namespace StackEx
{
     class Program
    {
        static void Main(string[] args)
        {
            Stack stack = new Stack();
            stack.Push(100);
            stack.Push(11);
            stack.Pop();
            stack.Push(2.33);
            stack.Pop();
            stack.Push("๋ฌธ์ž์—ด");

            while(stack.Count > 0)
            {
                Console.WriteLine(stack.Pop());
            }
        }
    }
}
Output

๋ฌธ์ž์—ด
100

Hashtable(ํ•ด์‹œํ…Œ์ด๋ธ”)

  • Hashtable : ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค-๊ฐ’(key-value)์Œ์œผ๋กœ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

  • Key๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ณ  value๋ฅผ ์–ป๋Š”๋‹ค.

  • Hashing ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐฉ์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

    • ์ด๋กœ์ธํ•ด ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์†๋„๋ฅผ ๋‚ด๋ฉฐ ํƒ์ƒ‰์— ์žˆ์–ด O(1) => ์ƒ์ˆ˜์‹œ๊ฐ„ ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.
      • ํ‚ค๋ฅผ ์ด์šฉํ•ด ํ•œ๋ฒˆ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ปฌ๋ ‰์…˜ ๋‚ด์˜ ์ฃผ์†Œ๋ฅผ ๊ณ„์‚ฐํ•ด๋‚ธ๋‹ค.
    using System.Collections;
    

namespace HashtableEx
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht["Apple"] = "์‚ฌ๊ณผ";
ht["Banana"] = "๋ฐ”๋‚˜๋‚˜";
ht["Orange"] = "์˜ค๋ Œ์ง€";

        Console.WriteLine(ht["Apple"]);
        Console.WriteLine(ht["Banana"]);
        Console.WriteLine(ht["Orange"]);
    }
}

}

 ```java
Output

์‚ฌ๊ณผ
๋ฐ”๋‚˜๋‚˜
์˜ค๋ Œ์ง€

์ „์ฒด ์ฝ”๋“œ

 using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("-----ํ-----");
        Queue q = new Queue();

        q.Enqueue(1);
        q.Enqueue(100);
        q.Enqueue(200);
        q.Dequeue();
        q.Enqueue(22.5);
        int aa = (int)q.Dequeue();
        // objํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ casting ํ•„์š”.
        q.Enqueue("๊ฐ€๋‚˜๋‹ค");

        while (q.Count > 0)
        {
            Console.WriteLine(q.Dequeue());
        }

        Console.WriteLine("\n-----์Šคํƒ-----");

        Stack stack = new Stack();
        stack.Push(100);
        stack.Push(11);
        stack.Pop();
        stack.Push(2.33);
        stack.Pop();
        stack.Push("๋ฌธ์ž์—ด");

        while (stack.Count > 0)
        {
            Console.WriteLine(stack.Pop());
        }

        Console.WriteLine("\n-----ํ•ด์‹œํ…Œ์ด๋ธ”-----");

        Hashtable ht = new Hashtable();
        ht["Apple"] = "์‚ฌ๊ณผ";
        ht["Banana"] = "๋ฐ”๋‚˜๋‚˜";
        ht["Orange"] = "์˜ค๋ Œ์ง€";

        Console.WriteLine(ht["Apple"]);
        Console.WriteLine(ht["Banana"]);
        Console.WriteLine(ht["Orange"]);
    }
}
Output 

-----ํ-----
200
22.5
๊ฐ€๋‚˜๋‹ค

-----์Šคํƒ-----
๋ฌธ์ž์—ด
100

-----ํ•ด์‹œํ…Œ์ด๋ธ”-----
์‚ฌ๊ณผ
๋ฐ”๋‚˜๋‚˜
์˜ค๋ Œ์ง€