package com.github.Ven13.coding2017.basic;
//MyLinkedList功能类:
public class MyLinkedList extends MyAbstractList {
private Node first, last;
private static class Node {
Object element;
Node next;
public Node(Object element) {
this.element = element;
}
}
public MyLinkedList(Object[] objects) {
super(objects);
}
public MyLinkedList() {
}
public Object getFirst() {
if (size == 0) {
return null;
} else {
return first.element;
}
}
public Object getLast() {
if (size == 0) {
return null;
} else {
return last.element;
}
}
public void addFirst(Object o) {
Node newNode = new Node(o);
newNode.next = first;
first = newNode;
size++;
if (last == null) {
last = first;
}
}
public void addLast(Object o) {
if (last == null) {
last = first = new Node(o);
} else {
Node newNode = new Node(o);
last.next = newNode;
last = last.next;
}
size++;
}
public void add(int index, Object o) {
if (index == 0) {
addFirst(o);
} else {
if (index >= size) {
addLast(o);
} else {
Node current = first;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node temp1 = current.next;
Node temp2 = new Node(o);
current.next = temp2;
temp2.next = temp1;
size++;
}
}
}
public Object set(int index, Object o) {
Node temp = new Node(o);
if (index == 0) {
first = temp;
} else {
Node current = first;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node temp1 = current.next;
current.next = temp;
temp.next = temp1;
}
return o;
}
public void clear() {
first = last = null;
}
public boolean contains(Object o) {
Node current = first;
while (current != null) {
if (current.element.equals(o)) {
return true;
}
current = current.next;
}
return false;
}
public Object get(int index) {
Node current = first;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.element;
}
public int indexOf(Object o) {
Node current = first;
int index = -1;
int i = 0;
while (current != null) {
if (current.element.equals(o)) {
return i;
}
current = current.next;
i++;
}
return index;
}
public int lastIndexOf(Object o) {
Node current = first;
int index = -1;
int i = 0;
while (current != null) {
if (current.element.equals(o)) {
index = i;
}
current = current.next;
i++;
}
return index;
}
public boolean remove(Object o) {
return remove(indexOf(o));
}
public boolean remove(int index) {
if (index == 0) {
first.next = first;
return true;
} else {
if (index >= size) {
return false;
} else {
Node current = first;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node temp = current.next;
current.next = temp.next;
size--;
return true;
}
}
}
public String toString() {
StringBuffer result = new StringBuffer("[");
Node current = first;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", ");
} else {
result.append("]");
}
}
return result.toString();
}
}
// MyList接口类:
public interface MyList {
public void add(Object o);
public void add(int index, Object o);
public void clear();
public boolean contains(Object o);
public Object get(int index);
public int indexOf(Object o);
public boolean isEmpty();
public int lastIndexOf(Object o);
public boolean remove(Object o);
public boolean remove(int index);
public Object set(int index, Object o);
public int size();
}
// MyAbstractList抽象类:
public abstract class MyAbstractList implements List {
protected int size;
protected MyAbstractList() {
}
protected MyAbstractList(Object[] objects) {
for (int i = 0; i < objects.length; i++) {
this.add(objects[i]);
}
}
public void add(Object o) {
add(size, o);
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}