SlideShare a Scribd company logo
1 of 15
Download to read offline
SWIFT
Data Structure -
Dequeue
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Dequeue
•Concept
•Features
•Implementation
•References
Dequeue
데크(Dequeue)는 Doubly-ended Queue의 약자로서 양쪽 끝에
서 추가, 삭제가 가능한 선형 구조 형태의 자료구조입니다.
스택과 큐의 장점을 모아서 만들어진 형태입니다.
추가, 삭제가 되는 부분의 명칭을 보통 Front, Rear이라고 명칭합
니다.
데크(Dequeue)에는 제약 조건을 걸어 사용 목적을 달리하는 구조
또한 있습니다.
그것은 바로 입력제한데크(Scroll)와 출력제한데크(Shelf)가 있습
니다.
Concept
Dequeue의 기본 동작 흐름은 아래와 같습니다.
- 입력과 출력이 양방향 가능
- 입력과 출력의 순서가 맘대로 정할 수 있음
- Enqueue(추가) 및 Dequeue(삭제) 실행 속도는 O(1)
Concept
입력제한데크(Scroll)의 기본 동작 흐름은 아래와 같습니다.
- 입력(추가)을 한곳에만 제한을 주는 데크
- 삭제는 양방향 가능
Concept
출력제한데크(Shelf)의 기본 동작 흐름은 아래와 같습니다.
- 삭제를 한곳에만 제한을 주는 데크
- 추가는 양방향 가능
Features
Dequeue의 특징을 살펴보면 아래와 같습니다.
- 데이터를 양쪽에서 삽입 한 곳에서는 삭제가 가능한 구조
- 데이터 입력 순서와 상관없이 출력 순서 조절 가능
- 스택과 큐의 장점을 모아서 만들어진 자료구조
Implementation
Swift를 활용하여 Dequeue를 구현해보겠습니다. 우선 필요한 메
소드는 아래와 같습니다.
- init : 리스트를 초기화하는 함수
- enqueue : 데이터 입력
- enqueueFront : 앞쪽 방향에 데이터 입력
- dequeue : 첫번째 데이터 삭제
- dequeueBack : 뒤쪽 방향의 데이터 삭제
- peekFront : 첫번째 데이터 반환
- peekBack : 마지막 데이터 반환
- removeAll : 모든 데이터 삭제
- count : 현재 리스트의 크기를 반환
- isEmpty : 현재 리스트의 크기가 비어있는지 체크
Implementation
class Dequeue<T> {
private var array = [T]()
public init() {}
public func enqueue(_ element: T) {
array.append(element)
}
public func enqueueFront(_ element: T) {
array.insert(element, at: 0)
}
public func dequeue() -> T? {
if isEmpty {
return nil
} else {
return array.removeFirst()
}
}
public func dequeueBack() -> T? {
if isEmpty {
return nil
} else {
return array.removeLast()
}
}
public func removeAll() {
array.removeAll()
}
}
Implementation
extension Dequeue {
public func peekFront() -> T? {
return array.first
}
public func peekBack() -> T? {
return array.last
}
public var count: Int {
return array.count
}
public var isEmpty: Bool {
return array.isEmpty
}
}
Implementation
데이터 반복(Iterator) 제어를 위하여 아래의 프로토콜들을 체택합
니다.
public struct DequeueIterator<T> : IteratorProtocol {
var currentElement: [T]
public mutating func next() -> T? {
if !self.currentElement.isEmpty {
return currentElement.removeFirst()
}else {
return nil
}
}
}
extension Dequeue : Sequence {
public func makeIterator() -> DequeueIterator<T> {
return DequeueIterator(currentElement: self.array)
}
}
Implementation
let dequeue = Dequeue<Int>()
dequeue.enqueue(1)
dequeue.enqueue(2)
dequeue.enqueue(3)
dequeue.enqueueFront(4)
dequeue.enqueueFront(5)
// 현재 데이터 카운트 : 5
print(dequeue.count)
for node in dequeue {
print(node)
// 5
// 4
// 1
// 2
// 3
}
Implementation
print(dequeue.peekFront()!) // 5
print(dequeue.dequeue()!) // 5
print(dequeue.dequeueBack()!) // 3
for node in dequeue {
print(node)
// 4
// 1
// 2
}
print(dequeue.peekBack()!) // 2
References
[1] 3. 데큐 구조체 : https://herohjk.com/16
[2] [선형구조]자료 구조의 개념 정리(리스트, 스택, 큐, 데크) :
https://lee-mandu.tistory.com/462
[3] 자료구조 ] 큐와 데크란? : https://m.blog.naver.com/
PostView.nhn?
blogId=rbdi3222&logNo=220620826550&proxyReferer=htt
ps:%2F%2Fwww.google.com%2F
[4] 정보처리 자료구조 스택 큐 데크 : https://m.blog.naver.com/
PostView.nhn?
blogId=xpiart&logNo=220268937540&proxyReferer=https:
%2F%2Fwww.google.com%2F
[5] [자료구조] 4. Deque : https://ieatt.tistory.com/7
Thank you!

More Related Content

What's hot

R 스터디 네번째
R 스터디 네번째R 스터디 네번째
R 스터디 네번째Jaeseok Park
 
R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1happychallenge
 
2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자Circulus
 
Startup JavaScript 4 - 객체
Startup JavaScript 4 - 객체Startup JavaScript 4 - 객체
Startup JavaScript 4 - 객체Circulus
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Circulus
 
R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작Terry Cho
 
알고리즘 스터디(정렬) Seungdols
알고리즘 스터디(정렬) Seungdols알고리즘 스터디(정렬) Seungdols
알고리즘 스터디(정렬) Seungdolsseungdols
 
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)MIN SEOK KOO
 
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육탑크리에듀(구로디지털단지역3번출구 2분거리)
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)NAVER D2
 
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03chl132435
 
Python Programming: Function
Python Programming: FunctionPython Programming: Function
Python Programming: FunctionChan Shik Lim
 
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdfkd19h
 
R 스터디 첫번째
R 스터디 첫번째R 스터디 첫번째
R 스터디 첫번째Jaeseok Park
 
나에 첫번째 자바8 람다식 지앤선
나에 첫번째 자바8 람다식   지앤선나에 첫번째 자바8 람다식   지앤선
나에 첫번째 자바8 람다식 지앤선daewon jeong
 

What's hot (20)

R 스터디 네번째
R 스터디 네번째R 스터디 네번째
R 스터디 네번째
 
R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1
 
2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자
 
2012 Ds 03
2012 Ds 032012 Ds 03
2012 Ds 03
 
Startup JavaScript 4 - 객체
Startup JavaScript 4 - 객체Startup JavaScript 4 - 객체
Startup JavaScript 4 - 객체
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
 
R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작R 프로그래밍-향상된 데이타 조작
R 프로그래밍-향상된 데이타 조작
 
알고리즘 스터디(정렬) Seungdols
알고리즘 스터디(정렬) Seungdols알고리즘 스터디(정렬) Seungdols
알고리즘 스터디(정렬) Seungdols
 
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)
(망작)이것이 자바다 Chap.16 스트림&병렬처리 Stream&parallel processing(java)
 
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
(SQL초보자를 위한, 쿼리최적화 for SQL튜닝)SQL쿼리작성Tip,최적화팁,최적화된SQL작성방법교육
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)
 
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03
 
Python Programming: Function
Python Programming: FunctionPython Programming: Function
Python Programming: Function
 
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf
 
Ch07
Ch07Ch07
Ch07
 
강의자료4
강의자료4강의자료4
강의자료4
 
Ch08
Ch08Ch08
Ch08
 
R 스터디 첫번째
R 스터디 첫번째R 스터디 첫번째
R 스터디 첫번째
 
나에 첫번째 자바8 람다식 지앤선
나에 첫번째 자바8 람다식   지앤선나에 첫번째 자바8 람다식   지앤선
나에 첫번째 자바8 람다식 지앤선
 
1.9 튜닝의도구 10053 event
1.9 튜닝의도구 10053 event1.9 튜닝의도구 10053 event
1.9 튜닝의도구 10053 event
 

Similar to [Swift] Data Structure - Dequeue

[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - StackBill Kim
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - QueueBill Kim
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
 
데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayListJaewook Byun
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search TreeBill Kim
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트S.O.P.T - Shout Our Passion Together
 
20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POP20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POPChiwon Song
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQLI Goo Lee
 
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 [GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 Sehyeon Nam
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율S.O.P.T - Shout Our Passion Together
 
Function calling convention
Function calling conventionFunction calling convention
Function calling conventionYuk SeungChan
 
Collection framework
Collection frameworkCollection framework
Collection frameworkssuser34b989
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산S.O.P.T - Shout Our Passion Together
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard LibraryDongMin Choi
 
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationSecrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationHyuncheol Jeon
 
Data structure review (summer study)
Data structure review (summer study)Data structure review (summer study)
Data structure review (summer study)Melon Lemon
 

Similar to [Swift] Data Structure - Dequeue (20)

[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList
 
강의자료5
강의자료5강의자료5
강의자료5
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
 
20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POP20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POP
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
From MSSQL to MySQL
From MSSQL to MySQLFrom MSSQL to MySQL
From MSSQL to MySQL
 
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 [GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
 
Function calling convention
Function calling conventionFunction calling convention
Function calling convention
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
 
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationSecrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
 
자료구조02
자료구조02자료구조02
자료구조02
 
Scala
ScalaScala
Scala
 
Data structure review (summer study)
Data structure review (summer study)Data structure review (summer study)
Data structure review (summer study)
 

More from Bill Kim

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting ComparisonBill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O NotationBill Kim
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell SortBill Kim
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix SortBill Kim
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick SortBill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap SortBill Kim
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting SortBill Kim
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection SortBill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge SortBill Kim
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion SortBill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble SortBill Kim
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary SearchBill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVLBill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary TreeBill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - TreeBill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - GraphBill Kim
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - HeapBill Kim
 

More from Bill Kim (20)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
 

[Swift] Data Structure - Dequeue

  • 1. SWIFT Data Structure - Dequeue Bill Kim(김정훈) | ibillkim@gmail.com
  • 3. Dequeue 데크(Dequeue)는 Doubly-ended Queue의 약자로서 양쪽 끝에 서 추가, 삭제가 가능한 선형 구조 형태의 자료구조입니다. 스택과 큐의 장점을 모아서 만들어진 형태입니다. 추가, 삭제가 되는 부분의 명칭을 보통 Front, Rear이라고 명칭합 니다. 데크(Dequeue)에는 제약 조건을 걸어 사용 목적을 달리하는 구조 또한 있습니다. 그것은 바로 입력제한데크(Scroll)와 출력제한데크(Shelf)가 있습 니다.
  • 4. Concept Dequeue의 기본 동작 흐름은 아래와 같습니다. - 입력과 출력이 양방향 가능 - 입력과 출력의 순서가 맘대로 정할 수 있음 - Enqueue(추가) 및 Dequeue(삭제) 실행 속도는 O(1)
  • 5. Concept 입력제한데크(Scroll)의 기본 동작 흐름은 아래와 같습니다. - 입력(추가)을 한곳에만 제한을 주는 데크 - 삭제는 양방향 가능
  • 6. Concept 출력제한데크(Shelf)의 기본 동작 흐름은 아래와 같습니다. - 삭제를 한곳에만 제한을 주는 데크 - 추가는 양방향 가능
  • 7. Features Dequeue의 특징을 살펴보면 아래와 같습니다. - 데이터를 양쪽에서 삽입 한 곳에서는 삭제가 가능한 구조 - 데이터 입력 순서와 상관없이 출력 순서 조절 가능 - 스택과 큐의 장점을 모아서 만들어진 자료구조
  • 8. Implementation Swift를 활용하여 Dequeue를 구현해보겠습니다. 우선 필요한 메 소드는 아래와 같습니다. - init : 리스트를 초기화하는 함수 - enqueue : 데이터 입력 - enqueueFront : 앞쪽 방향에 데이터 입력 - dequeue : 첫번째 데이터 삭제 - dequeueBack : 뒤쪽 방향의 데이터 삭제 - peekFront : 첫번째 데이터 반환 - peekBack : 마지막 데이터 반환 - removeAll : 모든 데이터 삭제 - count : 현재 리스트의 크기를 반환 - isEmpty : 현재 리스트의 크기가 비어있는지 체크
  • 9. Implementation class Dequeue<T> { private var array = [T]() public init() {} public func enqueue(_ element: T) { array.append(element) } public func enqueueFront(_ element: T) { array.insert(element, at: 0) } public func dequeue() -> T? { if isEmpty { return nil } else { return array.removeFirst() } } public func dequeueBack() -> T? { if isEmpty { return nil } else { return array.removeLast() } } public func removeAll() { array.removeAll() } }
  • 10. Implementation extension Dequeue { public func peekFront() -> T? { return array.first } public func peekBack() -> T? { return array.last } public var count: Int { return array.count } public var isEmpty: Bool { return array.isEmpty } }
  • 11. Implementation 데이터 반복(Iterator) 제어를 위하여 아래의 프로토콜들을 체택합 니다. public struct DequeueIterator<T> : IteratorProtocol { var currentElement: [T] public mutating func next() -> T? { if !self.currentElement.isEmpty { return currentElement.removeFirst() }else { return nil } } } extension Dequeue : Sequence { public func makeIterator() -> DequeueIterator<T> { return DequeueIterator(currentElement: self.array) } }
  • 12. Implementation let dequeue = Dequeue<Int>() dequeue.enqueue(1) dequeue.enqueue(2) dequeue.enqueue(3) dequeue.enqueueFront(4) dequeue.enqueueFront(5) // 현재 데이터 카운트 : 5 print(dequeue.count) for node in dequeue { print(node) // 5 // 4 // 1 // 2 // 3 }
  • 13. Implementation print(dequeue.peekFront()!) // 5 print(dequeue.dequeue()!) // 5 print(dequeue.dequeueBack()!) // 3 for node in dequeue { print(node) // 4 // 1 // 2 } print(dequeue.peekBack()!) // 2
  • 14. References [1] 3. 데큐 구조체 : https://herohjk.com/16 [2] [선형구조]자료 구조의 개념 정리(리스트, 스택, 큐, 데크) : https://lee-mandu.tistory.com/462 [3] 자료구조 ] 큐와 데크란? : https://m.blog.naver.com/ PostView.nhn? blogId=rbdi3222&logNo=220620826550&proxyReferer=htt ps:%2F%2Fwww.google.com%2F [4] 정보처리 자료구조 스택 큐 데크 : https://m.blog.naver.com/ PostView.nhn? blogId=xpiart&logNo=220268937540&proxyReferer=https: %2F%2Fwww.google.com%2F [5] [자료구조] 4. Deque : https://ieatt.tistory.com/7