자료구조 - 연결리스트(LinkedList)

code++·2024년 9월 27일
public class LinkedList {
    Node head;
    int size = 0;
    class Node{
        int data;
        Node next;
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }


    public void addFirst(int data) {
        Node newNode = new Node(data);//새로운 노드 생성

        newNode.next = head; //이 전 head를 가리켜야함.
        head = newNode;// newNode가 head다.
        size++;
    }

    public void addLast(int data) {
        Node newNode = new Node(data);//새로운 노드 생성
        if (head == null) {
            head = newNode;
            size++;
        }else{
            Node temp = head; //임시변수를 head라고 하자
            while (temp.next != null) {
                temp = temp.next; //다음 노드로 이동.
            }
            temp.next = newNode;
            size++;

        }

    }

    public void printList() {
        Node temp = head;  // 임시 변수로 head를 사용
        while (temp != null) {
            System.out.print(temp.data + " -> ");  // 현재 노드의 데이터 출력
            temp = temp.next;  // 다음 노드로 이동
        }
        System.out.println("null");  // 리스트의 끝을 나타내기 위해 null 출력
        System.out.println("총 리스트 크기는: "+ size);
    }
    public void insert(int data, int position) {
        Node newNode = new Node(data);

        //position이 맨앞이면?
        if (position == 0) {
            addFirst(data);
        }
        Node temp = head;
        // 첫번째 포지션부터 마지막 포지션까지 접근하는데...
        for (int i = 0; i < position - 1; i++) {
            if (temp == null) { // 임시 변수(헤드)가 아무것도 가리키지 않는다면?
                System.out.println("position에 벗어난것같다.");
            }
            // 임시변수가 뭐라도 건드린다면?
            temp = temp.next; // 다음 노드로이동
        }
        newNode.next = temp.next;
        temp.next = newNode;
        size++;
    }

    public void delete(int key) {
        Node temp = head;// 임시변수가 헤드를
        Node prev = null; // 이전 노드

        if (temp == null) { // 임시변수가 아무 값도 안가리키면?
            System.out.println("리스트가 비어 있습니다.");
        }

        //첫번째 노드 삭제

        if (temp != null && temp.data == key) {
            head = temp.next;
            size--;
            return;
        }
        //나머지 노드 삭제
        while (temp != null && temp.data != key) {
            prev = temp;
            temp = temp.next;
        }
        if (temp == null) {
            System.out.println("노드를 찾을 수 없습니다. " +key);
        }
        prev.next = temp.next;

    }

    // 연결 리스트에서 특정 요소 검색 메서드
    public boolean search(int key) {
        Node temp = head;  // 임시 변수로 head를 사용
        while (temp != null) {
            if (temp.data == key) {
                return true;  // 요소를 찾으면 true 반환
            }
            temp = temp.next;  // 다음 노드로 이동
        }
        return false;  // 요소를 찾지 못하면 false 반환
    }
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addLast(3);

        linkedList.addFirst(1);
        linkedList.addFirst(2);
        linkedList.addLast(4);
        linkedList.addLast(5);
        // 2-> 1 -> 3 ->4 ->5
        linkedList.printList();
        linkedList.insert(8,2);
        // 2-> 1 -> 8 -> 3 ->4 ->5
        linkedList.printList();
        linkedList.insert(15,4);
        // 2 -> 1 ->8 -> 3 -> 15 -> 4 -> 5
        linkedList.printList();
        linkedList.delete(2);

        linkedList.printList();

    }
}
profile
일상

0개의 댓글