LinkedList 자료구조
DFS(깊이 우선 탐색) 구현 시에 LinkedList를 사용한다.
각 노드가 데이터와 포인터를 가지고 한 줄로 연결되어 있는 방식의 자료구조이다.
객체를 추가하거나 삭제하면 앞, 뒤 링크만 변경되고 나머지 링크는 변경되지 않는다.
데이터 추가나 삭제 시 인덱스가 밀리거나 당겨지는 일이 없기에 ArrayList에 비해 추가, 삭제가 용이하다.
(1) 선언 방법
LinkedList list = new LinkedList();
LinkedList<Student> student = new LinkedList<Student>(); // 타입 지정
LinkedList<Student> members = new LinkedList<Student>(); // 타입 지정
(2) 값 추가하기
list.addFirst(1); // 가장 앞에 데이터 추가
list.addLast(2); // 가장 마지막에 데이터 추가
list.add(3); // 가장 마지막에 데이터 추가
list.add(1, 10); // index 1 뒤에 데이터 10 추가
(3) 값 삭제하기
list.removeFirst(); // 가장 앞의 데이터 제거
list.removeLast(); // 가장 마지막 데이터 제거
list.remove(1); // index 1 제거, 생략시 index 0 제거
list.clear(); // 모든 값 제거
(4) 크기 구하기
list.size();
(5) 값 검색하기
System.out.println(list.contains(1)); // list에 1이 있는지 Boolean 값 반환
System.out.println(list.indexOf(1)); // 1이 있다면 해당하는 index 반환, 없다면 -1 반환
'CS > Algorithm' 카테고리의 다른 글
[Algorithm] 그래프(Graph) (0) | 2020.11.15 |
---|---|
[Algorithm/Java] Iterator, ListIterator (0) | 2020.11.14 |
[Algorithm] BFS, 너비 우선 탐색 (0) | 2020.11.14 |
[Algorithm] DFS, 깊이 우선 탐색 (0) | 2020.11.02 |
[Algorithm] 완전탐색 (Exhaustive Search) (0) | 2020.10.09 |