- 큐(Queue)란?
- 먼저 입력된 데이터가 먼저 출력되는 FIFO(First Int First Out) 데이터 구조를 나타내는 것
- 큐(Queue)의 주된 메소드
enqueue(): 큐의 맨 뒤에 새로운 요소를 추가
dequeue(): 큐의 맨 첫 번째 요소를 제거한 뒤 반환
peak(): 큐의 첫 번째 요소를 반환하되, 제거를 하지 않음
clear(): 큐를 재설정해 빈 상태가 되게함
count(): 큐에 있는 요소의 수를 반환
isEmpty(): 큐가 비어있으면 true 반환, 그렇지 않으면 flase 반환
isFull(): 큐가 꽉 차있으면 true 반환/ 그렇지 않으면 false 반환
- 큐(Queue)의 서브 메소드
capacity : 큐 용량을 가져오거나 설정하기 위한 read/write 프로퍼티
insert(_:atindex:): 큐의 특정 인덱스 위치에 요소를 삽입
removeAtindex()atindex: : 큐의 특정 인덱스 위치에 있는 요소를 제거
public struct Queue<T> {
private var queue: [T] = []
public var isEmpty: Bool{
return queue.isEmpty
}
public var count: Int{
return queue.count
}
public mutating func enqueue(_ element: T){
queue.append(element)
}
public mutating func dequeue() -> T? {
return isEmpty ? nil : queue.removeFisrt()
}
}
struct Queue<T> {
private var queue: [T?] = []
private var head: Int = 0
public var isEmpty:Bool {
return queue.isEmpty
}
public var count: Int {
return queue.count
}
public mutating func enqueue(_ elment: T){
queue.append(element)
}
publuc mutating func dequeue() -> T? {
guard head <= queue.count, let element = queue[head] else {
return queue[head] = nil
head +=1
if head > 50 {
queue.removeFirst(head)
head=0
}
}
return element
}
}