๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์ž๋ฃŒ๊ตฌ์กฐ-PS] Java ์—์„œ ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ :: (Simple Linked List sample code)

by Wonit 2020. 6. 12.

๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๋…ธ๋“œ๊ฐ€ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•˜๋‚˜์˜ ์ฐธ์กฐ๋งŒ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ, ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ํŠน์ง• ๋•๋ถ„์— ๊ตฌํ˜„ํ•˜๊ธฐ ๊ฐ„๋‹จํ•˜๋‹ค.

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋…ธ๋“œ ์ƒ์„ฑ

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•ด๋ณด์ž.

์‚ฝ์ž… ์—ฐ์‚ฐ

์‚ฝ์ž… ์—ฐ์‚ฐ์€ ์ด ์„ธ ๊ฐ€์ง€๋กœ ํฌ๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. Head์— ์‚ฝ์ž…ํ•˜๊ธฐ.
  2. Tail์— ์‚ฝ์ž…ํ•˜๊ธฐ.
  3. ํŠน์ • ์œ„์น˜์— ์‚ฝ์ž…ํ•˜๊ธฐ.

1. Head์— ์‚ฝ์ž…ํ•˜๊ธฐ

Head์— ์‚ฝ์ž…ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

  1. ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ƒ์„ฑ
  2. ์ƒˆ๋กœ์šด ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ(link ํ•„๋“œ)๋ฅผ ๊ธฐ์กด์— ์žˆ๋˜ head๋…ธ๋“œ๋กœ ์„ค์ •
  3. ํ—ค๋“œ ๋…ธ๋“œ์™€ ์ƒˆ๋กœ์šด ๋…ธ๋“œ์˜ ๊ตํ™˜.
  4. ํ…Œ์ผ ๋…ธ๋“œ๋ฅผ ๊ธฐ์กด์˜ ํ—ค๋“œ๋…ธ๋“œ๋กœ ์„ค์ •.

2. Tail์— ์‚ฝ์ž…ํ•˜๊ธฐ

3. ํŠน์ • ์œ„์น˜์— ์‚ฝ์ž…ํ•˜๊ธฐ

ํŠน์ • ์œ„์น˜์— ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ํŠน์ • ์œ„์น˜๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ํŠน์ • ์œ„์น˜๋ฅผ ์ฐพ๋Š” ์ฝ”๋“œ๋ฅผ ๋จผ์ € ๊ตฌํ˜„ํ•ด๋ณด์ž.
ํŠน์ • ์œ„์น˜๋ฅผ ๋ฐฉ๋ฒ•

ํŠน์ • ์œ„์น˜์— ๋…ธ๋“œ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ head ๋…ธ๋“œ์—์„œ ๋ถ€ํ„ฐ ํŠน์ • index๋งŒํผ ์ด๋™ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๊ธธ ์›ํ•˜๋Š” index ๋ฒˆ์งธ ๋…ธ๋“œ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

ํŠน์ • ์œ„์น˜์— ์‚ฝ์ž…

ํŠน์ • ์œ„์ง€(index)๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ณ  ํŠน์ • ์œ„์น˜์— ๋„๋‹ฌํ•˜๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ Front Node์™€ Back Node ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์„ ๋Š์–ด์ฃผ๊ณ  ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋กœ ๊ฐ๊ฐ ์—ฐ๊ฒฐ ์‹œ์ผœ ์ค€๋‹ค๋ฉด ๋œ๋‹ค.

์‚ญ์ œ ์—ฐ์‚ฐ

์‚ญ์ œ ์—ฐ์‚ฐ์—๋Š” ์‚ฝ์ž… ์—ฐ์‚ฐ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

  1. Head ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ
  2. ํŠน์ • ์œ„์น˜ ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ

1. Head ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ

2. ํŠน์ • ์œ„์น˜ ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ

์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ

public class Main {
    public static void main(String[] args) {

    }
}

class SimpleLinkedList {

    private Node headNode; // head node
    private Node tailNode; // tail node
    private int size; // list's size

    public SimpleLinkedList(){ // initialize list
        headNode = new Node(null);
        size = 0;
    }

    private class Node { // Node class

        private Object data; // Data Field
        private Node nextNode; // Link Field

        Node (Object data){
            this.data = data;
            nextNode = null;
        }
    }

    /*Head ์— ์‚ฝ์ž… ํ•˜๊ธฐ.*/
    void addHead(Object data){

        Node newNode = new Node(data); // Make new Node
        newNode.nextNode = headNode; // set newNode's Link to head
        headNode = newNode; // swap headNode to newNode;
        size++;

        if(headNode.nextNode == null){ // check : Is headNode first
            tailNode =headNode;
        }
    }

    void addTail(Object data){
        if(size == 0){ // if list is empty
            addHead(data);
        }else {
            Node newNode = new Node(data); // Make new Node
            tailNode.nextNode = newNode; // update tail's next
            tailNode = newNode; // swap tailNode to newNode;
            size++;
        }
    }

    /*ํŠน์ • ์œ„์น˜์˜ ๋…ธ๋“œ ์ฐพ๊ธฐ*/
    Node node(int index){
        Node x = headNode;
        for (int i = 0; i < index; i++) { // index ๊นŒ์ง€ ๋ฐ˜๋ณต
            x = x.nextNode;               // ํ˜„์žฌ ๋…ธ๋“œ์— ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ• ๋‹น.
        }
        return x; // ํŠน์ • index ์˜ ๋…ธ๋“œ ๋ฐ˜ํ™˜
    }

    void add(Object data, int index){
        if(index == 0) addHead(data);
        else {
            Node temp1 = node(index - 1);
            Node temp2 = temp1.nextNode;

            Node newNode = new Node(data);
            temp1.nextNode = newNode;
            newNode.nextNode = temp2;
            size++;

            if(newNode.nextNode == null){
                tailNode = newNode;
            }
        }
    }

    /*Head ๋…ธ๋“œ ์‚ญ์ œ*/
    Object removeHead(){
        Node temp = headNode; // head ์— ์žˆ๋Š” data ์ž„์‹œ ์ €์žฅ
        headNode = temp.nextNode; // headNode ๋ฅผ ๊ธฐ์กด์˜ ๋‘ ๋ฒˆ์จฐ ๋…ธ๋“œ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
        /*๋…ธ๋“œ ๋ฐ˜ํ™˜ ๊ณผ์ •*/
        Object returnData = temp.data;
        temp = null;
        size--;
        return returnData;
    }

    /*Tail ํŠน์ • ๋…ธ๋“œ ์‚ญ์ œ*/
    Object removeNode(int index){
        if(index == 0) {
            removeHead();
        }

        Node temp = node(index -  1); // ํŠน์ • ์œ„์น˜ ์ „์˜ ๋…ธ๋“œ ์ฐพ๊ธฐ
        Node targetNode = temp.nextNode; // temp ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋  ๋…ธ๋“œ
        temp.nextNode = temp.nextNode.nextNode; // temp ๋…ธ๋“œ๋Š” ์‚ญ์ œ๋  ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด

        /*๋…ธ๋“œ ๋ฐ˜ํ™˜ ๊ณผ์ •/
        Object returnData = targetNode.data;a

        if(targetNode == tailNode){
            tailNode = temp;
        }
        targetNode = null;
        size--;
        return returnData;
    }

}

๋Œ“๊ธ€