Home > Algorithm > 2024 > πŸ“¦[DS,Algorithm] λ…Έλ“œ(Node)

πŸ“¦[DS,Algorithm] λ…Έλ“œ(Node)
DataStructure Algorithm

1️⃣ Node(λ…Έλ“œ).

Node(λ…Έλ“œ) λŠ” μ—°κ²° 리슀트, 트리, κ·Έλž˜ν”„ λ“±μ˜ μžλ£Œκ΅¬μ‘°μ—μ„œ 기본적인 λ‹¨μœ„ μš”μ†Œλ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

λ…Έλ“œλŠ” 데이터λ₯Ό μ €μž₯ν•˜λ©°, λ‹€λ₯Έ λ…Έλ“œμ™€μ˜ 연결을 λ‚˜νƒ€λ‚΄λŠ” 포인터(μ°Έμ‘°)λ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€.

1️⃣ Node의 ꡬ성 μš”μ†Œ.

  1. 데이터(Data) : λ…Έλ“œκ°€ μ €μž₯ν•˜λŠ” μ‹€μ œ κ°’μž…λ‹ˆλ‹€. μ΄λŠ” 숫자, 문자, 객체 λ“± λͺ¨λ“  데이터 νƒ€μž…μ΄ 될 수 μžˆμŠ΅λ‹ˆλ‹€.

  2. 포인터(μ°Έμ‘°, Pointer) : λ‹€μŒ λ…Έλ“œ(λ˜λŠ” λ‹€λ₯Έ κ΄€λ ¨λœ λ…Έλ“œ)λ₯Ό κ°€λ¦¬μ§€λŠ” μ°Έμ‘°μž…λ‹ˆλ‹€. ν¬μΈν„°μ˜ μˆ˜μ™€ μ’…λ₯˜λŠ” μžλ£Œκ΅¬μ‘°μ— 따라 λ‹€λ¦…λ‹ˆλ‹€.

    • Singly Linked List : ν•˜λ‚˜μ˜ λ‹€μŒ λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터λ₯Ό κ°€μ§‘λ‹ˆλ‹€.
    • Doubly Linked List : 이전 λ…Έλ“œμ™€ λ‹€μŒ λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 두 개의 포인터λ₯Ό κ°€μ§‘λ‹ˆλ‹€.
    • 트리(Tree) : λΆ€λͺ¨ λ…Έλ“œ, μžμ‹ λ…Έλ“œ λ“± μ—¬λŸ¬ λ°©ν–₯의 포인터λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
    • κ·Έλž˜ν”„(Graph) : μ—¬λŸ¬ 개의 인접 λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

Singly Linked List의 Node
Singly Linked Listμ—μ„œμ˜ λ…Έλ“œλŠ” λ‹€μŒκ³Ό 같은 ꡬ쑰λ₯Ό κ°€μ§‘λ‹ˆλ‹€.

public class Node {
  int data; // λ…Έλ“œκ°€ μ €μž₯ν•˜λŠ” 데이터
  Node next; // λ‹€μŒ λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터

  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

Doubly Linked List의 Node
Doubly Linked Listμ—μ„œμ˜ λ…Έλ“œλŠ” λ‹€μŒκ³Ό 같은 ꡬ쑰λ₯Ό κ°€μ§‘λ‹ˆλ‹€.

public class DoublyNode {
  int data; // λ…Έλ“œκ°€ μ €μž₯ν•˜λŠ” 데이터
  DoublyNode next; // λ‹€μŒ λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터
  DoublyNode prev; // 이전 λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” 포인터

  public DoublyNode(int data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

2️⃣ Nodeλ₯Ό μ‚¬μš©ν•˜λŠ” 예제.

Singly Linked List

Singly Linked ListλŠ” 각 λ…Έλ“œκ°€ λ‹€μŒ λ…Έλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” ꡬ쑰둜 μ—°κ²°λœ 리슀트 μž…λ‹ˆλ‹€.

μ•„λž˜λŠ” Singly Linked Listλ₯Ό κ΅¬ν˜„ν•œ μ˜ˆμ œμž…λ‹ˆλ‹€.

//SinglyLinkedList
public class SinglyLinkedList {
  private Node head;

  public SinglyLinkedList() {
    this.head = null;
  }

  // 리슀트의 맨 μ•žμ— λ…Έλ“œ μΆ”κ°€
  public void addFirst(int data) {
    Node newNode = new Node(data);
    newNode.next = head;
    head = newNode;
  }

  // 리슀트의 맨 끝에 λ…Έλ“œ μΆ”κ°€
  public void addLast(int data) {
    Node newNode = new Node(data);
    if (head == null) {
      head = newNode;
      return;
    }
    Node current = head;
    while (current.next != null) {
      current = current.next;
    }
    current.next = newNode;
  }

  // 리슀트의 νŠΉμ • κ°’ μ‚­μ œ
  public void delete(int data) {
    if (head == null) {
      return;
    }

    if (head.data == data) {
      head = head.next;
      return;
    }

    Node current = head;

    while (current.next != null && current.next.data != data) {
      current = current.next;
    }

    if (current.next != null) {
      current.next = current.next.next;
    }
  }

  // 리슀트 좜λ ₯
  public void printList() {
    Node current = head;
    while (current != null) {
      System.out.print(current.data + " ");
      current = current.next;
    }
    System.out.println();
  }
}

// Main
public class Main {

  public static void main(String[] args) {
    // Singly Linked List 생성
    SinglyLinkedList singlyLinkedList = new SinglyLinkedList();

    singlyLinkedList.addFirst(1);
    singlyLinkedList.addLast(2);
    singlyLinkedList.addLast(3);
    singlyLinkedList.printList(); // 1 2 3

    singlyLinkedList.delete(2);
    singlyLinkedList.printList(); // 1 3
  }
}