HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🛁
공부기록
/
🚡
알고리즘 정리
/
🚤
이중 연결리스트
🚤

이중 연결리스트

태그

더블 링크드 리스트

  • 양방향으로 연결이 되어있기 때문에 노드의 탐색이 양쪽(전, 후)으로 가능하다.
public class MyDoubleLinkedList<T> { public Node<T> head = null; public Node<T> tail = null; public class Node<T> { T data; Node<T> prev = null; Node<T> next = null; public Node(T data) { this.data = data; } } public void addInsideNode(T data,T isData) { if(this.head == null) { addNode(data); } else if (this.head.data == isData) { Node<T> newHead = new Node<>(data); newHead.next = this.head; this.head = newHead; } else { Node<T> node = this.head; while (node != null) { if (node.data == isData) { Node<T> prevNode = node.prev; prevNode.next = new Node<>(data); prevNode.next.next = node; prevNode.next.prev = prevNode; node.prev = prevNode.next; break; } node = node.next; } } } public void addNode(T data) { if (this.head == null) { this.head = new Node<>(data); this.tail = this.head; } else { Node<T> node = this.head; while (node.next != null) { node = node.next; } node.next = new Node<>(data); node.next.prev = node; this.tail = node.next; } } public void printAll() { if (this.head != null) { Node<T> node = this.head; System.out.println(node.data); while (node.next != null) { node = node.next; System.out.println(node.data); } } } private T searchFromHead(T isData) { if (this.head == null) { return null; } else { Node<T> node = this.head; while (node != null) { if (node.data == isData) { return node.data; } else { node = node.next; } } return null; } } public T searchFromTail(T isData) { if (this.head == null) { return null; } else { Node<T> node = this.tail; while (node != null) { if (node.data == isData) { return node.data; } else { node = node.prev; } } return null; } } public static void main(String[] args) { MyDoubleLinkedList<Integer> myDoubleLinkedList = new MyDoubleLinkedList<>(); myDoubleLinkedList.addNode(1); myDoubleLinkedList.addNode(3); myDoubleLinkedList.addNode(5); myDoubleLinkedList.addNode(7); myDoubleLinkedList.addNode(9); myDoubleLinkedList.addInsideNode(6, 7); myDoubleLinkedList.printAll(); } }