JAVA

[JAVA] LinkedList 직접 구현해보기

개발중인 감자 2023. 8. 7. 17:44

 

✏️ Node 자료형 구현 : MyListNode

public class MyListNode {
    private String data;
    public MyListNode next;

    public MyListNode() {
        data = null;
        next = null;
    }

    public MyListNode(String data) {
        this.data = data;
        this.next = null;
    }

    public MyListNode(String data, MyListNode next) {
        this.data = data;
        this.next = next;
    }

    public String getData() {
        return data;
    }
}

 

✏️ MyLinkedList 구현 

public class MyLinkedList {
    private MyListNode head;
    int count;
    public MyLinkedList() {
        head = null;
        count = 0;
    }
    public MyListNode addElement(String data) {
        MyListNode node = new MyListNode(data);
        if (head == null) {
            head = node;
        } else {
            MyListNode tmp = head;
            while (tmp.next != null) {
                tmp = tmp.next;
            }
            tmp.next = node;
        }
        count++;
        System.out.println("addElement (" + (count-1) + "): " +  node.getData());
        return node;
    }

    public MyListNode insertElement(int pos, String data) {
        MyListNode node = new MyListNode(data);

        if (pos < 0 || pos > count) {
            System.out.println("추가할 위치("+pos+")는 오류입니다. 현재 리스트의 사이즈는 " + getSize() + "입니다. ");
            return null;
        }

        if (pos == 0) {
            MyListNode tmp = head;
            head = node;
            head.next = tmp;
        } else {
            MyListNode pre = null;
            MyListNode nxt = head;
            for (int i = 0; i < pos; i++) {
                pre = nxt;
                nxt = nxt.next;
            }
            node.next = nxt;
            pre.next = node;
        }

        count++;
        System.out.println("insertElement (" + pos + "): " +  node.getData());
        return node;

    }

    public MyListNode removeElement(int pos) {

        if (pos < 0 || pos >= count) {
            System.out.println("삭제할 위치("+pos+")는 오류입니다. 현재 리스트의 사이즈는 " + getSize() + "입니다. ");
            return null;
        }

        MyListNode node = head;

        if (pos == 0) {
            head = head.next;
        } else {
            MyListNode pre = null;
            MyListNode tmp = head;
            for (int i = 0; i < pos; i++) {
                pre = tmp;
                tmp = tmp.next;
            }
            pre.next = tmp.next;
            node = tmp;
        }

        count--;
        if (!isEmpty()) System.out.println("removeElement (" + pos + "): " +  node.getData());
        else System.out.println("removeElement (" + pos + "): 리스트가 전부 비었습니다.");

        return node;

    }

    public String getElement(int position)
    {
        if (position < 0 || position >= count) {
            System.out.println("검색 오류");
            return new String("Error");
        }

        if (position == 0) return head.getData();

        MyListNode node = head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }
        return node.getData();
    }

    public MyListNode getNode(int position)
    {
        if (position < 0 || position >= count) {
            System.out.println("검색 오류");
            return null;
        }

        if (position == 0) return head;

        MyListNode node = head;
        for (int i = 0; i < position; i++) {
            node = node.next;
        }
        return node;
    }

    public void removeAll()
    {
        head = null;
        count = 0;
        System.out.println("remove All");
    }

    public int getSize()
    {
        return count;
    }

    public void printAll()
    {
        if(count == 0){
            System.out.println("출력할 내용이 없습니다.");
            return;
        }

        System.out.print("전체 출력: ");
        MyListNode temp = head;
        while(temp != null){
            System.out.print(temp.getData());
            temp = temp.next;
            if(temp!=null){
                System.out.print("->");
            }
        }
        System.out.println("");
    }

    public boolean isEmpty()
    {
        if(head == null) return true;
        else return false;
    }


}

 

✏️ 테스트 코드 

public class MyLinkedListTest {
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addElement("A");
        list.addElement("B");
        list.addElement("C");
        list.printAll();
        list.insertElement(10, "D");
        list.insertElement(3, "D");
        list.printAll();
        list.removeElement(0);
        list.printAll();
        list.removeElement(1);
        list.removeElement(10);
        list.printAll();

        list.insertElement(0, "AAAA");
        list.printAll();
        System.out.println("getSize(): " + list.getSize());

        list.removeElement(0);
        list.printAll();
        System.out.println("getSize(): " + list.getSize());

        list.removeAll();
        list.printAll();
        list.addElement("A");
        list.insertElement(10, "AA");
        list.printAll();
        System.out.println("getElement(0): " + list.getElement(0));
        list.removeElement(0);
    }
}

 

✏️ 실행 결과