class Item{
public int data;
public Item next;
public Item prev;
public Item(int data){
//オブジェクトの作成
//データの受取
this.data = data;
//次のデータ
this.next = null;
//前のデータ
this.prev = null;
}
}
class DoublyLinkedList{
Item head;
Item tail;
public DoublyLinkedList(int arr[]){
//リストが0だったら
if (arr.length<= 0){
this.head = null;
this.tail = this.head;
//処理終わり
return;
}
//先頭を設定
this.head = new Item(arr[0]);
//ノードの格納変数を宣言
Item currentNode = head;
//リストが0だったら
//固定配列をすべてノード化
for (int i = 1;i<arr.length;i++){
currentNode.next = new Item(arr[i]);
//prevをつける
currentNode.next.prev = currentNode;
//currentNodeを次のノードにしていく
currentNode = currentNode.next;
}
this.tail = currentNode;
currentNode = this.head;
}
}
class MyClass{
public static void main(String[] args){
int [] arr = new int[]{1,2,3,4,5,6,7};
DoublyLinkedList numList = new DoublyLinkedList(arr);
System.out.println(numList.head.data);//1
System.out.println(numList.head.next.data);//2
System.out.println(numList.head.next.prev.data);//1
System.out.println(numList.tail.data);//7
System.out.println(numList.tail.prev.data);//6
System.out.println(numList.tail.prev.prev.data);//5
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme