순서를 가진 데이터의 집합을 가리키는 추상자료형(Abstract Data Type)
구현방법에 따른 분류
특성
기본 구조
노드(Node)
하나의 원소에 필요한 데이터를 갖고 있는 자료 단위
구성요소
1) 데이터 필드
- 원소의 값을 저장하는 자료구조
- 저장할 원소의 종류나 크기에 따라 구조를 정의하여 사용
2) 링크 필드
헤드(Head)
리스트의 처음 노드를 가리키는 레퍼런스
연결 구조
# 첫 번째 노드로 삽입하는 알고리즘
addtoFirst(L, i)
new <= createNode();
new.data = i;
new.link = L;
L = new;
end addtoFirst()
# 가운데 노드로 삽입하는 알고리즘
add(L, pre, i)
new <= createNode()
new.data = i;
if (L=NULL) then{
L = new;
new.link = NULL;
}
else{
new.link = pre.link
pre.link = new;
}
end add()
addtoLast(L, i)
new <= createNode()
new.data = i;
new.link = NULL;
if (L=NULL) then{
L = new;
return;
}
temp = L;
while (temp.link != NULL) do
temp = temp.link;
temp.link = new;
end addtoLast()
양쪽 방향으로 순회할 수 있도록 노드를 연결한 리스트
두 개의 링크 필드와 한 개의 데이터필드로 구성
push
와 pop
구현public class Node {
String data;
Node link;
public Node(String data, Node link) {
super();
this.data = data;
this.link = link;
}
public Node(String data) {
this.data = data;
}
}
public class SimpleLinkedList {
private Node head;
public void addFirstNode(String data) {
Node newNode = new Node(data, head);
head = newNode;
}
public void popFirstNode() {
Node secondNode = head.link;
head.link = null;
head = secondNode;
}
public void printList() {
for (Node currNode=head; currNode!=null; currNode=currNode.link) {
System.out.print(currNode.data + " ");
}
System.out.println();
}
}