@Override public void addAtPosition(Object data, int position) { // TODO Auto-generated method stub if (position == 0) { addFront(data); return; } if (position >= this.length()) { addBack(data); return; } Node newNode = new Node(data); if (this.head == null && this.tail == null) { this.head = newNode; this.tail = newNode; } else { Node current = this.head; Node previous = this.head; int counter = 0; while (current != null) { if (counter == position) { previous.setNext(newNode); newNode.setPrev(previous); newNode.setNext(current); current.setPrev(newNode); break; } previous = current; current = current.getNext(); counter++; } } }
@Override public void addFront(Object data) { // TODO Auto-generated method stub Node newNode = new Node(data); if (this.head == null) { this.head = newNode; } else { Node tempHead = this.head; newNode.setNext(tempHead); tempHead.setPrev(newNode); this.head = newNode; } }
public void addBack(Object data) { // TODO Auto-generated method stub Node newNode = new Node(data); if (this.head == null && this.tail == null) { this.head = newNode; this.tail = newNode; } else { Node tempTail = this.tail; tempTail.setNext(newNode); newNode.setPrev(tempTail); this.tail = newNode; } }