티스토리 뷰

오늘은 링크드리스트를 자바로 구현해보았다.

 

Linked List 

public class Node {

    public int data;
    public Node next;

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

public class LinkedList {

    private Node head;

    public LinkedList() {
        this.head = null;
    }
// 데이터 추가
    public void add(int data) {
        Node newNode = new Node(data);

        Node current = head;
        if (head == null) {
            head = newNode;
        } else {
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }
// 원하는 위치에 데이터 추가
    public void add(int data, int index) {
        Node newNode = new Node(data);

        if (index == 0) {
            newNode.next = head;
            head = newNode;
        } else {
            Node current = head;
            int currentIndex = 0;
            while (current.next != null && currentIndex < index - 1) {
                current = current.next;
                currentIndex++;
            }
            if (current == null) {
                System.out.println("Not exist index");
                return;
            }
            newNode.next = current.next;
            current.next = newNode;
        }
    }
// 데이터 삭제
    public void remove() {
        Node current = head;
        Node preCurrent = null;

        while (current.next != null) {
            preCurrent = current;
            current = current.next;
        }
        preCurrent.next = null;
    }
// 원하는 위치의 데이터 삭제
    public void remove(int index) {
        Node current = head;
        Node preCurrent = null;
        int currentIndex = 0;

        if (index == 0) {
            head = current.next;
            return;
        }

        while (currentIndex != index && current != null) {
            preCurrent = current;
            current = current.next;
            currentIndex++;
        }
        if (current == null) {
            System.out.println("out of index");
            return;
        } else {
            preCurrent.next = current.next;
            return;
        }

    }
// 출력하기
    public void display() {
        Node current = head;
        while (current != null) {
            System.out.println(current.data);
            current = current.next;
        }
    }
// 원하는 위치의 데이터 찾기
    public void find(int index) {
        Node current = head;
        int currentIndex = 0;
        while (true) {
            if (current == null) {
                System.out.println("out of index");
                return;
            }
            if (index == currentIndex) {
                System.out.println("find : " + current.data);
                return;
            }
            current = current.next;
            currentIndex++;
        }
    }

    public static void main(String[] args) {
        LinkedList testLinkedList = new LinkedList();
        testLinkedList.add(10);
        testLinkedList.add(10);
        testLinkedList.add(10);
        testLinkedList.add(5, 1);
        testLinkedList.add(4, 1);
        testLinkedList.add(101);
        testLinkedList.display();
        System.out.println("---------------------");
        testLinkedList.remove(0);
        testLinkedList.remove(1);
        testLinkedList.display();
    }
}

'개발 > 자료구조 알고리즘' 카테고리의 다른 글

[자료구조] 원형 큐 (Circular Queue) 구현  (0) 2023.10.26
[자료구조] 그래프  (0) 2023.06.13
[자료구조] Binary Tree  (0) 2023.05.25
[자료구조] HashTable 구현  (0) 2023.05.17
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/10   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함