SlideShare a Scribd company logo
1 of 24
Download to read offline
DataAnalysis
(Lecture 6 – MyLinkedList)
1
Jaewook Byun
Ph.D. Assistant Professor, Department of Software, Sejong University
Associate Director, Auto-ID Labs, Korea
jwbyun@sejong.ac.kr , bjw0829@gmail.com
https://sites.google.com/view/jack-dfpl/home
https://www.youtube.com/channel/UC988e-Y8nto0LXVae0aqaOQ
목차
• 실 세계 데이터 분석을 LinkedList를 통해 수행
• 배열 기반, 참조 기반의 리스트 구현의 장단점을 이해 (삽입(Enqueue))
• List/Queue 인터페이스의 참조기반 자료구조 설계 및 구현
• 실 세계 데이터 분석을 MyLinkedList를 통해 수행
• 삽입(Enqueue) 측면의 배열 기반, 참조 기반의 리스트 분석
2
I. LinkedList를 통한 실 세계 데이터 분석
1) List로써의 LinkedList를 이용한 간단한 데이터 분석
3
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
유튜브: https://youtu.be/VjOdl0Z5cis
I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
4
1. 삽입 (Enqueue)
유튜브: https://youtu.be/3nenst1o8zQ
1
0
9
1
7
2
myArrayList
새로운 instance 마지막에 삽입
start last
9
1
null null
7
offer(5)
1
0 1 2 3
새로운 배열 생성
1
0 1 2 3
인덱스 0 채우기
1
0
9
1 2 3
인덱스 1 채우기 (복사시작)
1
0
9
1
7
2
5
3
새로운 instance 추가
1 2 3 4
1
0
9
1
7
2 3
인덱스 2 채우기 (복사완료)
5
start last
9
1
null null
myLinkedList
7 5
offer(5)
I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
5
2. Index 기반 순회
유튜브: https://youtu.be/8cR__Oi0210
myArrayList
1
0
9
1
7
2
5
3
get(2) 1
인덱스 2 직접 접근
1
0
9
1
5
7
3
2
myLinkedList
start last
9
1
null
7 null
5
start last
9
1
null
7 null
5
1 2
get(2)
start 부터 순회 시작
3
I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
6
3. 추출 (Dequeue)
https://youtu.be/6AQx4kC424E
poll()
poll() 1
0 1 2
새로운 배열 생성
9
0 1 2
0 이후 요소 복사 시작
1 2 3 4
myLinkedList
start last
9
1
null
7 null
5
myArrayList
1
0
9
1
7
2
5
3
9
0
7
1 2
9
0
7
1
5
2
나머지 요소 복사 완료
start last
9
1
null
7 null
5
null
참조의 재조정으로 효율적으로 해결
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
1) LinkedList 복습
7
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add​(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
void add(int index, E element) List의 특정 위치에 instance를 삽입
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
E get(int index) List의 특정 위치에 있는 instance를 추출
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환)
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
void clear() Collection을 비움
Object[] toArray​() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
List<E> subList(int from, int to) List의 부분을 가져온다
META
C
R
D
T
U
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
2) MyLinkedList 설계 전략
8
MyNode
prev
MyNode
next
E
item
MyNode:
3
MyLinkedList:
first last
2
1
null null
size = 3
index = 0
get(0) → 1
Poll
Offer
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
3) MyLinkedList 구현 준비
9
• MyNode<E> 생성
• MyNode<E> prev;
• E item;
• MyNode <E> next;
• List<E> 와 Queue<E> 인터페이스를 구현한 MyLinkedList<E> 생성
• MyNode<E> first;
• MyNode<E> last;
• int size;
• 유튜브: https://youtu.be/I5fZVmGzJiE
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
10
• size가 0이면 true
• 유튜브: https://youtu.be/aeFgEHp6G9k
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
3
first last
2
1
null null
size = 3
return size == 0;
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
11
• size를 반환
• 유튜브: https://youtu.be/873l54fbLIE
Return Type Method Description
int size() Collection의 크기를 반환
3
first last
2
1
null null
size = 3
return size;
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
12
Return Type Method Description
boolean add​(E e) Collection에 새로운 instance를 삽입
• MyLinkedList가 비어 있지 않을 때
• 유튜브: https://youtu.be/I40FfKGdelQ
3
first
2
1
null null
size = 3
add(3)
(1) MyNode newNode 생성
prev = last;
item = e;
next = null;
(2) last.next 참조 갱신
last.next = newNode;
last
(3) last 갱신
(4) size++
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
13
• size가 0이면 []를 반환
• size가 0이 아니라면, first 부터 시작하여 getNext()가 null일 때까지 순회하며 문자열을
생성하여 반환
• 유튜브: https://youtu.be/dwtKUYFV-CA
Return Type Method Description
String toString() MyLinkedList의 문자열 표현을 반환한다.
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
14
• for-each loop를 이용하여 구현
• 유튜브: https://youtu.be/pa9MuYx17cQ
Return Type Method Description
생성자 MyLinkedList() 빈 LinkedList를 생성
생성자 MyLinkedList(Collection c) LinkedList를 Collection c의 요소를 가져와 생성
JVM Heap
MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>();
MyLinkedList
[ ]
1. 빈 리스트 생성
2. 주소할당
Collection<Integer> collection = arrayList; // [0,1,2,3,4] ArrayList
MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>(collection);
MyLinkedList
[0,1,2,3,4]
1. 컬렉션으로 리스트 생성
2. 주소할당
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
15
Return Type Method Description
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
E get(int index) List의 특정 위치에 있는 instance를 추출
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
3
first last
2
1
null null
2
contains(o)
get(index)
indexOf(o)
(1) item이 o인지 확인하거나
index가 맞는지 확인
(2) 맞다면,
true, item, index 반환
(3) 틀리다면, next로 계속 이동하여 (1) (2) 수행
(4) next가 null에 도달시,
false, Exception, -1 반환
사전에 throw됨
유튜브: https://youtu.be/ugeWL5DD1QE
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
16
유튜브: https://youtu.be/Z_6Kv2ussZ0
Return Type Method Description
void add(int index, E element) List의 특정 위치에 instance를 삽입
• MyLinkedList의 마지막에 추가할 경우
• add(E e)와 같음
• MyLinkedList의 중간에 추가할 경우
관찰:
• first 와 last는 다르다
• next를 get(index)를 갖고 있는 노드로 지정
• prev를 get(index)를 갖고 있는 노드의 prev로 지정
• 새로운 노드는 next, prev 사이에 위치
• first와 prev는 같을 수 있음
• next와 last는 같을 수 있음
• first가 null일 때, 새로 추가될 노드는 first가 됨
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
17
유튜브: https://youtu.be/IVx3iUlbd4A
Return Type Method Description
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
관찰:
• 만약 prev이 null이라면
• 지워질 노드는 first였었음
• 새로운 first는 next가 됨
• 만약 prev가 null이 아니라면
• prev.setNext(next)로 prev와 next를 연결해준다.
• 만약 next이 null이라면
• 지워질 노드는 last였었음
• 새로운 last는 prev가 됨
• 만약 next가 null이 아니라면
• next.setPrev(prev)로 prev와 next를 연결해준다.
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
18
유튜브: https://youtu.be/9r-u4TSadEI
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
3
start last
2
1
null null
index: -1 index: 0 index: 1 index: 2
(size-1)
index: size
List list에 대한 Cursor와 메소드 동작
초기 cursor 값
iterator() → -1
listIterator(int index) → index -1
즉, iterator() == listIterator(0)
previousIndex() cursor 반환
previous() list.get(cursor) 반환 후 cursor-- 수행
hasPrevious() list.get(cursor) == null 반환
nextIndex() cursor + 1 반환
next() list.get(cursor+1) 반환 후 cursor++ 수행
hasNext() list.get(cursor+1) == null 반환
ListIterator<Integer> ai = list.listIterator(0);
while (ai.hasNext()) {
Integer value = ai.next(); // Do Something
}
ListIterator<Integer> di =
list.listIterator(list.size());
while (di.hasPrevious()) {
Integer value = di.previous();// Do Something
}
순회 패턴1: 앞에서 부터
순회 패턴2: 뒤에서 부터
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
19
유튜브: https://youtu.be/Bbz8UYsbE7w
• toArray(): size만큼의 Object[] 생성 후, iterator를 이용하여 추가
• sort: toArray와 listIterator의 구현으로 동작을 보장
Return Type Method Description
Object[] toArray​() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
인덱스
값
Object[]
toArray()
sort(c)
subList(2,4)
first last
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
20
유튜브: https://youtu.be/fSWMkYQJAR4
• add, offer: capacity가 없으므로 구현이 동일
• clear(): remove(0)를 계속 호출하여 구현
Return Type Method Description
boolean add​(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
void clear() Collection을 비움
first last
2
1 3
4
last
first
2
1 3
add(4)
offer(4)
first last
null
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
21
유튜브: https://youtu.be/giUWhdZFJd4
Return Type Method Description
E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환)
first last
2
1 3
element()
peek()
1 반환
first last
null
element()
peek() null 반환
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
22
유튜브: https://youtu.be/keSPEaWFxws
Return Type Method Description
E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
start last
2
1 3
start
2
1 3
remove()
poll()
last
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
23
유튜브: https://youtu.be/zi2HewCHpiA
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
24
유튜브: https://youtu.be/vqFleHXOOso
1. 삽입 (Enqueue)
1
0
9
1
7
2
myArrayList
새로운 instance 마지막에 삽입
start last
9
1
null null
7
offer(5)
1
0 1 2 3
새로운 배열 생성
1
0 1 2 3
인덱스 0 채우기
1
0
9
1 2 3
인덱스 1 채우기 (복사시작)
1
0
9
1
7
2
5
3
새로운 instance 추가
1 2 3 4
1
0
9
1
7
2 3
인덱스 2 채우기 (복사완료)
5
start last
9
1
null null
myLinkedList
7 5
offer(5)

More Related Content

What's hot

Linq to object using c#
Linq to object using c#Linq to object using c#
Linq to object using c#
병걸 윤
 
SpringCamp 2013 : About Jdk8
SpringCamp 2013 : About Jdk8SpringCamp 2013 : About Jdk8
SpringCamp 2013 : About Jdk8
Sangmin Lee
 
빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)
SeongHyun Ahn
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)
fmbvbfhs
 
10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스
유석 남
 

What's hot (20)

Linq to object using c#
Linq to object using c#Linq to object using c#
Linq to object using c#
 
SpringCamp 2013 : About Jdk8
SpringCamp 2013 : About Jdk8SpringCamp 2013 : About Jdk8
SpringCamp 2013 : About Jdk8
 
Python 활용: 이미지 처리와 데이터 분석
Python 활용: 이미지 처리와 데이터 분석Python 활용: 이미지 처리와 데이터 분석
Python 활용: 이미지 처리와 데이터 분석
 
빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)
 
Fp basic-kotlin
Fp basic-kotlinFp basic-kotlin
Fp basic-kotlin
 
Java collection
Java collectionJava collection
Java collection
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
 
Python programming for Bioinformatics
Python programming for BioinformaticsPython programming for Bioinformatics
Python programming for Bioinformatics
 
Scala
ScalaScala
Scala
 
Python Programming: Function
Python Programming: FunctionPython Programming: Function
Python Programming: Function
 
알고리즘과 자료구조
알고리즘과 자료구조알고리즘과 자료구조
알고리즘과 자료구조
 
JVM 메모리 해부학
JVM 메모리 해부학JVM 메모리 해부학
JVM 메모리 해부학
 
Just java
Just javaJust java
Just java
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)
 
10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스10장 문자열 클래스와 파일 클래스
10장 문자열 클래스와 파일 클래스
 
Falcor overview
Falcor overviewFalcor overview
Falcor overview
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
 
[Swift] Iterator
[Swift] Iterator[Swift] Iterator
[Swift] Iterator
 
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
 

Similar to 데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자

9 object class
9 object class9 object class
9 object class
웅식 전
 
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
실용주의 디자인패턴   2 인터페이스로 프로그래밍하기실용주의 디자인패턴   2 인터페이스로 프로그래밍하기
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
Cosmos Shin
 
Project#3다항식의연산 Hwp
Project#3다항식의연산 HwpProject#3다항식의연산 Hwp
Project#3다항식의연산 Hwp
Kimjeongmoo
 
자료구조3보고서
자료구조3보고서자료구조3보고서
자료구조3보고서
KimChangHoen
 

Similar to 데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자 (20)

Java_08 collection
Java_08 collectionJava_08 collection
Java_08 collection
 
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
 
집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking
집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking
집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking
 
5. queue
5. queue5. queue
5. queue
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
 
08장 객체와 클래스 (기본)
08장 객체와 클래스 (기본)08장 객체와 클래스 (기본)
08장 객체와 클래스 (기본)
 
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
 
Light Tutorial Python
Light Tutorial PythonLight Tutorial Python
Light Tutorial Python
 
Swift3 generic
Swift3 genericSwift3 generic
Swift3 generic
 
자료구조 큐
자료구조 큐자료구조 큐
자료구조 큐
 
20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POP20220716_만들면서 느껴보는 POP
20220716_만들면서 느껴보는 POP
 
9 object class
9 object class9 object class
9 object class
 
자바 테스트 자동화
자바 테스트 자동화자바 테스트 자동화
자바 테스트 자동화
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initialization
 
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
실용주의 디자인패턴   2 인터페이스로 프로그래밍하기실용주의 디자인패턴   2 인터페이스로 프로그래밍하기
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
 
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
 
Lecture 1: Introduction to Python and TensorFlow
Lecture 1: Introduction to Python and TensorFlowLecture 1: Introduction to Python and TensorFlow
Lecture 1: Introduction to Python and TensorFlow
 
Data structure review (summer study)
Data structure review (summer study)Data structure review (summer study)
Data structure review (summer study)
 
Project#3다항식의연산 Hwp
Project#3다항식의연산 HwpProject#3다항식의연산 Hwp
Project#3다항식의연산 Hwp
 
자료구조3보고서
자료구조3보고서자료구조3보고서
자료구조3보고서
 

데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자

  • 1. DataAnalysis (Lecture 6 – MyLinkedList) 1 Jaewook Byun Ph.D. Assistant Professor, Department of Software, Sejong University Associate Director, Auto-ID Labs, Korea jwbyun@sejong.ac.kr , bjw0829@gmail.com https://sites.google.com/view/jack-dfpl/home https://www.youtube.com/channel/UC988e-Y8nto0LXVae0aqaOQ
  • 2. 목차 • 실 세계 데이터 분석을 LinkedList를 통해 수행 • 배열 기반, 참조 기반의 리스트 구현의 장단점을 이해 (삽입(Enqueue)) • List/Queue 인터페이스의 참조기반 자료구조 설계 및 구현 • 실 세계 데이터 분석을 MyLinkedList를 통해 수행 • 삽입(Enqueue) 측면의 배열 기반, 참조 기반의 리스트 분석 2
  • 3. I. LinkedList를 통한 실 세계 데이터 분석 1) List로써의 LinkedList를 이용한 간단한 데이터 분석 3 1. 이벤트의 수 구하기 2. 최소 사람 ID를 구하기 3. 최대 사람 ID를 구하기 유튜브: https://youtu.be/VjOdl0Z5cis
  • 4. I. LinkedList를 통한 실 세계 데이터 분석 2) ArrayList<Email> vs. LinkedList<Email> 4 1. 삽입 (Enqueue) 유튜브: https://youtu.be/3nenst1o8zQ 1 0 9 1 7 2 myArrayList 새로운 instance 마지막에 삽입 start last 9 1 null null 7 offer(5) 1 0 1 2 3 새로운 배열 생성 1 0 1 2 3 인덱스 0 채우기 1 0 9 1 2 3 인덱스 1 채우기 (복사시작) 1 0 9 1 7 2 5 3 새로운 instance 추가 1 2 3 4 1 0 9 1 7 2 3 인덱스 2 채우기 (복사완료) 5 start last 9 1 null null myLinkedList 7 5 offer(5)
  • 5. I. LinkedList를 통한 실 세계 데이터 분석 2) ArrayList<Email> vs. LinkedList<Email> 5 2. Index 기반 순회 유튜브: https://youtu.be/8cR__Oi0210 myArrayList 1 0 9 1 7 2 5 3 get(2) 1 인덱스 2 직접 접근 1 0 9 1 5 7 3 2 myLinkedList start last 9 1 null 7 null 5 start last 9 1 null 7 null 5 1 2 get(2) start 부터 순회 시작 3
  • 6. I. LinkedList를 통한 실 세계 데이터 분석 2) ArrayList<Email> vs. LinkedList<Email> 6 3. 추출 (Dequeue) https://youtu.be/6AQx4kC424E poll() poll() 1 0 1 2 새로운 배열 생성 9 0 1 2 0 이후 요소 복사 시작 1 2 3 4 myLinkedList start last 9 1 null 7 null 5 myArrayList 1 0 9 1 7 2 5 3 9 0 7 1 2 9 0 7 1 5 2 나머지 요소 복사 완료 start last 9 1 null 7 null 5 null 참조의 재조정으로 효율적으로 해결
  • 7. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 1) LinkedList 복습 7 Return Type Method Description boolean isEmpty() Collection이 비어 있는지 확인 int size() Collection의 크기를 반환 boolean add​(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생) void add(int index, E element) List의 특정 위치에 instance를 삽입 boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환) boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 E get(int index) List의 특정 위치에 있는 instance를 추출 int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터) int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터) E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생) E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환) E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트 boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 E remove(int index) List의 특정 위치에 있는 instance를 삭제 E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생) E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환) Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 void clear() Collection을 비움 Object[] toArray​() Collection을 배열에 담음 void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함 List<E> subList(int from, int to) List의 부분을 가져온다 META C R D T U
  • 8. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 2) MyLinkedList 설계 전략 8 MyNode prev MyNode next E item MyNode: 3 MyLinkedList: first last 2 1 null null size = 3 index = 0 get(0) → 1 Poll Offer
  • 9. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 3) MyLinkedList 구현 준비 9 • MyNode<E> 생성 • MyNode<E> prev; • E item; • MyNode <E> next; • List<E> 와 Queue<E> 인터페이스를 구현한 MyLinkedList<E> 생성 • MyNode<E> first; • MyNode<E> last; • int size; • 유튜브: https://youtu.be/I5fZVmGzJiE
  • 10. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 10 • size가 0이면 true • 유튜브: https://youtu.be/aeFgEHp6G9k Return Type Method Description boolean isEmpty() Collection이 비어 있는지 확인 3 first last 2 1 null null size = 3 return size == 0;
  • 11. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 11 • size를 반환 • 유튜브: https://youtu.be/873l54fbLIE Return Type Method Description int size() Collection의 크기를 반환 3 first last 2 1 null null size = 3 return size;
  • 12. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 12 Return Type Method Description boolean add​(E e) Collection에 새로운 instance를 삽입 • MyLinkedList가 비어 있지 않을 때 • 유튜브: https://youtu.be/I40FfKGdelQ 3 first 2 1 null null size = 3 add(3) (1) MyNode newNode 생성 prev = last; item = e; next = null; (2) last.next 참조 갱신 last.next = newNode; last (3) last 갱신 (4) size++
  • 13. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 13 • size가 0이면 []를 반환 • size가 0이 아니라면, first 부터 시작하여 getNext()가 null일 때까지 순회하며 문자열을 생성하여 반환 • 유튜브: https://youtu.be/dwtKUYFV-CA Return Type Method Description String toString() MyLinkedList의 문자열 표현을 반환한다.
  • 14. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 14 • for-each loop를 이용하여 구현 • 유튜브: https://youtu.be/pa9MuYx17cQ Return Type Method Description 생성자 MyLinkedList() 빈 LinkedList를 생성 생성자 MyLinkedList(Collection c) LinkedList를 Collection c의 요소를 가져와 생성 JVM Heap MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>(); MyLinkedList [ ] 1. 빈 리스트 생성 2. 주소할당 Collection<Integer> collection = arrayList; // [0,1,2,3,4] ArrayList MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>(collection); MyLinkedList [0,1,2,3,4] 1. 컬렉션으로 리스트 생성 2. 주소할당
  • 15. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 15 Return Type Method Description boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 E get(int index) List의 특정 위치에 있는 instance를 추출 int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터) int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터) 3 first last 2 1 null null 2 contains(o) get(index) indexOf(o) (1) item이 o인지 확인하거나 index가 맞는지 확인 (2) 맞다면, true, item, index 반환 (3) 틀리다면, next로 계속 이동하여 (1) (2) 수행 (4) next가 null에 도달시, false, Exception, -1 반환 사전에 throw됨 유튜브: https://youtu.be/ugeWL5DD1QE
  • 16. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 16 유튜브: https://youtu.be/Z_6Kv2ussZ0 Return Type Method Description void add(int index, E element) List의 특정 위치에 instance를 삽입 • MyLinkedList의 마지막에 추가할 경우 • add(E e)와 같음 • MyLinkedList의 중간에 추가할 경우 관찰: • first 와 last는 다르다 • next를 get(index)를 갖고 있는 노드로 지정 • prev를 get(index)를 갖고 있는 노드의 prev로 지정 • 새로운 노드는 next, prev 사이에 위치 • first와 prev는 같을 수 있음 • next와 last는 같을 수 있음 • first가 null일 때, 새로 추가될 노드는 first가 됨
  • 17. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 17 유튜브: https://youtu.be/IVx3iUlbd4A Return Type Method Description E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트 boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 E remove(int index) List의 특정 위치에 있는 instance를 삭제 관찰: • 만약 prev이 null이라면 • 지워질 노드는 first였었음 • 새로운 first는 next가 됨 • 만약 prev가 null이 아니라면 • prev.setNext(next)로 prev와 next를 연결해준다. • 만약 next이 null이라면 • 지워질 노드는 last였었음 • 새로운 last는 prev가 됨 • 만약 next가 null이 아니라면 • next.setPrev(prev)로 prev와 next를 연결해준다.
  • 18. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 18 유튜브: https://youtu.be/9r-u4TSadEI Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 3 start last 2 1 null null index: -1 index: 0 index: 1 index: 2 (size-1) index: size List list에 대한 Cursor와 메소드 동작 초기 cursor 값 iterator() → -1 listIterator(int index) → index -1 즉, iterator() == listIterator(0) previousIndex() cursor 반환 previous() list.get(cursor) 반환 후 cursor-- 수행 hasPrevious() list.get(cursor) == null 반환 nextIndex() cursor + 1 반환 next() list.get(cursor+1) 반환 후 cursor++ 수행 hasNext() list.get(cursor+1) == null 반환 ListIterator<Integer> ai = list.listIterator(0); while (ai.hasNext()) { Integer value = ai.next(); // Do Something } ListIterator<Integer> di = list.listIterator(list.size()); while (di.hasPrevious()) { Integer value = di.previous();// Do Something } 순회 패턴1: 앞에서 부터 순회 패턴2: 뒤에서 부터
  • 19. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 19 유튜브: https://youtu.be/Bbz8UYsbE7w • toArray(): size만큼의 Object[] 생성 후, iterator를 이용하여 추가 • sort: toArray와 listIterator의 구현으로 동작을 보장 Return Type Method Description Object[] toArray​() Collection을 배열에 담음 void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함 인덱스 값 Object[] toArray() sort(c) subList(2,4) first last
  • 20. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 20 유튜브: https://youtu.be/fSWMkYQJAR4 • add, offer: capacity가 없으므로 구현이 동일 • clear(): remove(0)를 계속 호출하여 구현 Return Type Method Description boolean add​(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생) boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환) void clear() Collection을 비움 first last 2 1 3 4 last first 2 1 3 add(4) offer(4) first last null
  • 21. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 21 유튜브: https://youtu.be/giUWhdZFJd4 Return Type Method Description E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생) E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환) first last 2 1 3 element() peek() 1 반환 first last null element() peek() null 반환
  • 22. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 22 유튜브: https://youtu.be/keSPEaWFxws Return Type Method Description E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생) E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환) start last 2 1 3 start 2 1 3 remove() poll() last
  • 23. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 23 유튜브: https://youtu.be/zi2HewCHpiA 1. 이벤트의 수 구하기 2. 최소 사람 ID를 구하기 3. 최대 사람 ID를 구하기
  • 24. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현 4) MyLinkedList 구현 24 유튜브: https://youtu.be/vqFleHXOOso 1. 삽입 (Enqueue) 1 0 9 1 7 2 myArrayList 새로운 instance 마지막에 삽입 start last 9 1 null null 7 offer(5) 1 0 1 2 3 새로운 배열 생성 1 0 1 2 3 인덱스 0 채우기 1 0 9 1 2 3 인덱스 1 채우기 (복사시작) 1 0 9 1 7 2 5 3 새로운 instance 추가 1 2 3 4 1 0 9 1 7 2 3 인덱스 2 채우기 (복사완료) 5 start last 9 1 null null myLinkedList 7 5 offer(5)