코드와이
LinkedList 본문
Node
package week0208_0212;
public class Node {
public String data;
public Node link;
public Node(String data) {
super();
this.data = data;
}
public Node(String data, Node link) {
this.data = data;
this.link = link;
}
@Override
public String toString() {
return "Node [data=" + data + ", link=" + link + "]";
}
}
SinglyLinkedList
package week0208_0212;
public class SinglyLinkedList {
private Node head;
public void addFirstNode(String data) {
Node curr = new Node(data, head);
head = curr;
}
public Node getLastNode() {
if (head == null) return null;
else {
Node curr = head;
while(curr.link != null) {
curr = curr.link;
}
return curr;
}
}
public void addLastNode(String data) {
Node curr = new Node(data);
if(head == null) head = curr;
else getLastNode().link = curr;
}
public void insertAfterNode(Node prev, String data) {
if( prev == null ) {
System.out.println("Prev nowhere");
return;
}
Node curr = new Node(data, prev.link);
prev.link = curr;
}
public Node getNode(String data){
for (Node currNode = head; currNode != null; currNode = currNode.link) {
if (currNode.data.equals(data)) {
return currNode;
}
}
return null;
}
public Node getPreviousNode(Node target) {
Node curr = head, nextn = null;
if( curr != null) {
while( (nextn = curr.link) != null) {
if( nextn == target) return curr;
curr = curr.link;
}
}
return null;
}
public void deleteNode(String data) {
if(head==null){
System.out.println("공백리스트이어서 삭제가 불가능합니다.");
return;
}
Node targetNode = getNode(data);
if(targetNode == null) return;
Node preNode = getPreviousNode(targetNode);
if(preNode == null && targetNode == head) {
head = targetNode.link;
}else {
preNode.link = targetNode.link;
}
targetNode.link = null;
}
public void printList() {
System.out.print("L = ( ");
for (Node currNode = head; currNode != null; currNode = currNode.link) {
System.out.print(currNode.data+" ");
}
System.out.println(" )");
}
}
'algorithm' 카테고리의 다른 글
[Back Tracking] N-Queen 예제 + 설명 (0) | 2021.02.18 |
---|---|
분할 알고리즘 예시(1) (0) | 2021.02.16 |
CompleteBinaryTree (0) | 2021.02.10 |
부분집합 (0) | 2021.02.05 |
순열과 조합 (0) | 2021.02.05 |