티스토리 뷰

반응형

 

 

해커랭크 Day 15 챌린지를 시작해보자. 🔥

 

오늘은  LinkedList에 대해서 배웠다.

 

먼저 소스코드부터 확인하자.

 


 

import java.util.LinkedList;

public class LinkedListUS {
    // Properties
    Node head;
    int count;

    // Constructors
    public LinkedListUS() {
        head = null;
        count = 0;
    }

    public LinkedListUS(Node newHead) {
        head = newHead;
        count = 1;
    }

    // Methods

    // add
    public void add(int newData) {
        Node temp = new Node(newData);
        Node current = head;
        while(current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(temp);
        count++;
    }

    // get
    public int get(int index) {
        if (index <= 0) {
            return -1;
        }
        Node current = head;
        for(int i=0; i<index; i++) {
            current = current.getNext();
        }
        return current.getData();
    }

    // size
    public int size() {
        return count;
    }

    // isEmpty
    public boolean isEmpty() {
        return head == null;
    }

    // remove
    public void remove() {
        // remove from the back of the list
        Node current = head;
        while(current.getNext().getNext() != null) {
             current = current.getNext();
        }
        current.setNext(null);
        count--;
    }

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.add("Alice");
        System.out.println(linkedList);
        linkedList.add("Alicey");
    }
}

 

 

linkedList는 학교 자료구조 시간에 처음 배웠고

자바의 정석 강의를 통해

조금 더 자세히 알게 되었다.

 

위 소스 코드는 LinkedList가 무엇인지 알고 있어야

이해할 수 있으므로

만약 LinkedList가 무엇인지 모른다면

다음의 사이트를 참고하자.

 

 

 

https://dandev.tistory.com/entry/%EC%9E%90%EB%B0%94%EC%9D%98%EC%A0%95%EC%84%9D-ch-11-12-LinkedList-%EB%B0%B0%EC%97%B4%EC%9D%98-%EC%9E%A5%EB%8B%A8%EC%A0%90?category=1009920 

 

[자바의정석] ch 11-12 LinkedList - 배열의 장단점

🤔 LinkedList 배열의 장단점 ✅ 장점 ✔ 배열은 구조가 간단하고 데이터를 읽는 데 걸리는 시간(접근시간, access time)이 짧다. ✅ 단점 ✔ 크기를 변경할 수 없다. 크기를 변경해야 할 경우 새로운

dandev.tistory.com

 

https://dandev.tistory.com/entry/%EC%9E%90%EB%B0%94%EC%9D%98-%EC%A0%95%EC%84%9D-Ch11-1-%EC%BB%AC%EB%A0%88%EC%85%98-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8Dcollections-framework-Ch-11-7-ArrayList?category=1009920 

 

[자바의 정석] Ch11-1 컬레션 프레임웍(collections framework) / Ch 11-7 ArrayList

🤔 컬렉션 프레임워크(collections framework) ✔ 컬렉션(collection) - 여러 객체(데이터)를 모아 놓은 것 ✔ 프레임웍(framework) - 표준화, 정형화된 체계적인 프로그래밍 방식 ✔ 컬렉션 프레임웍(collections

dandev.tistory.com

 

 


 

위 두 사이트는 필자가 위에서 말한

자바의 정석 강의 내용을

정리해둔 포스팅으로 LinkedList의 개념이 나오니

함께 살펴보면 도움될 것이다!

 

그 외에 소스코드에 대한 설명은

linkedList에 대한 이해를 바탕으로 진행하기 때문에

딱히 설명하지 않겠다!!

 

만약 이해가 되지 않는 부분이 있다면 댓글을 통해 질문하길 바란다!

 

 

그럼 필자는 Day 15 문제 풀이 포스팅으로 다시 찾아오겠다! 

 

반응형