課程
/前端開發(fā)
/JavaScript
/JavaScript入門篇
為什么代碼提交沒錯,但是運行點擊按鈕沒有效果??
2015-10-13
源自:JavaScript入門篇 4-1
正在回答
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做為一名Web工程師的必備技術(shù),本教程讓您快速入門
6 回答代碼無效果
1 回答代碼效果無法顯示。
1 回答為什么這段代碼運行沒有效果
4 回答代碼運行沒結(jié)果
3 回答代碼運行結(jié)果不理解
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-10-14
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;
?? ?}
}
2015-10-14
你代碼怎么寫的?