Simple Linked List (단순 연결 리스트) 구조
[LinkedListTest.java]
package testCode;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
System.out.println("공백 리스트에 노드 3개 삽입");
linkedList.insertLastNode("월");
linkedList.insertLastNode("수");
linkedList.insertLastNode("일");
linkedList.printList();
System.out.println("수 노드 뒤에 금 노드 삽입");
ListNode prev = linkedList.searchNode("수");
if(prev == null) {
System.out.println("찾는 정보가 없습니다.");
} else {
linkedList.insertMidNode(prev, "금");
}
linkedList.printList();
System.out.println("리스트의 첫 번째에 노드 추가");
linkedList.insertFirstNode("토");
linkedList.printList();
System.out.println("리스트의 마지막 노드 삭제");
linkedList.deleteLasNode();
linkedList.printList();
System.out.println("리스트의 중간 노드 수 삭제");
linkedList.deleteMidNode("수");
linkedList.printList();
System.out.println("리스트의 첫 번째 노드 삭제");
linkedList.deleteFirstNode();
linkedList.printList();
System.out.println("리스트 역순으로 출력");
linkedList.reverseList();
linkedList.printList();
}
}
[LinkedList.java]
package testCode;
public class LinkedList {
private ListNode head;
public LinkedList() {
this.head = null;
}
// 삽입
void insertLastNode(String str) {
ListNode node = new ListNode(str);
if(head == null) {
head = node;
} else {
ListNode current = head;
while(current.next != null) {
current = current.next;
}
current.next = node;
}
}
void insertMidNode(ListNode pre, String str) {
ListNode node = new ListNode(str);
if(head == null) {
head = node;
} else {
ListNode current = head;
while(current.next != pre) {
current = current.next;
}
current = current.next;
node.next = current.next;
current.next = node;
}
}
void insertFirstNode(String str) {
ListNode node = new ListNode(str);
if(head == null) {
head = node;
} else {
ListNode current = head;
node.next = current;
head = node;
}
}
// 삭제
void deleteLasNode() {
if(head == null) {
System.out.println("삭제할 노드가 존재하지 않습니다.");
} else {
ListNode prev = head;
ListNode curr = head.next;
while(curr.next != null) {
prev = curr;
curr = curr.next;
}
prev.next = null;
}
}
void deleteMidNode(String str) {
ListNode node = new ListNode(str);
if(head == null) {
System.out.println("삭제할 노드가 존재하지 않습니다.");
} else {
ListNode prev = head;
ListNode curr = head.next;
while(curr.data != node.data) {
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
}
}
void deleteFirstNode() {
if(head == null) {
System.out.println("삭제할 노드가 존재하지 않습니다.");
} else {
head = head.next;
}
}
// 조회
ListNode searchNode(String str) {
ListNode node = new ListNode(str);
if(head == null) {
System.out.println("노드가 비어있습니다.");
} else {
ListNode curr = head;
while(curr.data != node.data) {
curr = curr.next;
}
}
return node;
}
void reverseList() {
ListNode next = head;
ListNode curr = null;
ListNode prev = null;
while(next != null) {
prev = curr;
curr = next;
next = next.next;
curr.next = prev;
}
head = curr;
}
void printList() {
if(head == null) {
System.out.println("출력할 리스트가 없스니다.");
} else {
ListNode curr = head;
System.out.print("[");
while(curr.next != null) {
System.out.println(curr.data + "");
curr = curr.next;
}
System.out.print(curr.data);
System.out.print("]");
System.out.println();
}
}
}
[ListNode.java]
package testCode;
public class ListNode {
String data;
ListNode next;
public ListNode() {
this.data = null;
this.next = null;
}
public ListNode(String data) {
this.data = data;
this.next = null;
}
public ListNode(String data, ListNode next) {
this.data = data;
this.next = next;
this.next = null;
}
public String getData() {
return this.data;
}
}
'JAVA' 카테고리의 다른 글
[TEST] (0) | 2019.12.24 |
---|---|
[JAVA] StringBuilder (0) | 2019.12.18 |
[JAVA] int 자릿수 별 int 배열 분할 (0) | 2019.12.16 |