1οΈβ£ ν(Queue)
ν(Queue)λ μ»΄ν¨ν° κ³Όνμμ νν μ¬μ©λλ μλ£κ΅¬μ‘° μ€ νλλ‘, μ μ μ μΆ(FIFO, First In First Out) λ°©μμΌλ‘ λμν©λλ€.
μ¦, νμ λ¨Όμ λ€μ΄κ° λ°μ΄ν°κ° λ¨Όμ λμ€λ ꡬ쑰μ λλ€.
νμ μ£Όμμ°μ°.
-
- Enqueue : νμ λ€μͺ½(rear)μ μλ‘μ΄ μμλ₯Ό μΆκ°ν©λλ€.
-
- Dequeue : νμ μμͺ½(front)μμ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€.
-
- Peek λλ Front : νμ μμͺ½(front) μμλ₯Ό μ κ±°νμ§ μκ³ λ°νν©λλ€.
-
- IsEmpty : νκ° λΉμ΄ μλμ§ μ¬λΆλ₯Ό νμΈν©λλ€.
νκ° μ μ©νκ² μ¬μ©λλ μν©.
- νλ¦°ν° λκΈ°μ΄ : μΈμ μμ μ μμλλ‘ μ²λ¦¬ν©λλ€.
- νλ‘μΈμ€ μ€μΌμ€λ§ : μ΄μ 체μ μμ νλ‘μΈμ€λ€μ΄ CPU μκ°μ μ»κΈ° μν΄ λκΈ°νλ μμλ₯Ό μ μ§ν©λλ€.
- λλΉ μ°μ νμ(BFS) : κ·Έλν νμ μκ³ λ¦¬μ¦μμ κ° λ Έλλ₯Ό λ°©λ¬Έν μμλ₯Ό μ μ§ν©λλ€.
νμ ꡬν.
νλ λ°°μ΄μ΄λ μ°κ²° 리μ€νΈλ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μμ΅λλ€.
λ°°μ΄μ μ¬μ©ν ν ꡬν μμ
public class QueueArray {
private int[] elements;
private int front;
private int rear;
private int size;
private int capacity;
public QueueArray(int capacity) {
this.capacity = capacity;
elements = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
// νμ μμ μΆκ°
public void enqueue(int element) {
if (size == capacity) {
System.out.println("Queue is full");
return;
}
rear = (rear + 1) % capacity;
elements[rear] = element;
size++;
}
// νμμ μμ μ κ±° λ° λ°ν
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
int element = elements[front];
front = (front + 1) % capacity;
size--;
return element;
}
// νμ μμͺ½ μμ λ°ν
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return elements[front];
}
// νκ° λΉμ΄ μλμ§ νμΈ
public boolean isEmpty() {
return size == 0;
}
// νμ ν¬κΈ° λ°ν
public int getSize() {
return size;
}
}
// Main ν΄λμ€
public class Main {
public static void main(String[] args) {
QueueArray queue = new QueueArray(5);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Front element: " + queue.peek());
System.out.println("Dequeue element: " + queue.dequeue());
System.out.println("Front element after dequeue: " + queue.peek());
queue.enqueue(4);
queue.enqueue(5);
queue.enqueue(6);
while (!queue.isEmpty()) {
System.out.println("Dequeued element: " + queue.dequeue());
}
}
}
// μΆλ ₯
/*
Front element: 1
Dequeue element: 1
Front element after dequeue: 2
Dequeued element: 2
Dequeued element: 3
Dequeued element: 4
Dequeued element: 5
Dequeued element: 6
* /
μ°κ²° 리μ€νΈλ₯Ό μ¬μ©ν ν ꡬν μμ
// Node
public class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
// QueueLinkedList
public class QueueLinkedList {
private Node front;
private Node rear;
private int size;
public QueueLinkedList() {
front = null;
rear = null;
size = 0;
}
// νμ μμ μΆκ°
public void enqueue(int data) {
Node newNode = new Node(data);
if (rear != null) {
rear.next = newNode;
}
rear = newNode;
if (front == null) {
front = newNode;
}
size++;
}
// νμμ μμ μ κ±° λ° λ°ν
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
int data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
size--;
return data;
}
// νμ μμͺ½ μμ λ°ν
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return front.data;
}
// νκ° λΉμ΄ μλμ§ νμΈ
public boolean isEmpty() {
return front == null;
}
// νμ ν¬κΈ° λ°ν
public int getSize() {
return size;
}
}
// Main ν΄λμ€
public class Main {
public static void main(String[] args) {
QueueLinkedList queue = new QueueLinkedList();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
System.out.println("Front element: " + queue.peek());
System.out.println("Dequeue element: " + queue.dequeue());
System.out.println("Front element after dequeue: " + queue.peek());
queue.enqueue(4);
queue.enqueue(5);
while (!queue.isEmpty()) {
System.out.println("Dequeued element: " + queue.dequeue());
}
}
}
// μΆλ ₯
/*
Front element: 1
Dequeue element: 1
Front element after dequeue: 2
Dequeued element: 2
Dequeued element: 3
Dequeued element: 4
Dequeued element: 5
* /