SlideShare a Scribd company logo
1 of 12
Download to read offline
SWIFT
Data Structure -
Stack
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Stack
•Concept
•Features
•Implementation
•References
Stack
스택(Stack)는 리스트의 한쪽 끝에서 자료의 삽입과 삭제가 이루어
지는 자료구조입니다.
가장 최근에 들어온 자료가 가장 먼저 나가게 되는 LIFO(Last-In
First-Out) 형태를 가지고 있습니다.
따라서 중간에서는 데이터를 추가 및 삭제하는 것이 불가능합니다
스택이 입출력이 이루어지는 부분을 스택 상단(Stack top) , 바닥
부분을 스택 하단(Stack bottom), 스택에 저장되는 것을 요소
(Element) 라 부르며 스택에 요소가 하나도 없을 때 그러한 스택을
공백 스택(Empty stack) 이라고 합니다.
Concept
Stack의 기본 동작 흐름은 아래와 같습니다.
Features
Stack의 특징을 살펴보면 아래와 같습니다.
- 데이터를 한 곳에서 삽입, 삭제
- Last In First Out (LIFO, 후입선출) 방식
- 중간에서 데이터 삽입 및 삭제 불가
- 배열과 비슷하지만 삽입, 삭제 등이 더 제한적임
Implementation
Swift를 활용하여 Stack 을 구현해보겠습니다.
우선 필요한 메소드는 아래와 같습니다.
- init : 리스트를 초기화하는 함수
- push : 데이터 입력
- pop : 마지막 데이터 삭제
- peak : 마지막 데이터 반환
- count : 현재 리스트의 크기를 반환
- isEmpty : 현재 리스트의 크기가 비어있는지 체크
Implementation
class Stack<T> {
private var elements = Array<T>()
public init() {}
public func push(element: T) {
self.elements.append(element)
}
public func pop() -> T? {
return self.elements.popLast()
}
public func peek() -> T? {
return self.elements.last
}
public var isEmpty: Bool {
return self.elements.isEmpty
}
public var count: Int {
return self.elements.count
}
}
Implementation
데이터 반복(Iterator) 제어를 위하여 아래의 프로토콜들을 체택합
니다.
public struct ArrayIterator<T> : IteratorProtocol {
var currentElement: [T]
init(elements: [T]) {
self.currentElement = elements
}
public mutating func next() -> T? {
if !self.currentElement.isEmpty {
return self.currentElement.popLast()
}
return nil
}
}
extension Stack : Sequence {
public func makeIterator() -> ArrayIterator<T> {
return ArrayIterator<T>(elements: self.elements)
}
}
Implementation
let stack = Stack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
// 현재 데이터 카운트 : 5
print(stack.count)
for node in stack {
print(node)
// 5
// 4
// 3
// 2
// 1
}
References
[1] [스위프트 : 자료구조] 스택 : Stack : https://the-brain-of-
sic2.tistory.com/38
[2] [스위프트:자료구조] 스택: Stack: 자료구조:
DataStructure: 쌓기: swift : https://the-brain-of-
sic2.tistory.com/18
[3] [Swift 자료구조 ch04] Stack : https://
kor45cw.tistory.com/240
[4] Swift로 작성해보는 기본 자료구조 - Stack, Queue :
https://wlaxhrl.tistory.com/87
[5] Stack : https://woongsios.tistory.com/87
References
[6] 스택(Stack) : https://kka7.tistory.com/74
[7] Swift, Data Structure, Stack : https://
devmjun.github.io/archive/Stack
[8] [알고리즘] 2.1. 자료구조 : 스택(Stack) 이해하기 : https://
monsieursongsong.tistory.com/4
[9] [자료구조]스택과 큐. (stack and queue) : https://
winplz.tistory.com/entry/자료구조스택과-큐-stack-and-
queue
[10] 자료구조(1) - 스택(Stack)이란 무엇일까? : https://
minosaekki.tistory.com/8
Thank you!

More Related Content

What's hot

Data structure review (summer study)
Data structure review (summer study)Data structure review (summer study)
Data structure review (summer study)Melon Lemon
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율S.O.P.T - Shout Our Passion Together
 
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자Jaewook Byun
 
LINQ란 무엇인가
LINQ란 무엇인가LINQ란 무엇인가
LINQ란 무엇인가Jungsoo Park
 
05_STL컨테이너정리
05_STL컨테이너정리05_STL컨테이너정리
05_STL컨테이너정리noerror
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리S.O.P.T - Shout Our Passion Together
 
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기Jaewook Byun
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell SortBill Kim
 
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자Jaewook Byun
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐S.O.P.T - Shout Our Passion Together
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)fmbvbfhs
 
데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayListJaewook Byun
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트S.O.P.T - Shout Our Passion Together
 
[Commit Again] 1주차 STL study
[Commit Again] 1주차 STL study[Commit Again] 1주차 STL study
[Commit Again] 1주차 STL study경 송
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산S.O.P.T - Shout Our Passion Together
 
2.2.3 part1
2.2.3 part12.2.3 part1
2.2.3 part1weasel87
 
R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R BasicsYoonwhan Lee
 

What's hot (18)

Data structure review (summer study)
Data structure review (summer study)Data structure review (summer study)
Data structure review (summer study)
 
(Lisp)
(Lisp)(Lisp)
(Lisp)
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
 
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
 
LINQ란 무엇인가
LINQ란 무엇인가LINQ란 무엇인가
LINQ란 무엇인가
 
05_STL컨테이너정리
05_STL컨테이너정리05_STL컨테이너정리
05_STL컨테이너정리
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리
[SOPT] 데이터 구조 및 알고리즘 스터디 - #05 : AVL 트리
 
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
 
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)
 
데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 3 - Java Collection Framework와 ArrayList
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
 
[Commit Again] 1주차 STL study
[Commit Again] 1주차 STL study[Commit Again] 1주차 STL study
[Commit Again] 1주차 STL study
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
 
2.2.3 part1
2.2.3 part12.2.3 part1
2.2.3 part1
 
R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R Basics
 

Similar to [Swift] Data Structure - Stack

[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - DequeueBill Kim
 
Swift3 generic
Swift3 genericSwift3 generic
Swift3 genericEunjoo Im
 
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl승혁 조
 
Collection framework
Collection frameworkCollection framework
Collection frameworkssuser34b989
 
[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
 
나에 첫번째 자바8 람다식 지앤선
나에 첫번째 자바8 람다식   지앤선나에 첫번째 자바8 람다식   지앤선
나에 첫번째 자바8 람다식 지앤선daewon jeong
 
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdfkd19h
 
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 [GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 Sehyeon Nam
 
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현Sang Goo Kwon
 
사칙연산 프로그램
사칙연산 프로그램사칙연산 프로그램
사칙연산 프로그램중선 곽
 
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03chl132435
 
자료구조 스택_큐_링크드리스트
자료구조 스택_큐_링크드리스트자료구조 스택_큐_링크드리스트
자료구조 스택_큐_링크드리스트송미 이
 
Java advancd ed10
Java advancd ed10Java advancd ed10
Java advancd ed10hungrok
 
05 컬렉션제너릭
05 컬렉션제너릭05 컬렉션제너릭
05 컬렉션제너릭rjawptlsghk
 

Similar to [Swift] Data Structure - Stack (18)

[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
 
강의자료4
강의자료4강의자료4
강의자료4
 
Swift 0x17 generics
Swift 0x17 genericsSwift 0x17 generics
Swift 0x17 generics
 
Swift3 generic
Swift3 genericSwift3 generic
Swift3 generic
 
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
 
4. stack
4. stack4. stack
4. stack
 
나에 첫번째 자바8 람다식 지앤선
나에 첫번째 자바8 람다식   지앤선나에 첫번째 자바8 람다식   지앤선
나에 첫번째 자바8 람다식 지앤선
 
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf
 
자료구조 큐
자료구조 큐자료구조 큐
자료구조 큐
 
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용 [GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
 
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현
Kgc2006 Template Metaprogramming을 이용한 LuaTinker 구현
 
사칙연산 프로그램
사칙연산 프로그램사칙연산 프로그램
사칙연산 프로그램
 
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03
 
자료구조 스택_큐_링크드리스트
자료구조 스택_큐_링크드리스트자료구조 스택_큐_링크드리스트
자료구조 스택_큐_링크드리스트
 
Java advancd ed10
Java advancd ed10Java advancd ed10
Java advancd ed10
 
05 컬렉션제너릭
05 컬렉션제너릭05 컬렉션제너릭
05 컬렉션제너릭
 

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] 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] 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 Introduction
[Swift] Data Structure Introduction[Swift] Data Structure Introduction
[Swift] Data Structure IntroductionBill Kim
 
Design Pattern Introduction
Design Pattern IntroductionDesign Pattern Introduction
Design Pattern IntroductionBill Kim
 
[Swift] Visitor
[Swift] Visitor[Swift] Visitor
[Swift] VisitorBill 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] 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] 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 Introduction
[Swift] Data Structure Introduction[Swift] Data Structure Introduction
[Swift] Data Structure Introduction
 
Design Pattern Introduction
Design Pattern IntroductionDesign Pattern Introduction
Design Pattern Introduction
 
[Swift] Visitor
[Swift] Visitor[Swift] Visitor
[Swift] Visitor
 

[Swift] Data Structure - Stack

  • 1. SWIFT Data Structure - Stack Bill Kim(김정훈) | ibillkim@gmail.com
  • 3. Stack 스택(Stack)는 리스트의 한쪽 끝에서 자료의 삽입과 삭제가 이루어 지는 자료구조입니다. 가장 최근에 들어온 자료가 가장 먼저 나가게 되는 LIFO(Last-In First-Out) 형태를 가지고 있습니다. 따라서 중간에서는 데이터를 추가 및 삭제하는 것이 불가능합니다 스택이 입출력이 이루어지는 부분을 스택 상단(Stack top) , 바닥 부분을 스택 하단(Stack bottom), 스택에 저장되는 것을 요소 (Element) 라 부르며 스택에 요소가 하나도 없을 때 그러한 스택을 공백 스택(Empty stack) 이라고 합니다.
  • 4. Concept Stack의 기본 동작 흐름은 아래와 같습니다.
  • 5. Features Stack의 특징을 살펴보면 아래와 같습니다. - 데이터를 한 곳에서 삽입, 삭제 - Last In First Out (LIFO, 후입선출) 방식 - 중간에서 데이터 삽입 및 삭제 불가 - 배열과 비슷하지만 삽입, 삭제 등이 더 제한적임
  • 6. Implementation Swift를 활용하여 Stack 을 구현해보겠습니다. 우선 필요한 메소드는 아래와 같습니다. - init : 리스트를 초기화하는 함수 - push : 데이터 입력 - pop : 마지막 데이터 삭제 - peak : 마지막 데이터 반환 - count : 현재 리스트의 크기를 반환 - isEmpty : 현재 리스트의 크기가 비어있는지 체크
  • 7. Implementation class Stack<T> { private var elements = Array<T>() public init() {} public func push(element: T) { self.elements.append(element) } public func pop() -> T? { return self.elements.popLast() } public func peek() -> T? { return self.elements.last } public var isEmpty: Bool { return self.elements.isEmpty } public var count: Int { return self.elements.count } }
  • 8. Implementation 데이터 반복(Iterator) 제어를 위하여 아래의 프로토콜들을 체택합 니다. public struct ArrayIterator<T> : IteratorProtocol { var currentElement: [T] init(elements: [T]) { self.currentElement = elements } public mutating func next() -> T? { if !self.currentElement.isEmpty { return self.currentElement.popLast() } return nil } } extension Stack : Sequence { public func makeIterator() -> ArrayIterator<T> { return ArrayIterator<T>(elements: self.elements) } }
  • 9. Implementation let stack = Stack<Int>() stack.push(1) stack.push(2) stack.push(3) stack.push(4) stack.push(5) // 현재 데이터 카운트 : 5 print(stack.count) for node in stack { print(node) // 5 // 4 // 3 // 2 // 1 }
  • 10. References [1] [스위프트 : 자료구조] 스택 : Stack : https://the-brain-of- sic2.tistory.com/38 [2] [스위프트:자료구조] 스택: Stack: 자료구조: DataStructure: 쌓기: swift : https://the-brain-of- sic2.tistory.com/18 [3] [Swift 자료구조 ch04] Stack : https:// kor45cw.tistory.com/240 [4] Swift로 작성해보는 기본 자료구조 - Stack, Queue : https://wlaxhrl.tistory.com/87 [5] Stack : https://woongsios.tistory.com/87
  • 11. References [6] 스택(Stack) : https://kka7.tistory.com/74 [7] Swift, Data Structure, Stack : https:// devmjun.github.io/archive/Stack [8] [알고리즘] 2.1. 자료구조 : 스택(Stack) 이해하기 : https:// monsieursongsong.tistory.com/4 [9] [자료구조]스택과 큐. (stack and queue) : https:// winplz.tistory.com/entry/자료구조스택과-큐-stack-and- queue [10] 자료구조(1) - 스택(Stack)이란 무엇일까? : https:// minosaekki.tistory.com/8