[자료구조] Linked List - 2

zerokick·2023년 4월 15일
0

Data Structure

목록 보기
6/14
post-thumbnail

Linked List - 2


Linked List 데이터 핸들링

1. 데이터 삽입

삽입할 노드의 이전 노드의 '다음 노드 주소'에 삽입할 노드의 주소를 넣어준다.
삽입할 노드의 '다음 노드 주소'에 다음 노드의 주소를 넣어준다.

    public static class SingleLinkedList<T> {
        public Node<T> search(T data) {
            if(this.head == null) {
                return null;
            } else {
                Node<T> node = this.head;

                while(node != null) {
                    if(node.data == data) {
                        return node;
                    } else {
                        node = node.next;
                    }
                }

                return null;
            }
        }

        public void addNodeInside(T data, T isData) {
            Node<T> bfNode = this.search(isData);

            if(bfNode == null) {
                this.addNode(data);
            } else {
                Node<T> nextNode = bfNode.next;
                bfNode.next = new Node<T>(data);
                bfNode.next.next = nextNode;
            }
        }
    }
    
    public static void main(String[] args) {

		// LinkedList 중간 노드 삽입
        System.out.println("");
        System.out.println("======================== LinkedList 중간 노드 삽입 ========================");

        SingleLinkedList<Integer> linkedList3 = new SingleLinkedList<Integer>();

        linkedList3.addNode(1);
        linkedList3.addNode(2);
        linkedList3.addNode(3);
        linkedList3.printAll();

        // 2 뒤에 4 삽입
        linkedList3.addNodeInside(4, 2);
        linkedList3.printAll();

        // 3 뒤에 5 삽입
        linkedList3.addNodeInside(5, 3);
        linkedList3.printAll();

        // 없는 데이터를 찾도록 하여 맨 뒤에 6 삽입
        linkedList3.addNodeInside(6, 20);
        linkedList3.printAll();
    }
}

2. 데이터 삭제

삭제할 노드의 주소를 담고 있는 이전 노드의 '다음 노드 주소'에 삭제할 노드의 다음 노드의 주소를 넣어준다.

    public static class SingleLinkedList<T> {
        public boolean delNode(T isdata) {
            if(this.head == null) {
                return false;
            } else {
                Node<T> node = this.head;
                if(node.data == isdata) {
                    this.head = this.head.next;
                    return true;
                } else {
                    while(node.next != null) {
                        if(node.next.data == isdata) {
                            node.next = node.next.next;
                            return true;
                        }
                        node = node.next;
                    }

                    return false;
                }
            }
        }
    }

    public static void main(String[] args) {

        // LinkedList 특정 노드 삭제
        System.out.println("");
        System.out.println("======================== LinkedList 특정 노드 삭제 ========================");

        SingleLinkedList<Integer> linkedList4 = new SingleLinkedList<Integer>();

        linkedList4.addNode(1);
        linkedList4.addNode(2);
        linkedList4.addNode(3);
        linkedList4.addNode(4);
        linkedList4.addNode(5);
        linkedList4.printAll();

        boolean flag = true;

        // 중간 노드 삭제
        flag = linkedList4.delNode(2);
        System.out.print(flag + " : ");
        linkedList4.printAll();

        // 헤드 노드 삭제
        flag = linkedList4.delNode(1);
        System.out.print(flag + " : ");
        linkedList4.printAll();

        // 마지막 노드 삭제
        flag = linkedList4.delNode(5);
        System.out.print(flag + " : ");
        linkedList4.printAll();

        // 없는 데이터 노드 삭제
        flag = linkedList4.delNode(20);
        System.out.print(flag + " : ");
        linkedList4.printAll();
    }
profile
Opportunities are never lost. The other fellow takes those you miss.

0개의 댓글