π£ 2. Data Structures
π 1. λ°°μ΄κ³Ό λ¬Έμμ΄
1. Hash Tables
- ν΄μν μ΄λΈ : ν¨μ¨μ μΈ νμμ μν μλ£κ΅¬μ‘°
ν€(key)λ₯Ό κ°(value)μ λμνλ€.
κ°λ¨ν ν΄μν μ΄λΈ ꡬν λ°©λ²
- μ°κ²°λ¦¬μ€νΈ(linked list), ν΄μ μ½λ ν¨μ(hash code function)
- ν΄μν
μ΄λΈ λ°μ΄ν° μ½μ
κ³Όμ
- ν€μ ν΄μ μ½λλ₯Ό κ³μ°νλ€.
- ν€μ μλ£νμ 보ν΅
int
νΉμlong
μ΄ λλ€. - β‘οΈ μ΄λ μλ‘ λ€λ₯Έ λ κ°μ ν€κ° κ°μ ν΄μ μ½λλ₯Ό κ°λ¦¬ν¬ μ μμμ λνλ!
- ν€μ μλ£νμ 보ν΅
hash(key) % array_length
μ κ°μ λ°©μμΌλ‘ ν΄μ μ½λλ₯Ό μ΄μ©ν΄, λ°°μ΄μ μΈλ±μ€λ₯Ό ꡬνλ€.- λ°°μ΄μ κ° μΈλ±μ€μλ ν€-κ° μΌλ‘ μ΄λ£¨μ΄μ§ μ°κ²°λ¦¬μ€νΈκ° μ‘΄μ¬νλ€.
- μΆ©λμ λλΉνμ¬ λ°λμ μ°κ²°λ¦¬μ€νΈλ₯Ό μ΄μ©νλ€!
- ν€μ ν΄μ μ½λλ₯Ό κ³μ°νλ€.
π€ ν΄μ μΆ©λ?
- μλ‘ λ€λ₯Έ λ κ°μ ν€κ° κ°μ ν΄μ μ½λλ₯Ό κ°λ¦¬ν€κ±°λ
- μλ‘ λ€λ₯Έ λ κ°μ ν΄μ μ½λκ° κ°μ μΈλ±μ€λ₯Ό κ°λ¦¬ν€λ κ²½μ°
π μν μκ°
- μ΅μ μ κ²½μ° : $O(N)$
- But μΌλ°μ μΌλ‘ ν΄μμ λν΄ μ΄μΌκΈ°ν λλ μΆ©λμ μ΅μννλλ‘ μ ꡬνλ κ²½μ°λ₯Ό κ°μ νλ€. β‘οΈ μ΄ κ²½μ°λ O(1)
2. ArrayList & Resizable Arraya
ArrayList : λμ κ°λ³ ν¬κΈ° κΈ°λ₯ λ΄μ¬ μλ£ν
- ArrayListλ νμμ λ°λΌ ν¬κΈ°λ₯Ό λ³νμν¬ μ μμΌλ©΄μ $O(1)$ μ μ κ·Ό μκ°μ μ μ§νλ€.
- λ°°μ΄μ΄ κ°λ μ°¨λ μκ°, λ°°μ΄μ ν¬κΈ°λ₯Ό 2λ°°λ‘ λλ¦°λ€.
ν¬κΈ°λ₯Ό 2λ°° λ리λ μ°μ°μ $O(N)$ μ΄μ§λ§ μμ£Ό λ°μνμ§ μμΌλ―λ‘, $O(1)$μ΄ λλ€.
- _ βοΈμν μ λ ₯ μκ° κ°λ λμ !_
3. StringBuilder
- λ¬Έμμ΄μ νλλ‘ μ΄μ΄λΆμ΄λ λ°©λ²
- ex1
1
2
3
4
5
6
7
String joinWords(String[] words){
String sentence = "";
for (String w : words) {
sentence = sentence + w;
}
return sentence;
}
- λ¬Έμμ΄μ μ΄μ΄λΆμΌ λλ§λ€ μλ‘μ΄ λ¬Έμμ΄μ 볡μ¬νλ λ°©λ²
- μ²μμλ xκ°, λ λ²μ§Έλ 2xκ°, μΈ λ²μ§Έλ 3xκ°, nλ²μ§Έλ nxκ°μ λ¬Έμλ₯Ό 볡μ¬νλ€.
μκ°λ³΅μ‘λ : $O(xn^2)$
- $1 + 2 + β¦ + n = n(n + 1)/2$
- ex2
1
2
3
4
5
6
7
String joinWords(String[] words) {
StringBuilder sentence = new StringBuilder();
for (String w : words) {
sentence.append(w);
}
return sentence.toString;
}
StringBuilder
λ₯Ό μ΄μ©νμ¬ κ°λ³ ν¬κΈ° λ°°μ΄μ μ¬μ©νλ λ°©λ²- νμν κ²½μ°μλ§ λ¬Έμμ΄μ 볡μ¬νκ²λ νλ€.
π 2. μ°κ²°λ¦¬μ€νΈ
- μ°κ²°λ¦¬μ€νΈ : μ°¨λ‘λ‘ μ°κ²°λ λ Έλλ₯Ό ννν΄μ£Όλ μλ£κ΅¬μ‘°
- λ¨λ°©ν₯ μ°κ²°λ¦¬μ€νΈ : κ°λ³ λ Έλ β‘οΈ λ€μ λ Έλ
- μλ°©ν₯ μ°κ²°λ¦¬μ€νΈ : κ°λ³ λ Έλ β‘οΈ λ€μ λ Έλ & μ΄μ λ Έλ
- βοΈ μμ μ§μ μμμ μμ΄ν μΆκ° λ° μμ μ°μ°μ΄ μμ μκ° μμ
1. Creating a Linked List
- λ¨λ°©ν₯ μ°κ²°λ¦¬μ€νΈ ꡬν μ½λ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
void appendToTail(int d){
Node end = new Node(d);
Node n = this;
while(n.next != null){
n = n.next;
}
n.next = end;
}
}
- λ¨λ°©ν₯ μ°κ²°λ¦¬μ€νΈμμμ κ³ λ €μ¬ν :
head
κ° λ°λλ©΄ μ΄λ»κ² λλκ°?- π
head
κ° λ°λμμμλ μ΄λ€ κ°μ²΄λhead
λ₯Ό κ³μ κ°λ¦¬ν€κ³ μμ μλ μλ€. Node
ν΄λμ€λ₯Ό ν¬ν¨νλLinkedList
ν΄λμ€λ₯Ό λ§λ λ€.(head Node
λ³μμ headλ₯Ό κ°λ¦¬ν€λ κ° μ μ₯)
- π
2. Deleting a Node from a Singly Linked List
- μμ λ
Έλ : n
prev.next
λ₯Όn.next
λ‘ μ°κ²°νλ€.- (μλ°©ν₯ μ°κ²°λ¦¬μ€νΈμΌ κ²½μ°)
n.next.prev
λ₯Όn.prev
λ‘ μ°κ²°νλ€.
- β οΈ λ©λͺ¨λ¦¬ κ΄λ¦¬κ° νμν μΈμ΄λ₯Ό μ¬μ©ν΄ ꡬννλ κ²½μ°μλ μμ ν λ Έλμ ν λΉλμλ λ©λͺ¨λ¦¬κ° μ λλ‘ λ°νλμλμ§ νμΈ νμ!
- λ Έλ μμ μ½λ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) {
return head.next;
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head;
}
n = n.next;
}
return head;
}
3. The βRunnerβ Technique
- Runner : μ°κ²°λ¦¬μ€νΈλ₯Ό μνν λ λ κ°μ ν¬μΈν°λ₯Ό λμμ μ¬μ©νλ€.
- ν ν¬μΈν°κ° λ€λ₯Έ ν¬μΈν°λ³΄λ€ μμλλ‘ νκ³ ν¬μΈν°λ₯Ό μμ§μΌ λ μ§μ λ κ°μ νΉμ μ¬λ¬ λ Έλλ₯Ό νλ²μ μμ§μΌ μ μλλ‘ μ€μ νλ€.
4. Recursive Problems
- μ°κ²°λ¦¬μ€νΈ λ¬Έμ β μ¬κ· νΈμΆ
- β οΈ μ¬κ·(recursive) μκ³ λ¦¬μ¦μ μ μ΄λ $O(n)$μ κ³΅κ° λ³΅μ‘λλ₯Ό κ°λλ€!
πΒ 1. CS50 2020 - Lecture 2 - Arrays
1
int scores[3];
- a chunk of memory
- μ°μλ κ°μΌλ‘ μ λ ₯λλ€.
1
2
3
4
int scores[3];
scores[0] = 72;
scores[1] = 73;
scores[2] = 33;
π‘ μΌμ’ μ νλ‘κ·Έλλ° κ΄λ‘λ‘ 0λΆν° μΈλ±μ€λ₯Ό μμνλ€.
- λ°°μ΄μ μ μΈν λ β[3]βκ³Ό κ°μ ννμ μ°λ λ°, μ΄λ μ΄ κ°μ μλ₯Ό λνλΈλ€.
- λ°°μ΄μ μμΈμ μμ±ν λ, ν΄λΉ λ©λͺ¨λ¦¬ μ²ν¬μ νΉμ μμΉλ‘ μ΄λνλ©΄ μ μ¬ν μ«μλ₯Ό μ¬μ©νλ€.
- β‘οΈ μλμ μΈ μμΉλ₯Ό μ°Έμ‘°(첫 λ²μ§Έ, λ λ²μ§Έ, μΈ λ²μ§Έ)
πΒ 2. Arrays | Coursera
1. Arrays
Array : λ©λͺ¨λ¦¬μ μ°μμ μΈ μμ
- νλμ λ©λͺ¨λ¦¬ μ²ν¬
Key Point : random access
- λ°°μ΄μ μ΄λ νΉμ ν μμμ μ κ·Όν μ μλ€.
- βConstant time access to read, Constant time access to wrightβ
- κ° μμμ μμμ λͺ¨λ κ°μ μ¬μ΄μ¦λ‘ ν λΉμ΄ λλ€.
- μμμ μ£Όμλ₯Ό λμΆνλ μμ
2. Multi-Dimensional Arrays
row - column
μμΌλ‘ ννμ΄ κ°λ₯νλ€.μ κ·Ό λ°©λ²
- μ κ·Όνλ €λ μμ μ΄μ μ νλ€μ 건λλ΄λ€.
- ν νμ λͺ¨λ μμλ€μ κ°μλ₯Ό κ³±νλ€.
2-1. row-major ordering & column-major ordering
_(μ’ : row-major ordering | μ° : column-major ordering)_ |
λ©λͺ¨λ¦¬μλ λ€μκ³Ό κ°μ΄ λ°°μΉλλ€.
- row-major ordering : μμκ° νμ λ°λΌ μ°μμ μΌλ‘ λ°°μΉλλ λ°©μ
- column-major ordering : μμκ° μ΄μ λ°λΌ μ°μμ μΌλ‘ λ°°μΉλλ λ°©μ
- μΈμ΄μ μ»΄νμΌλ¬λ§λ€ λ€λ₯΄λ€.
3. Times for Common Operations
- beginning : nκ° μμ μ¬μ λ ¬
- end : μ 체 μμμ κ°μλ§ μ λ°μ΄νΈνλ©΄ λ¨
middle : n/2κ° μμ μ¬μ λ ¬
- β ν΅μ¬ : μμμ μ κ·Ό/μ½κΈ°/μ°κΈ°μ μΌμ ν μκ°μ΄ μμλλ€.
β¨ Summary
- Array : contiguous area of memory consisting of equal-size elements indexed by contiguous integers.
- Constant-time access to any element.
- Constant time to add/remove at the end.
- Linear time to add/remove at an arbitrary location.
πΒ 3. Dynamic Arrays | Coursera
1. Problem
λ¬Έμ μ¬ν : λ°°μ΄μ μ μ μ΄λ€.
- ν΄κ²° : λ°νμμμ ν¬κΈ°κ° κ²°μ λλ λμ κ°λ³ λ°°μ΄μ μ¬μ©νλ€.
Solution : dynamic arrays
- λμ μΌλ‘ ν λΉλλ λ°°μ΄μ ν¬μΈν°λ₯Ό μ μ₯νκ³ μλ‘ ν λΉμ΄ νμν λμλ ν΄λΉ ν¬μΈν°λ₯Ό λ°κΎΌλ€.
- μ΄μ λ°°μ΄μ μμλ€μ 볡μ¬νκ³ μ΄μ λ°°μ΄μ μμΉλ₯Ό κ°λ¦¬ν€λ ν¬μΈν° κ°μ μλ‘μ΄ λ°°μ΄μ κ°λ¦¬ν€λ ν¬μΈν° κ°μΌλ‘ λ°κΎΌλ€.
Operations
get(i)
: iλ²μ§Έμ μμΉν μμset(i, val)
: iλ²μ§Έ μμΉμ val μ½μpushback(val)
: valμ λ§μ§λ§ μμΉμ μ½μremove(i)
: iλ²μ§Έμ μμΉν μμ μ κ±°size(i)
: μ 체 μμ κ°μ
- size :vs: capacity
- λ°°μ΄μ΄ λ€ μ°Όμ κ²½μ°
- μλ ν¬κΈ°μ λ λ°°λ§νΌ μλ‘μ΄ λ°°μ΄μ λ§λ λ€.
- κΈ°μ‘΄ μμλ€μ 볡μ¬νμ¬ λ£λλ€.
- μλμ ν¬μΈν° μμΉλ₯Ό μ λ°°μ΄μ ν¬μΈν° μμΉλ‘ λ°κΎΌλ€.
2. Common Implementations
- C++ : vector
- Java : ArrayList
- Python : list
- pythonμμλ μ μ λ°°μ΄μ ννν μ μλ€.
3. Runtimes
get(i)
: $O(1)$set(i, val)
: $O(1)$pushback(val)
: $O(n)$remove(i)
: $O(n)$size()
: $O(1)$
β¨ Summary
- Unlike static arrays, dynamic arrays can be resized.
- Appending a new element to a dynamic array is often constant time, but can take $O(n)$.
- Some space is wasted.
π 4. Implementation Arrays
Implement a vector (mutable array with automatic resizing)
- Practice coding using arrays and pointers, and pointer math to jump to an index instead of using indexing.
- New raw data array with allocated memory
- can allocate int array under the hood, just not use its features
- start with 16, or if starting number is greater, use power of 2 - 16, 32, 64, 128
- size() - number of items
- capacity() - number of items it can hold
- is_empty()
- at(index) - returns item at given index, blows up if index out of bounds
- push(item)
- insert(index, item) - inserts item at index, shifts that indexβs value and trailing elements to the right
- prepend(item) - can use insert above at index 0
- pop() - remove from end, return value
- delete(index) - delete item at index, shifting all trailing elements left
- remove(item) - looks for value and removes index holding it (even if in multiple places)
- find(item) - looks for value and returns first index with that value, -1 if not found
- resize(new_capacity) // private function
- when you reach capacity, resize to double the size
- when popping an item, if size is 1/4 of capacity, resize to half
- Time
- O(1) to add/remove at end (amortized for allocations for more space), index, or update
- O(n) to insert/remove elsewhere
- Space
- contiguous in memory, so proximity helps performance
- space needed = (array capacity, which is >= n) * size of item, but even if 2n, still O(n)
π€ μ€κ³ κ³ λ €μ¬ν
- μλ£νμ
int
λ‘ νμ νλ€. - 맨 μ²μ μμ±μλ₯Ό ν΅ν΄ μ΄κΈ°ν 벑ν°κ° μμ±λλ€.
New raw data array with allocated memory
- can allocate int array under the hood, just not use its features
- start with 16, or if starting number is greater, use power of 2 - 16, 32, 64, 128
- javaμ
ArrayList
λ₯Ό μ¬μ©νμ§ μκ³ μ μ λ°°μ΄ ννλ‘ λμ κ°λ³ λ°°μ΄μ ꡬννλ€. - κΈ°λ³Έ μμμ κ°μλ 16λΆν° μμνλ€.
- κ·Έ λ°μ λν μΌν ꡬν μ¬νλ€μ λ¬Έμν μ£ΌμμΌλ‘ ν¨μ μ μλ¬Έ μμ κΈ°λ‘νλ€.
- β οΈ μ£Όμμ¬ν
- μ½μ μ, λ°°μ΄ νμ₯ κ³ λ €
- μμ μ, λ°°μ΄ μΆμ κ³ λ €
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
public class Arrays {
private int[] vector;
private int elemCnt = 0;
public Arrays(){
vector = new int[16];
}
public int size(){
return elemCnt;
}
public int capacity(){
return vector.length - size();
}
/**
* @return λΉμ΄μμΌλ©΄ True(1), μμκ° μμΌλ©΄ False(0)
*/
public boolean is_empty(){
return elemCnt == 0;
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public int at(int index){
if (index >= 0 || index < size()){
return vector[index];
} else {
throw new IndexOutOfBoundsException();
}
}
public void push(int item){
if (capacity() == 0){
resize(size() * 2);
}
vector[elemCnt++] = item;
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public void insert(int index, int item){
if (index < size()){
int[] newVector = new int[vector.length];
if (capacity() == 0) {
resize(size() * 2);
}
for (int i = 0; i < index; i++) {
newVector[i] = vector[i];
}
newVector[index] = item;
for (int i = index + 1; i < size() + 1; i++) {
newVector[i] = vector[i - 1];
}
elemCnt++;
vector = newVector;
} else {
throw new IndexOutOfBoundsException();
}
}
public void prepend(int item){
if (capacity() == 0){
resize(size() * 2);
}
int[] newVector = new int[vector.length];
for (int i = 0; i < size(); i++){
newVector[i + 1] = vector[i];
}
newVector[0] = item;
elemCnt++;
vector = newVector;
}
/**
* 벑ν°κ° λΉμμ κ²½μ° 'NullPointerException' μλ¬ λ°μ
*/
public int pop(){
int rst;
if (is_empty()){
throw new NullPointerException();
}
rst = vector[elemCnt - 1];
vector[elemCnt--] = 0;
if (size() / 4 == vector.length){
resize(size() / 4);
}
return rst;
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public void delete(int index){
if (index < size()){
for (int i = index; i < size(); i++){
vector[i] = vector[i + 1];
}
elemCnt--;
if (size() / 4 == vector.length){
resize(size() / 4);
}
} else {
throw new IndexOutOfBoundsException();
}
}
/**
* itemμ΄ μ‘΄μ¬νλ λͺ¨λ μΈλ±μ€ μμΉμ μμ μμ
*/
public void remove(int item){
for (int i = 0; i < size(); i++) {
if (vector[i] == item){
delete(i);
}
}
}
/**
* itemμ΄ λ²‘ν° λ΄μ μμΌλ©΄ -1 리ν΄
*/
public int find(int item){
for (int i = 0; i < size(); i++) {
return i;
}
return -1;
}
private void resize(int new_capacity){
int[] newVector = new int[new_capacity];
for (int i = 0; i < size(); i++) {
newVector[i] = vector[i];
}
vector = newVector;
}
// test
public static void main(String[] args) {
Arrays tmpVector = new Arrays();
System.out.println("test 1:" + tmpVector.size());
System.out.println("test 2:" + tmpVector.capacity());
System.out.println("test 3:" + tmpVector.is_empty());
tmpVector.push(3);
tmpVector.push(4);
System.out.println("test 4:" + tmpVector.at(0)); // 3
System.out.println("test 5:" + tmpVector.at(1)); // 4
tmpVector.insert(0, 2);
tmpVector.prepend(1);
System.out.println("test 6:" + java.util.Arrays.toString(tmpVector.vector)); // [1, 2, 3, 4, ...]
System.out.println("test 7:" + tmpVector.pop());
tmpVector.delete(0);
tmpVector.remove(2);
System.out.println("test 8:" + tmpVector.find(3)); // 0
}
}
πΒ 4. Implementation LinkedList
- Implement (I did with tail pointer & without):
- size() - returns number of data elements in list
- empty() - bool returns true if empty
- value_at(index) - returns the value of the nth item (starting at 0 for first)
- push_front(value) - adds an item to the front of the list
- pop_front() - remove front item and return its value
- push_back(value) - adds an item at the end
- pop_back() - removes end item and returns its value
- front() - get value of front item
- back() - get value of end item
- insert(index, value) - insert value at index, so current item at that index is pointed to by new item at index
- erase(index) - removes node at given index
- value_n_from_end(n) - returns the value of the node at nth position from the end of the list
- reverse() - reverses the list
- remove_value(value) - removes the first item in the list with this value
- Doubly-linked List
- Description (video)
- No need to implement
π€ μ€κ³ κ³ λ €μ¬ν
- μλ£νμ
int
λ‘ νμ νλ€. - 맨 μ²μ μμ±μλ₯Ό ν΅ν΄ μ΄κΈ°ν λ§ν¬λ 리μ€νΈκ° μμ±λλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.*;
public class ImplLinkedList {
private LinkedList<Integer> LinkedList;
public ImplLinkedList() {
LinkedList = new LinkedList<>();
}
public int size() {
return LinkedList.size();
}
public boolean empty() {
return LinkedList.size() == 0;
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public int value_at(int index) {
if (index >= 0 || index < size()){
return LinkedList.get(index);
} else {
throw new IndexOutOfBoundsException();
}
}
public void push_front(int value) {
LinkedList<Integer> newLinkedList = new LinkedList<>();
newLinkedList.push(value);
for (int e : LinkedList)
newLinkedList.push(e);
LinkedList = newLinkedList;
}
/**
* λ§ν¬λ 리μ€νΈκ° λΉμμ κ²½μ° 'NullPointerException' μλ¬ λ°μ
*/
public int pop_front() {
if (empty()){
throw new NullPointerException();
}
return LinkedList.pollFirst();
}
public void push_back(int value){
LinkedList.push(value);
}
/**
* λ§ν¬λ 리μ€νΈκ° λΉμμ κ²½μ° 'NullPointerException' μλ¬ λ°μ
*/
public int pop_back() {
if (empty()){
throw new NullPointerException();
}
return LinkedList.poll();
}
/**
* λ§ν¬λ 리μ€νΈκ° λΉμμ κ²½μ° 'NullPointerException' μλ¬ λ°μ
*/
public int front() {
if (empty()){
throw new NullPointerException();
}
return LinkedList.peekFirst();
}
/**
* λ§ν¬λ 리μ€νΈκ° λΉμμ κ²½μ° 'NullPointerException' μλ¬ λ°μ
*/
public int back() {
if (empty()){
throw new NullPointerException();
}
return LinkedList.peek();
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public void insert(int index, int value) {
if (index >= 0 || index < size()){
LinkedList<> newLinkedList = new LinkedList<Integer>();
for (int i = 0; i < index; i++) {
newLinkedList.push(LinkedList.get(i));
}
newLinkedList.add(index, value);
for (int i = index; i <= size(); i++) {
newLinkedList.push(LinkedList.get(i));
}
LinkedList = newLinkedList;
} else {
throw new IndexOutOfBoundsException();
}
}
/**
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public void erase(int index) {
if (index >= 0 || index < size()){
LinkedList.remove(index);
} else {
throw new IndexOutOfBoundsException();
}
}
/**
* 0λ²μ§Έ μμλΆν° κ°λ₯ -> 0λ²μ§Έ μμ : λ§μ§λ§ μμ
* μ
λ ₯ μΈλ±μ€κ° λ²μ λ°μ΄λ©΄ 'IndexOutOfBoundsException' μλ¬ λ°μ
*/
public int value_v_from_end(int n) {
if (n >= 0 || n < size()){
return LinkedList.get(size() - 1 - n);
} else {
throw new IndexOutOfBoundsException();
}
}
public LinkedList<Integer> reverse() {
LinkedList<Integer> newLinkedList = new LinkedList<>();
for (int e : LinkedList)
newLinkedList.add(0, e);
return newLinkedList;
}
/**
* κ°μ΄ μμΌλ©΄ μ무κ²λ μννμ§ μμ
* λ€μ€ κ° μ‘΄μ¬ μ, 맨 μ μμ μ κ±°
*/
public void remove_value(int value) {
for (int e : LinkedList){
if (e == value)
LinkedList.remove(e);
}
}
}
This post is licensed under CC BY 4.0 by the author.