我需要编写一个Java程序(LinkedList类),该程序应该不使用import List即可完成所有工作,但我已经尝试过,但是我不确定它们是否起作用。

有人可以帮帮我吗?特别是getFirstElement和getLastElement方法。

这是我的课程:

package main.java.a3;

public interface List<E> {
    public void add(E e);
    public void add(int index, E e);
    public int size();
    public E get(int index);
    public boolean isEmpty();
}




package main.java.a3;

    import java.util.NoSuchElementException;

    public class LinkedList<E> implements List<E>{

        private ListNode head;

        @Override
        public void add(E e) {
            if(e == null){
                throw new NullPointerException("Element was null");
            }
            if(head == null){
                head = new ListNode(e,null);
            }else{
                    ListNode temp = head;
                    while(temp.next!=null){
                        temp=temp.next;
                    }
                    temp.setNext(new ListNode(e,null));
                }
        }

        @Override
        public void add(int index, E e) {
            if(e == null) {
                throw new NullPointerException("Element was null!");
            }
            else if(index<0){
                throw new IndexOutOfBoundsException("Index was negative");
            }
            else if(index>=size() + 1){
                throw new IndexOutOfBoundsException("Index was bigger than size");
            } else {
                ListNode temp = head;
                while(temp.next != null) {
                    temp = temp.next;
                }
                temp.setNext(new ListNode(e, null));
            }

        }

        @Override
        public int size() {
            int size = 0;
            ListNode temp = head;
            while(temp != null) {
                size++;
                temp = temp.getNext();
            }
               return size;
        }

        @Override
        public E get(int index) {
            if(index<0){
                throw new IndexOutOfBoundsException("Index was negative");
            }
            if(index>=size()){
                throw new IndexOutOfBoundsException("Index was bigger than size");
            }
            ListNode temp = head;
            for (int i = 0; i<index;i++){
                temp = temp.next;
            }
            return temp.data;
        }

        @Override
        public boolean isEmpty() {
            if(head == null) {
                return true;
            } else {
                return false;
            }
        }

                // innere Klasse
                        private class ListNode{
                            E data;
                            ListNode next;

                            public ListNode(E data, ListNode next){
                                setData(data);
                                setNext(next);
                            }

                            public void setData(E data){
                                this.data = data;
                            }

                            public void setNext(ListNode next){
                                this.next = next;
                            }

                            public E getData() {
                                return data;
                            }

                            public ListNode getNext() {
                                return next;
                            }

                        }
                // innere Klasse

        public String toString() {
                return head.toString();
        }

        public void addFirst(E elem) {
            if(elem == null) {
                throw new NullPointerException("Element was null!");
            } else {
                ListNode temp = new ListNode(elem, head);
                if(head != null) {
                    temp.setNext(head);
                }
                head = temp;
            }
        }

        public void addLast(E elem) {
            if(elem == null) {
                throw new NullPointerException("Element was null!");
            } else {
                ListNode tail = new ListNode(elem, null);
                while(head != null) {
                    tail.getNext();
                    if(tail.getNext() == null) {
                        tail.setNext(head);
                    }
                }
            }
        }

        public E getFirst() {
            if(head == null) {
                throw new NoSuchElementException("Element was null!");
            }else {
                return (E) head;
            }

        }

        public E getLast(){
            E elem = null;
            ListNode tail = new ListNode(elem, null);
            if(tail == null) {
                throw new NoSuchElementException("Element was null!");
            }
            return (E) tail;
        }


    }

最佳答案

getFirstElement()和getLastElement()

/ *假设链接列表中的元素存储在Node类的变量数据中,* /

public E getFirst() {
            if(head == null) {
                throw new NoSuchElementException("Element was null!");
            }else {
                return (E) head.data;
            }

        }

        public E getLast(){
            if(head == null) {
                throw new NoSuchElementException("Element was null!");
            }
            else{
                for(Node iterate=head; iterate!=null; iterate=iterate.next){
                    return (E) iterate.data;
                }

            }
        }

08-25 18:32