ํํ
๐Ÿ—’

ํ

๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ๊ฐœ๋…
๋‚œ์ด๋„
ํ•˜
Type
์ž๋ฃŒ
file

ํ์˜ ์ •์˜

  • ์ปดํ“จํ„ฐ์˜ ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ํ•œ๊ฐ€์ง€, Python์€ insert(0, ๊ฐ’)์™€ pop(0)๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Javascript๋Š” unshift์™€ shift๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ. Java๋Š” LinkedList๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ add / offer๋ฅผ ํ†ตํ•ด ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ poll์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋จผ์ € ์ง‘์–ด ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜์˜ค๋Š” ์„ ์ž…์„ ์ถœ(FIFO - Frist In First Out)๊ตฌ์กฐ๋กœ ์ €์žฅํ•˜๋Š” ํ˜•์‹
  • ํ”„๋ฆฐํ„ฐ์˜ ์ถœ๋ ฅ ์ฒ˜๋ฆฌ๋‚˜ ์œˆ๋„์šฐ ์‹œ์Šคํ…œ์˜ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๊ธฐ, ํ”„๋กœ์„ธ์Šค ๊ด€๋ฆฌ ๋“ฑ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋œ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ์— ์ด์šฉ
notion imagenotion image

ํ์˜ ์šฉ์–ด

  • put : ํ์— ์ž๋ฃŒ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ
  • get : ํ์—์„œ ์ž๋ฃŒ๋ฅผ ๊บผ๋‚ด๋Š” ๊ฒƒ
  • front : ๋ฐ์ดํ„ฐ๋ฅผ getํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜
  • rear : ๋ฐ์ดํ„ฐ๋ฅผ putํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜
  • Overflow : ํ๊ฐ€ ๊ฝ‰ ์ฐจ์„œ ๋” ์ด์ƒ ์ž๋ฃŒ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
  • Underflow : ํ๊ฐ€ ๋น„์–ด ์žˆ์–ด ์ž๋ฃŒ๋ฅผ ๊บผ๋‚ผ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ

ํ์˜ ์ข…๋ฅ˜

1. ์„ ํ˜• ํ

  • ๋ง‰๋Œ€ ๋ชจ์–‘์œผ๋กœ ๋œ ํ
  • ํฌ๊ธฐ๊ฐ€ ์ œํ•œ๋˜์–ด ์žˆ๊ณ  ๋นˆ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋“  ์ž๋ฃŒ๋ฅผ ๊บผ๋‚ด๊ฑฐ๋‚˜ ์ž๋ฃŒ๋ฅผ ํ•œ ์นธ์”ฉ ์˜ฎ๊ฒจ์•ผ
    • ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Œ
  • Data : A โ†’ B โ†’ C โ†’ D
    • - insert
notion imagenotion image

2. ํ™˜ํ˜• ํ

  • ๋ฐฐ์—ด๋กœ ํ๋ฅผ ์„ ์–ธํ•  ์‹œ ํ์˜ ์‚ญ์ œ์™€ ์ƒ์„ฑ์ด ๊ณ„์† ์ผ์–ด๋‚ฌ์„ ๋•Œ, ๋งˆ์ง€๋ง‰ ๋ฐฐ์—ด์— ๋„๋‹ฌ ํ›„ ์‹ค์ œ๋กœ๋Š” ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์ด ๋‚จ์•„์žˆ์ง€๋งŒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์„ ํ˜• ํ์˜ ๋ฌธ์ œ์ ์„ ๋ณด์™„ํ•œ ๊ฒƒ
  • front(head)๊ฐ€ ํ์˜ ๋์— ๋‹ฟ์œผ๋ฉด ํ์˜ ๋งจ ์•ž์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๋ณด๋‚ด์–ด ์›ํ˜•์œผ๋กœ ์—ฐ๊ฒฐ
  • Data : A โ†’ B โ†’ C โ†’ D โ†’ E
notion imagenotion image
ย 
์ถœ์ฒ˜ : https://ko.wikipedia.org/wiki/ํ_(์ž๋ฃŒ_๊ตฌ์กฐ) (์œ„ํ‚ค๋ฐฑ๊ณผ)
ย 
  • ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋œ ์„ ํ˜• ํ (java)
public class Main { public static void main(String args[]) { int size = 3; ArrayQueue arrQueue = new ArrayQueue(size); arrQueue.enqueue("A"); arrQueue.print(); arrQueue.enqueue("B"); arrQueue.print(); arrQueue.peek(); arrQueue.print(); arrQueue.enqueue("C"); arrQueue.print(); arrQueue.enqueue("D"); arrQueue.print(); arrQueue.dequeue(); arrQueue.print(); arrQueue.enqueue("E"); arrQueue.print(); arrQueue.dequeue(); arrQueue.print(); arrQueue.clear(); arrQueue.print(); arrQueue.clear(); arrQueue.dequeue(); } } class ArrayQueue { private int front; private int rear; private int size; private String arr[]; public ArrayQueue(int size) { front = -1; rear = -1; this.size = size; arr = new String[this.size]; } public boolean isEmpty() { return (front == rear); } public boolean isFull() { return (rear == this.size - 1); } public void enqueue(String item) { if(isFull()) { System.out.println("Queue is full"); } else { arr[++rear] = item; } } public String dequeue() { if(isEmpty()) { System.out.println("Queue is empty"); return null; } else { System.out.println(arr[front + 1]); front = (front + 1) % this.size; return arr[front]; } } public String peek() { if(isEmpty()) { System.out.println("Queue is empty"); return null; } else { System.out.println(arr[front + 1]); return arr[front + 1]; } } public void clear() { if(isEmpty()) { System.out.println("Queue is already empty"); } else { front = -1; rear = -1; arr = new String[this.size]; System.out.println("Queue is clear"); } } public void print() { if(isEmpty()) { System.out.println("Queue is empty"); } else { for (int i = front + 1; i <= rear; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } }
// ์ถœ๋ ฅ A A B A A B A B C Queue is full A B C A B C // ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ•œ ์„ ํ˜• ํ์˜ ๋‹จ์  : ์‹ค์ œ๋กœ๋Š” ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„์ด ๋‚จ์•„์žˆ์ง€๋งŒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒ Queue is full B C B C Queue is clear Queue is empty Queue is already empty Queue is empty
ย 
  • ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋œ ์„ ํ˜• ํ (java)
public class Main { public static void main(String args[]) { int size = 3; ListQueue listQueue = new ListQueue(size); listQueue.enqueue("A"); listQueue.print(); listQueue.enqueue("B"); listQueue.print(); listQueue.peek(); listQueue.print(); listQueue.enqueue("C"); listQueue.print(); listQueue.enqueue("D"); listQueue.print(); listQueue.dequeue(); listQueue.print(); listQueue.enqueue("E"); listQueue.print(); listQueue.dequeue(); listQueue.print(); listQueue.clear(); listQueue.print(); listQueue.clear(); listQueue.dequeue(); listQueue.print(); } } class Node { private String data; public Node link; public Node(String data) { this.data = data; this.link = null; } public Node(String data, Node link) { this.data = data; this.link = link; } public String getData() { return this.data; } } class ListQueue { private Node head; private Node front; private Node rear; private int size; public ListQueue(int size) { head = null; front = null; rear = null; this.size = size; } public boolean isEmpty(){ return (front == null && rear == null); } public boolean isFull() { if (isEmpty()) { return false; } else { int nodeCnt = 0; Node tmpNode = head; while(tmpNode.link != null) { nodeCnt++; tmpNode = tmpNode.link; } return (this.size - 1 == nodeCnt); } } public void enqueue(String data) { Node newNode = new Node(data); if (isFull()) { System.out.println("Queue is full"); return; } else if (isEmpty()) { this.head = newNode; this.front = this.head; this.rear = this.head; } else { rear.link = newNode; rear = newNode; } } public void dequeue() { Node tmpNode; if (isEmpty()) { System.out.println("Queue is empty"); return; } if(front.link == null) { head = null; front = null; rear = null; } else { tmpNode = front.link; head = tmpNode; front.link = null; front = head; } } public void peek() { if(isEmpty()) { System.out.println("Queue is empty"); return; } else { System.out.println(front.getData()); } } public void clear() { if(isEmpty()) { System.out.println("Queue is already empty"); return; } else { head = null; front = null; rear = null; } } public void print() { if (isEmpty()) { System.out.println("Queue is empty"); return; } else { Node tmpNode = this.front; while(tmpNode != null) { System.out.print(tmpNode.getData() + " "); tmpNode = tmpNode.link; } System.out.println(); } } }
// ์ถœ๋ ฅ A A B A A B A B C Queue is full A B C B C B C E C E Queue is empty Queue is already empty Queue is empty Queue is empty
ย 
  • Queue ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (java)
import java.util.LinkedList; import java.util.Queue; public class Main { public static void main(String args[]) { Queue<String> queue = new LinkedList<String>(); queue.offer("A"); System.out.println(queue.toString()); queue.offer("B"); System.out.println(queue.toString()); System.out.println(queue.peek()); System.out.println(queue.toString()); queue.offer("C"); System.out.println(queue.toString()); queue.offer("D"); System.out.println(queue.toString()); System.out.println(queue.poll()); System.out.println(queue.toString()); queue.offer("E"); System.out.println(queue.toString()); System.out.println(queue.poll()); System.out.println(queue.toString()); queue.clear(); System.out.println(queue.toString()); } }
// ์ถœ๋ ฅ [A] [A, B] A [A, B] [A, B, C] [A, B, C, D] A [B, C, D] [B, C, D, E] B [C, D, E] []