importjava.util.Iterator;public class MyLinkedList extends MyAbstractList{private Node head;//头指针
private Node tail;//尾指针
private int size = 0;//链表长度
publicMyLinkedList() {
}publicMyLinkedList(E[] elements) {for(int i = 0; i < elements.length; i++) {
addLast(elements[i]);
}
}public intsize() {returnsize;
}protectedE getFirst() {//TODO Auto-generated method stub
if(size == 0) {return null;
}else{returnhead.element;
}
}protectedE getLast() {//TODO Auto-generated method stub
if(size == 0) {return null;
}else{returntail.element;
}
}public voidadd(E e) {
addLast(e);
}protected voidaddFirst(E e) {//TODO Auto-generated method stub
Node newNode = new Node<>(e);//创建新的Node
newNode.next = head;//将newNode节点的next节点设为head指针指向的节点
head = newNode;//head指针重新指向新加入的节点
size++;if(tail == null) {//如果链表为空,将尾指针也指向newNode
tail =newNode;
}
}protected voidaddLast(E e) {//TODO Auto-generated method stub
Node newNode = new Node<>(e);//创建新的Node
if(tail == null) {//如果链表为空,将头指针和尾指针指向newNode
head = tail =newNode;
}else{
tail.next= newNode;//将尾指针指向的Node的next节点设为newNode
tail = newNode;//尾指针指向新加入的节点
}
size++;
}
@Overridepublic void add(intindex, E e) {//TODO Auto-generated method stub
if(index <= 0) {
addFirst(e);
}else if(index >=size) {
addLast(e);
}else{
Node current =head;for(int i = 1; i < index; i++) {
current=current.next;
}
Node temp =current.next;
current.next= new Node(e);
(current.next).next=temp;
size++;
}
}//判断下标是否合法
private void checkIndex(intindex) {if(index < 0 || index >=size) {throw new IndexOutOfBoundsException("index " + index + " out of bounds");
}
}protectedE removeFirst() {//TODO Auto-generated method stub
if(size == 0) {return null;
}else{
Node temp =head;
head=head.next;
size--;if(head == null) {
tail= null;
}returntemp.element;
}
}protectedE removeLast() {//TODO Auto-generated method stub
if(size == 0) {return null;
}else if(size == 1){
Node temp =head;
head= tail = null;
size= 0;returntemp.element;
}else{
Node current =head;for(int i = 1; i < size - 1; i++) {
current=current.next;
}
Node temp =tail;
tail=current;
tail.next= null;
size--;returntemp.element;
}
}
@Overridepublic E remove(intindex) {//TODO Auto-generated method stub
if(index < 0 || index >=size) {return null;
}else if(index == 0) {returnremoveFirst();
}else if(index == size - 1) {returnremoveLast();
}else{
Node pervious =head;for(int i = 1; i < index; i++) {
pervious=pervious.next;
}
Node current =pervious.next;
pervious.next=current.next;
size--;returncurrent.element;
}
}
@OverridepublicString toString() {
StringBuilder result= new StringBuilder("[");
Node current =head;for(int i = 0; i < size; i++) {
result.append(current.element);
current=current.next;if(current != null) {
result.append(",");
}
}return result.toString() + "]";
}
@Overridepublic voidclear() {//TODO Auto-generated method stub
size = 0;
head= tail = null;
}
@Overridepublic booleancontains(E e) {//TODO Auto-generated method stub
Node current =head;if(current.element.equals(e)) {return true;
}for(int i = 1; i < size; i++) {
current=current.next;if(current.element.equals(e)) {return true;
}
}return false;
}
@Overridepublic E get(intindex) {//TODO Auto-generated method stub
checkIndex(index);if(index == 0) {returnhead.element;
}else if(index == size - 1) {returntail.element;
}else{
Node current =head;for(int i = 1; i < size - 1; i++) {
current=current.next;if(i ==index) {returncurrent.element;
}
}
}return null;
}
@Overridepublic intindexOf(E e) {//TODO Auto-generated method stub
if(e.equals(head.element)) {return 0;
}else{
Node current =head;for(int i = 1; i < size; i++) {
current=current.next;if(e.equals(current.element)) {return 1;
}
}
}return -1;
}
@Overridepublic intlastIndexOf(E e) {//TODO Auto-generated method stub
if(e.equals(tail.element)) {return size - 1;
}else{
Node current =head;int result = -1;for(int i = 1; i < size; i++) {
current=current.next;if(e.equals(current.element)) {
result=i;
}
}returnresult;
}
}
@Overridepublic E set(intindex, E e) {//TODO Auto-generated method stub
checkIndex(index);
E result= null;if(index == 0) {
result=head.element;
head.element=e;returnresult;
}else if(index == size - 1) {
result=tail.element;
tail.element=e;returnresult;
}else{
Node current =head;for(int i = 1; i < size - 1; i++) {
current=current.next;if(index ==i) {
result=current.element;
current.element=e;break;
}
}returnresult;
}
}
@Overridepublic Iteratoriterator() {//TODO Auto-generated method stub
return newLinkedListIterator();
}private class LinkedListIterator implements Iterator{private Node current =head;
@Overridepublic booleanhasNext() {//TODO Auto-generated method stub
return current != null;
}
@OverridepublicE next() {//TODO Auto-generated method stub
E e =current.element;
current=current.next;returne;
}
}
}