最初にリンクリストクラスを定義し、次に実装します
追いかけて、写真を投稿してください
リンクリスト
public class Node {
private Object data;
private Node next;
public Node() {
}
public Node(Object data, Node next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
リンクリスト
public class LinkedList implements List{
private Node head;
private int size;
public LinkedList() {
head = new Node(null,null);
}
@Override
public void add(int index, Object obj) {
if(index<0 || index >size) return;
Node curr = head;
for(int i=0;i<index;i++) {
curr = curr.getNext();
}
Node node = new Node();
node.setData(obj);
node.setNext(curr.getNext());
curr.setNext(node);
size++;
}
この図とコードによれば、リンクリストの挿入を簡単に理解できます。
好き、好き、集めて!!!