๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์์ ํ๋ก๊ทธ๋จ์ ์ฑ๋ฅ์ ์ง์ ์ ์ธ ์ํฅ์ ๋ฏธ์น๋ค.
๋ฐฐ์ด(Array)๊ณผ ๋์ ๋ฐฐ์ด(Dynamic Array) ๊ทธ๋ฆฌ๊ณ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Linked List)๋ ๋ํ์ ์ธ ์ ํ ์๋ฃ๊ตฌ์กฐ์ด์ง๋ง, ๊ตฌ์กฐ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์กฐํ / ์ฝ์
/ ์ญ์ ์ ๊ฐ์ ์ฐ์ฐ ์ฑ๋ฅ์์ ์ฐจ์ด๊ฐ ๋ฐ์ํ๋ค.
๊ฐ ์๋ฃ๊ตฌ์กฐ์ ํน์ง๊ณผ ์ฅ๋จ์ ์ ์ดํดํ๋ฉด ์ํฉ์ ๋ง๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ ํํด ํ๋ก๊ทธ๋จ ์ฑ๋ฅ์ ํฅ์์ํฌ ์ ์๋ค.
์ ํ ์๋ฃ๊ตฌ์กฐ(Linear Data Structure)๋ ๋ฐ์ดํฐ๊ฐ ๋ ผ๋ฆฌ์ ์ธ ์์๋ฅผ ๊ฐ์ง๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ปํ๋ค. ๊ฐ ๋ฐ์ดํฐ๋ ์๋ค ์์๋ฅผ ๊ฐ์ง๋ฉฐ, ์ฒ์๋ถํฐ ๋๊น์ง ์์ฐจ์ ์ผ๋ก ํ์ํ ์ ์๋ค.
๋ฐฐ์ด์ ๋์ผํ ํ์
์ ๋ฐ์ดํฐ๋ฅผ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฅํ๋ค. ๊ฐ ์์๋ ์ธ๋ฑ์ค(Index)๋ฅผ ํตํด ๊ตฌ๋ถ๋๋ฉฐ, ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๋ค.
๋ฐ์ดํฐ์ ํ์
์ด ๊ฐ๊ธฐ ๋๋ฌธ์ ๋ชจ๋ ์์์ ํฌ๊ธฐ(Byte)๊ฐ ๊ฐ๋ค. ๋ฐ๋ผ์ ์์ ์ฃผ์ + (์์์ ํฌ๊ธฐ x ์ธ๋ฑ์ค)์ ๊ณ์ฐํ๋ฉด ํน์ ์์์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๋ฐ๋ก ๊ณ์ฐํ ์ ์๋ค. ์ด์ฒ๋ผ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํด ์ํ๋ ์์์ ๋ฐ๋ก ์ ๊ทผํ๋ ๋ฐฉ์์ ์์ ์ ๊ทผ(Random Access)์ด๋ผ๊ณ ํ๋ค.
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ์์ ์ ๊ทผ
System.out.println(numbers[2]); // 30 ์ถ๋ ฅ
}
}โ
๋ฐฐ์ด์ ๋ฐ์ดํฐ๋ฅผ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ ์์ฑ ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๊ณ ํด๋น ํฌ๊ธฐ๋งํผ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋ฏธ๋ฆฌ ํ๋ณดํด์ผ ํ๋ค. ๋ฐ๋ผ์ ์์ฑ ์ดํ์๋ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ ์๋ ์๋ค.
public class ArraySizeExample {
public static void main(String[] args) {
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
// ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
// The final field array.length cannot be assigned
arr.length = 5;
}
}
ํฌ๊ธฐ๋ฅผ ๋๋ ค์ผ ํ๋ ๊ฒฝ์ฐ์๋ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๊ณ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํด์ผ ํ๋ค.
public class ArrayCopyExample {
public static void main(String[] args) {
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
// ์ํ๋ ํฌ๊ธฐ์ ๋ฐฐ์ด ์์ฑ
int[] newArr = new int[5];
// ๊ธฐ์กด ๋ฐ์ดํฐ ๋ณต์ฌ
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
arr = newArr;
}
}โ
๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ๊ฐ์์ ๊ด๊ณ์์ด ์ํ๋ ์์น์ ์์๋ฅผ ๋ฐ๋ก ์กฐํํ ์ ์๋ค. ๋ฐ๋ผ์ ์กฐํ ์ฐ์ฐ์ O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
public class ArrayAccessExample {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
// ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํด ์ํ๋ ์์์ ์ ๊ทผ
int value = arr[2];
System.out.println(value); // 30 ์ถ๋ ฅ
}
}
๋ฐ๋ฉด ๋ฐฐ์ด์ ์ค๊ฐ์ ์๋ก์ด ์์๋ฅผ ์ฝ์ ํ๊ฑฐ๋ ์ญ์ ํ๋ ค๋ฉด ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๊ธฐ ์ํด ๊ธฐ์กด ์์๋ค์ ์ด๋ํด์ผ ํ๋ค.
import java.util.Arrays;โ
public class ArrayInsertDeleteExample {
public static void main(String[] args) {
int[] arr = new int[6];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// ์ฝ์
int insertIndex = 2;
int insertValue = 25;
for (int i = 4; i >= insertIndex; i--) {
arr[i + 1] = arr[i];
}
arr[insertIndex] = insertValue;
// [10, 20, 25, 30, 40, 50]
System.out.println(Arrays.toString(arr));
// ์ญ์
int deleteIndex = 2;
for (int i = deleteIndex; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
// [10, 20, 30, 40, 50, 50]
// ์ญ์ ํ๋๋ผ๋ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ์ ์ง๋๋ฏ๋ก ๋ง์ง๋ง ์์๋ ๋จ์์๋ค.
System.out.println(Arrays.toString(arr));
}
}
๋ฐฐ์ด ์ค๊ฐ ์์น์ ์ฝ์
๊ณผ ์ญ์ ๋ ์ด๋ํด์ผ ํ๋ ์์๊ฐ ๋ง์์๋ก ์ฑ๋ฅ์ด ๋จ์ด์ง๋ฉฐ, ์ต์
์ ๊ฒฝ์ฐ O(n)์ ์๊ฐ์ด ํ์ํ๋ค.
๋ฐ๋ฉด ๋ฐฐ์ด์ ๋ ์์น์์์ ์ฝ์
๊ณผ ์ญ์ ๋ ์์ ์ด๋์ด ํ์ํ์ง ์๊ธฐ ๋๋ฌธ์ O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
๋์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ํ์ง ์์๋ ๋๋ ๋ฐฐ์ด์ด๋ค. ๋ด๋ถ์ ์ผ๋ก๋ ์ผ๋ฐ ๋ฐฐ์ด๊ณผ ๋น์ทํ์ง๋ง, ๊ณต๊ฐ์ด ๋ถ์กฑํด์ง๋ฉด ๋ ํฐ ๋ฐฐ์ด์ ์๋ก ๋ง๋ค์ด ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ๋ ๊ณผ์ ์ ์์์ ์ฒ๋ฆฌํ๋ค.
์ฆ, ์์์ ์ง์ ๊ตฌํํ๋ ๋ฐฐ์ด ๋ณต์ฌ ๊ณผ์ ์ ๋ด๋ถ์ ๊ฐ์ถ ๊ฒ์ด ๋์ ๋ฐฐ์ด์ด๋ค. ๋ํ์ ์ผ๋ก Java์ ArrayList, C++์ vector, Python์ list ๋ฑ์ด ์๋ค.
import java.util.ArrayList;
import java.util.List;
public class DynamicArrayExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
// ํฌ๊ธฐ๋ฅผ ์ง์ ํ์ง ์์๋ ๊ณ์ ์ฝ์ ํ ์ ์๋ค.
numbers.add(10);
numbers.add(20);
numbers.add(30);
// ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ์ ๊ทผ
System.out.println(numbers.get(2)); // 30 ์ถ๋ ฅ
}
}
๋์ ๋ฐฐ์ด์ ๋ ํฌ๊ธฐ๋ฅผ ๊ตฌ๋ถํด์ ๊ด๋ฆฌํ๋ค. ์ค์ ๋ก ํ๋ณดํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์๋ฏธํ๋ ์ฉ๋(Capacity)๊ณผ ํ์ฌ ๋ฐ์ดํฐ์ ๊ฐ์์ธ ํฌ๊ธฐ(Size)๋ค.
๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ๋ค๊ฐ ํฌ๊ธฐ๊ฐ ์ฉ๋์ ๋ค๋ค๋ฅด๋ฉด, ๋ ํฐ ๋ฐฐ์ด์ ์๋ก ๋ง๋ค๊ณ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ๋ค. ์ด ๊ณผ์ ์ ๋ฆฌ์ฌ์ด์ง(Resizing)์ด๋ผ๊ณ ํ๋ค.
import java.util.Arrays;
public class SimpleDynamicArray {
private int[] elements;
private int size;
public SimpleDynamicArray() {
this.elements = new int[4]; // ์์์ ์ด๊ธฐ ์ฉ๋
this.size = 0;
}
public void add(int value) {
// ์ฉ๋์ด ๊ฐ๋ ์ฐจ๋ฉด ํ์ฅ
if (size == elements.length) {
grow();
}
elements[size] = value;
size++;
}
private void grow() {
// ๊ธฐ์กด ์ฉ๋์ 2๋ฐฐ
int newCapacity = elemtns.length * 2;
// ๋ ํฐ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌ
elements = Arrays.copyOf(elements, newCapacity);
}
}โ
์ผ๋ฐ์ ์ผ๋ก ์ฉ๋์ ๋๋ฆด ๋ ๋ฐฐ์๋ก ๋๋ฆฌ๋๋ฐ, ์ด์ ๋ ๋ฆฌ์ฌ์ด์ง ํ์๋ฅผ ์ค์ด๊ธฐ ์ํด์๋ค. ๋ง์ฝ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ ๋๋ง๋ค ํฌ๊ธฐ๋ฅผ ๋๋ฆฐ๋ค๋ฉด ์ ์ฒด ๋ณต์ฌ๊ฐ ๋ฐ์ํด n ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋ ๋ฐ O(nยฒ)์ ์๊ฐ์ด ํ์ํ๋ค.
๋ฐ๋ฉด ๋ฐฐ์๋ก ๋๋ฆฌ๋ ๊ฒฝ์ฐ ๋ณต์ฌ ๋น์ฉ์ ์ ์ฒด ์ฝ์
ํ์์ ๋๋์ด ๊ณ์ฐํ์ ๋ ์ฝ์
ํ ๋ฒ๋น ํ๊ท O(1)์ด ๋๋ค.
๋์ ๋ฐฐ์ด์ ๋ด๋ถ๋ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ ์กฐํ๋ ๋ฐฐ์ด๊ณผ ๋์ผํ๊ฒ O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
import java.util.ArrayList;
import java.util.List;
public class DynamicArrayAccessExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30));
// ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ ์์ ์ ๊ทผ
int value = numbers.get(2);
System.out.println(value); // 30 ์ถ๋ ฅ
}
}
์ฝ์
์ญ์ ๋ฐฐ์ด๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ ์์น์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ ๋๋ ์์ ์ด๋์ด ์์ผ๋ฏ๋ก O(1)์ด์ง๋ง, ์ฉ๋์ด ๊ฐ๋ ์ฐจ ๋ฆฌ์ฌ์ด์ง์ด ๋ฐ์ํ๋ ์๊ฐ ์ ์ฒด ๋ณต์ฌ๊ฐ ์ผ์ด๋ O(n)์ด ๋๋ค.
์ค๊ฐ ์์น์ ์ฝ์
๊ณผ ์ญ์ ๋ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๊ธฐ ์ํด ๋ค์ชฝ ์์๋ค์ ๋ชจ๋ ์ด๋ํด์ผ ํ๋ฏ๋ก O(n)์ ์๊ฐ์ด ํ์ํ๋ค.
import java.util.ArrayList;
import java.util.List;
public class DynamicArrayInsertDeleteExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30, 40, 50));
// ๋ ์์น ์ฝ์
numbers.add(60);
// [10, 20, 30, 40, 50, 60]
System.out.println(numbers);
// ์ค๊ฐ ์์น ์ฝ์
numbers.add(2, 25);
// [10, 20, 25, 30, 40, 50, 60]
System.out.println(numbers);
// ์ค๊ฐ ์์น ์ญ์
numbers.remove(2);
// [10, 20, 30, 40, 50, 60]
// ๋ฐฐ์ด๊ณผ ๋ฌ๋ฆฌ ์ญ์ ํ๋ฉด ํฌ๊ธฐ(size)๋ ํจ๊ป ์ค์ด๋ ๋ค.
System.out.println(numbers);
}
}
๋ฐฐ์ด์์๋ ์์๋ฅผ ์ญ์ ํด๋ ํฌ๊ธฐ๊ฐ ์ ์ง๋์ด ๋ง์ง๋ง ๊ฐ์ด ๋จ์ง๋ง, ๋์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ํจ๊ป ๊ด๋ฆฌํ๋ฏ๋ก ์ญ์ ํ ๋งํผ ํฌ๊ธฐ๊ฐ ์ค์ด๋ ๋ค. ๋ค๋ง, ์ด๋ ์ค์ด๋๋ ๊ฒ์ ํฌ๊ธฐ์ผ ๋ฟ ์ฉ๋์ ๊ทธ๋๋ก์ด๋ฏ๋ก, ํ ๋ฒ ๋์ด๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ณ์ ์ ์ ํ๋ค.
์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ ๋ฐ์ดํฐ๋ฅผ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฅํ์ง ์๋๋ค. ๋์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ๋
ธ๋(Node)๋ฅผ ๋ง๋ค๊ณ , ๊ฐ ๋
ธ๋๊ฐ ๋ค์ ๋
ธ๋์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๋๋ก ์ฐ๊ฒฐํ๋ค.
๋
ธ๋๋ ์ ์ฅํ ๊ฐ๊ณผ ๋ค์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ก ๊ตฌ์ฑ๋๋ค. ๋ฐ๋ผ์ ๋ฆฌ์คํธ ์ ์ฒด๋ฅผ ๋ค๋ฃจ๋ ค๋ฉด ์ฒซ ๋ฒ์งธ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ(head)๋ถํฐ ์์ํด ๋ค์ ๋
ธ๋๋ฅผ ์์ฐจ์ ์ผ๋ก ์กฐํํด์ผ ํ๋ค.
๋ง์ง๋ง ๋
ธ๋๋ ๋ค์์ ์ด์ด์ง ๋
ธ๋๊ฐ ์์ผ๋ฏ๋ก ์ฐธ์กฐ ๊ฐ์ผ๋ก null์ด ๋ค์ด๊ฐ๋ค.
public class LinkedListExample {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// head๋ถํฐ next๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ์กฐํ
for (Node node = head; node != null; node = node.next) {
System.out.println(node.data); // 10, 20, 30 ์ถ๋ ฅ
}
}
}
์์๊ฐ ํฉ์ด์ ธ ์๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ด๋ ๋์ ๋ฐฐ์ด์ฒ๋ผ ์ธ๋ฑ์ค๋ก ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ณ์ฐํ ์ ์๋ค.
์ธ ๋ฒ์งธ ๊ฐ์ ์ฝ์ผ๋ ค๋ฉด head์์๋ถํฐ ์์ํด ์์๋๋ก ์ฐธ์กฐ๋ฅผ ๋ฐ๋ผ๊ฐ์ผ ํ๋ค. ์ด์ ๊ฐ์ ๋ฐฉ์์ ์์ฐจ ์ ๊ทผ(Sequential Access)์ด๋ผ๊ณ ํ๋ค.
๋ฐฐ์ด์ ์์ฑ ์์ ์ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ๋ณดํ์ง๋ง, ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ ๋
ธ๋๋ฅผ ํ๋ ๋ง๋ค ๋๋ง๋ค ๊ทธ ๋
ธ๋์ ํ์ํ ๊ณต๊ฐ๋ง ํ๋ณดํ๋ค.
๋ฐ๋ผ์ ํฌ๊ธฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ํ ํ์๊ฐ ์๊ณ , ๋์ ๋ฐฐ์ด์ฒ๋ผ ์ฉ๋์ด ๊ฐ๋ ์ฐจ ์ ์ฒด๋ฅผ ๋ณต์ฌํ๋ ๋ฆฌ์ฌ์ด์ง๋ ๋ฐ์ํ์ง ์๋๋ค.
๋์ ๋
ธ๋๋ง๋ค ๋ค์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ฅผ ํจ๊ป ์ ์ฅํ๋ฏ๋ก, ๊ฐ์ ๊ฐ์์ ๊ฐ์ ๋ด์๋ ๋ฐฐ์ด๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ์ฌ์ฉํ๊ฒ ๋๋ค.
์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ์กฐํ๋ head๋ถํฐ ์ฐธ์กฐ๋ฅผ ๋ฐ๋ผ๊ฐ์ผ ํ๋ฏ๋ก O(n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
public class LinkedListAccessExample {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// ์ธ ๋ฒ์งธ ๊ฐ์ ์ฝ์ผ๋ ค๋ฉด head๋ถํฐ ๋ ๋ฒ ์ด๋ํด์ผ ํ๋ค.
Node current = head;
for (int i = 0; i < 2; i++) {
current = current.next;
}
System.out.println(current.data); // 30 ์ถ๋ ฅ
}
}
๋ฐ๋ฉด ์ฝ์ ๊ณผ ์ญ์ ๋ ์ฐธ์กฐ๋ง ๋ฐ๊พธ๋ฉด ๋๋ฏ๋ก ์์๋ฅผ ์ด๋ํ ํ์๊ฐ ์๋ค. ์ฝ์ ํ ์์น์ ์ด์ ๋ ธ๋๋ฅผ ์๊ณ ์๋ค๋ฉด O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
public class LinkedListInsertDeleteExample {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// ์ฝ์
Node prev = head.next;
Node newNode = new Node(25);
newNode.next = prev.next;
prev.next = newNode;
// 10, 20, 25, 30
print(head);
// ์ญ์
prev.next = prev.next.next;
// 10, 20, 30
print(head);
}
private static void print(Node head) {
for (Node node = head; node != null; node = node.next) {
System.out.print(node.data + " ");
}
System.out.println();
}
}
์์ ๊ฐ์ด ๊ฐ ๋
ธ๋๊ฐ ๋ค์ ๋
ธ๋๋ง์ ๊ฐ๋ฆฌํค๋ ํํ๋ฅผ ๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Singly Linked List)๋ผ๊ณ ํ๋ค.
์ด์ธ์๋ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Doubly Linked List)๋ ๊ฐ ๋
ธ๋๊ฐ ์ด์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ฅผ ํ๋ ๋ ๊ฐ์ง๋ค. ์๋ฐฉํฅ์ผ๋ก ์ํ๊ฐ ๊ฐ๋ฅํ๋ฉฐ, ์ญ์ ํ ๋
ธ๋ ํ๋๋ง ์๋ฉด ์๋ค๋ก ๋ฐ๋ก ์ด์ด๋ถ์ผ ์ ์๋ค. Java์ LinkedList๊ฐ ์ด ๋ฐฉ์์ด๋ค.
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Circular Linked List)๋ ๋ง์ง๋ง ๋
ธ๋๊ฐ null ๋์ head๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ๋์ ๋๋ฌํด๋ ๋ค์ ์ฒ์์ผ๋ก ๋์์ค๋ฏ๋ก ์๋ฒ์ ๋์๊ฐ๋ฉฐ ์ฒ๋ฆฌํ๋ ์์
์ ์ ํฉํ๋ค.
import java.util.LinkedList;
import java.util.List;
public class JavaLinkedListExample {
public static void main(String[] args) {
LinkedList<Integer>โ numbers = new LinkedList<>(List.of(10, 20, 30));
// ๋งจ ์ ์ฝ์ ๋ ์์ ์ด๋ ์์ด ์ฒ๋ฆฌ๋๋ค.
numbers.addFirst(5);
// [5, 10, 20, 30]
System.out.println(numbers);
}
}
