java - LinkedList getFirstElement 和 getLastElement 方法

标签 java interface linked-list inner-classes

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

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

这是我的类(class):

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 类的变量data */

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;
                }

            }
        }

关于java - LinkedList getFirstElement 和 getLastElement 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724018/

相关文章:

java - 在 java 中,使用 "new"初始化数组有什么好处?

java - 打印 Java 数组的最简单方法是什么?

unit-testing - 我可以使用嵌套界面模拟库代码吗?

delphi - 当没有更多的引用时,接口(interface)实现表单会自行释放吗?

java - 通过 HTTP header 发送附加信息

java - 日期和时间格式

java - 内部类默认为 "sensible"接口(interface)?

c - 二叉树在构建树时丢失节点

c - 这个 C 递归结构会正确解除分配吗?

c++ - 单链表的地址是否总是有序的