コード例 #1
0
 public void print() {
   Node current = head;
   while (current != null) {
     System.out.println(current.data());
     current = current.next;
   }
 }
コード例 #2
0
 public void addRear(String data) {
   if (head == null) {
     head = new Node(null, data, null);
   } else {
     Node current = head;
     while (current.next != null) {
       current = current.next;
     }
     Node newNode = new Node(current, data, null);
     current.next = newNode;
   }
   size++;
 }
コード例 #3
0
 public void addFront(String data) {
   if (head == null) {
     head = new Node(null, data, null);
   } else {
     Node newNode = new Node(null, data, head);
     head.previous = newNode;
     head = newNode;
   }
   size++;
 }
コード例 #4
0
  public void insert(String data, int index) {
    if (head == null) return;
    if (index < 1 || index > size) return;

    Node current = head;
    int i = 1;
    while (i < index) {
      current = current.next;
      i++;
    }

    if (current.previous == null) {
      Node newNode = new Node(null, data, current);
      current.previous = newNode;
      head = newNode;
    } else {
      Node newNode = new Node(current.previous, data, current);
      current.previous.next = newNode;
      current.previous = newNode;
    }
    size++;
  }