‡๐Ÿ‘ฉ‍๐Ÿ’ป ‡/º Java

[์ž๋ฃŒ๊ตฌ์กฐ] ๋ฆฌ์ŠคํŠธ (List) ๊ตฌํ˜„ (Java)

Trudy | ์†ก์—ฐ 2023. 12. 5. 11:36

 

LinkedList - Node๋“ค์˜ ๊ตฌ์กฐ

๐Ÿ“๋ฆฌ์ŠคํŠธ(List) ๋ž€?

์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•œ ๊ฒƒ
๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ ๋…ธ๋“œ๋ผ๊ณ  ํ•œ๋‹ค.
๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ์™€ ๋‹ค์Œ ๋…ธ๋“œ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•œ๋‹ค.


๐Ÿ“๋‹จ์ผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ(List) ์˜ ๊ตฌํ˜„

๐Ÿพ  ํด๋ž˜์Šค ๊ตฌ์กฐ  

 

1.  ๋…ธ๋“œ ํด๋ž˜์Šค
๋ฐ์ดํ„ฐ์™€ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค

package list;

public class Node {
    Integer data;
    Node next;

    public Node(Integer data, Node next) {
        this.data = data;
        this.next = next;
    }
}

 



  2. ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค
head  : ๋งจ ์ฒ˜์Œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•œ๋‹ค. 

๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

๐Ÿพ์ƒ์„ฑ์ž

head์— null์„ ์ €์žฅํ•œ๋‹ค. 

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

 

๐Ÿพ insertFirst(x); ๋งจ ์ฒ˜์Œ์— ๋ฐ์ดํ„ฐ x๋ฅผ ์ถ”๊ฐ€

insertFirst()

๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค. 

๋…ธ๋“œ ๊ฐ์ฒด ์ƒ์„ฑ(๋…ธ๋“œ์˜ next์—๋Š” ๊ธฐ์กด head๋ฅผ ์ €์žฅ)

head์— ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅ

    void insertFirst(int x){
        Node node = new Node(x, head);
        head = node;
    }

 

๐Ÿพ insertLast(x); ๋งจ ๋งˆ์ง€๋ง‰์— ๋ฐ์ดํ„ฐ x๋ฅผ ์ถ”๊ฐ€

insertLast()

๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค. 

head๊ฐ€ null์ด๋ฉด insertFirst๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. 

๋…ธ๋“œ ๊ฐ์ฒด ์ƒ์„ฑ(๋…ธ๋“œ์˜ next์—๋Š” null ์ €์žฅ)

๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜€๋˜ current์˜ next์—๋Š” ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅ

    void insertLast(int x){
        if(head == null) this.insertFirst(x);
        else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            Node node = new Node(x, null);
            current.next = node;
        }
    }

 

๐Ÿพ inserIndex(index, x); ์›ํ•˜๋Š” index์— ๋ฐ์ดํ„ฐ x๋ฅผ ์ถ”๊ฐ€

insertIndex(2,data)

๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ธ๋ฑ์Šค์™€ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค. 

๋…ธ๋“œ ๊ฐ์ฒด ์ƒ์„ฑ(๋…ธ๋“œ์˜ next์—๋Š” ์›ํ•˜๋˜ index์˜ ๋…ธ๋“œ๋ฅผ ์ €์žฅ)

์›ํ•˜๋˜ index์˜ ์ „ ๋…ธ๋“œ์ธ current์˜ next์—๋Š” ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ๋…ธ๋“œ๋ฅผ ์ €์žฅ

    void insertIndex(int index, int x){
        Node current = head;
        //head๊ฐ€ ์ธ๋ฑ์Šค 0
        for (int i = 1; i < index; i++) {
            current = current.next;
        }
        Node node = new Node(x, current.next);
        current.next = node;
    }

 

๐Ÿพ display(); ํ˜„์žฌ ๋…ธ๋“œ๋“ค์„ ๋ชจ๋‘ ์ถœ๋ ฅ

    void display(){
        Node current = head;
        while(true){
            System.out.printf("[%d] ",current.data);
            if(current.next == null) break;
            current = current.next;
        }
        System.out.println();
    }

 

๋ฆฌ์ŠคํŠธ ์‚ญ์ œ ๋ฉ”์†Œ๋“œ

 

๐Ÿพ removeFirst(); ๋งจ ์ฒ˜์Œ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ

 

๐ŸพremoveLast(); ๋งจ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ

 

๐Ÿพ removeIndex(index); ์›ํ•˜๋Š” ์ธ๋ฑ์Šค์˜ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ


๐Ÿ“์ „์ฒด ์ฝ”๋“œ

LinkedList.java

package list;

public class LinkedList {
    Node head;

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

    void insertFirst(int x){
        Node node = new Node(x, head);
        head = node;
    }

    void insertLast(int x){
        if(head == null) this.insertFirst(x);
        else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            Node node = new Node(x, null);
            current.next = node;
        }
    }

    void insertIndex(int index, int x){
        Node current = head;
        //head๊ฐ€ ์ธ๋ฑ์Šค 0
        for (int i = 1; i < index; i++) {
            current = current.next;
        }
        Node node = new Node(x, current.next);
        current.next = node;
    }

    void display(){
        Node current = head;
        while(true){
            System.out.printf("[%d] ",current.data);
            if(current.next == null) break;
            current = current.next;
        }
        System.out.println();
    }
}

 

LinkedListMain.java

package list;

public class LinkedListMain {
    public static void main(String[] args) {
        LinkedList ll = new LinkedList();
        ll.insertFirst(10);
        ll.insertFirst(20);
        ll.insertFirst(30);
        ll.display();

        ll.insertLast(40);
        ll.insertLast(50);
        ll.display();

        ll.insertIndex(3, 60);
        ll.display();
    }
}

 

 

์‹คํ–‰ ๊ฒฐ๊ณผ