qq_树_11
2015-10-13 20:53
为什么代码提交没错,但是运行点击按钮没有效果??
public class MyLinkedList<E> extends AbstractList<E>{
private Node<E> head, tail;
private int size = 0;
public MyLinkedList() {
}
public E getFirst() {
if (size == 0) {
return null;
}
else {
return head.element;
}
}
public E getLast() {
if (size == 0) {
return null;
}
else {
return tail.element;
}
}
public void addFirst(E e) {
Node<E> newNode = new Node<E>(e); // Create a new Node
newNode.next = head;// link the new node with the head
head = newNode; // head points to the new node
size++;
if (tail == null) // the new node is the only node in list
tail = head;
}
public void addLast(E e) {
Node<E> newNode = new Node<E>(e); // Create a new Node for e
if (tail == null) {
head = tail = newNode; // The only node in list
}
else {
tail.next = newNode; // link the new with the last node
tail = tail.next; // tail now points to the last node
}
size++; // Increase size
}
public void add(int index, E e) {
if (index == 0) addFirst(e);// Insert first
else if (index >= size) addLast(e); //Insert last
else { // Insert in the middle
Node<E> current = head;
for (int i = 1; i < index; i++)
current = current.next;
Node<E> temp = current.next;
current.next = new Node<E>(e);
(current.next).next = temp;
size++;
}
}
public E removeFirst() {
if (size == 0)
return null;
else if (size == 1)
{
Node<E> temp = head;
head = tail = null;
size = 0;
return temp.element;
}
else
{
Node<E> temp = head;
head = head.next;
size--;
return temp.element;
}
}
public E removeLast() {
if (size == 0)
return null; // Nothing to remove
else if (size == 1) // only one element in the list
{
Node<E> temp = head;
head = tail = null; // list becomes empty
size = 0;
return temp.element;
}
else
{
Node<E> current = head;
for (int j = 0; j < size - 2; j++)
current = current.next;
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
public E remove(int index) {
if (index < 0 || index >= size)
return null; //Out of range
else if (index == 0)
return removeFirst(); // Remove first
else if (index == size - 1)
return removeLast(); // Remove last
else {
Node<E> previous = head;
for (int i = 1; i <index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
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();
}
public void clear() {
head = tail = null;
}
public boolean contains(Object o) {
System.out.println("Implementation left as an exercise");
return true;
}
public E get(int index) {
System.out.println("Implementation left as an exercise");
return null;
}
public int indexOf(Object o) {
System.out.println("Implementation left as an exercise");
return 0;
}
public int lastIndexOf(Object o) {
System.out.println("Implementation left as an exercise");
return 0;
}
public E set(int index, E e) {
System.out.println("Implementation left as an exercise");
return null;
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
}
你代码怎么写的?
JavaScript入门篇
739817 学习 · 9566 问题
相似问题