양방향 체인 테이블 자바 구현
9830 단어 문제 요약
import java.util.Iterator;
public class TwoWayLink implements Iterable {
Node head = null;
Node tail = null;
int size;
class Node {
Node next = null;
Node previous = null;
int data;
public Node(int data) {
this.data = data;
}
@Override
public String toString() {
return "data:" + data;
}
}
public void addNode(int d) {
Node newNode = new Node(d);
if (head == null) {
head = newNode;
tail = newNode;
size = 1;
return;
}
Node tailTmp = tail;
tailTmp.next = newNode;
newNode.previous = tailTmp;
tail = newNode;
size++;
}
public Node getNode(int index) {
if (index == 0) {
return head;
}
if (index + 1 == size) {
return tail;
}
int i = 1;
Node curNode = head.next;
while (curNode != null) {
if (i == index) {
return curNode;
}
curNode = curNode.next;
i++;
}
return null;
}
public Node removeNode(Node remove) {
Node previous = remove.previous;
Node next = remove.next;
if (remove.equals(head)) {
next.previous = null;
head = next;
} else if (remove.equals(tail)) {
previous.next = null;
tail = previous;
} else {
previous.next = next;
next.previous = previous;
}
size--;
return remove;
}
public int size() {
return size;
}
@Override
public Iterator iterator() {
return new Iterator() {
int cursor;
int lastRet = -1;
@Override
public boolean hasNext() {
return cursor != size;
}
@Override
public Object next() {
lastRet = cursor;
cursor++;
return getNode(lastRet);
}
};
}
public static void main(String[] args) {
TwoWayLink twoWayLink = new TwoWayLink();
twoWayLink.addNode(1);
twoWayLink.addNode(2);
twoWayLink.addNode(3);
System.out.println(twoWayLink.size());
Iterator iterator = twoWayLink.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
MySQLNonTransientConnectionException: No operations allowed after connection closed오늘 서버가 갑자기 오류를 보고했습니다. 인터페이스가 오류를 반환했습니다. 로그 찾기: mysql 문서 검색: dbcp 연결 풀 구성 수정: 이로써 문제가 해결되었다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.