dianbolan 2021-08-02 16:47 采纳率: 100%
浏览 40
已结题

单链表如何用java语言实现

如题,要完整的代码,包含最基本的链表是否为空,链表长度,在头或尾部添加元素,在头部移除元素(尾部不要求)

  • 写回答

2条回答 默认 最新

  • qq_36941205 2021-08-02 16:49
    关注

    /**

    • 单链表实现

    • @author caasi

    • @since 2021-07-28 16:58:20

    • /
      public class SinglyLinkedList implements Cloneable{
      /**

      • 需要有的最基本的方法
      • size() 返回链表的长度
      • isEmpty() 判断链表是否为空
      • addFirst() 在链表头部添加元素
      • addLast() 在链表尾部添加元素
      • first() 返回链表的第一个元素,如果链表为空,则返回null
      • last() 返回链表的最后一个元素,如果链表为空,则返回null
      • removeFirst() 删除并返回链表的第一个元素,如果链表为空,则返回null
      • /

      /**

      • 节点内部类

      • @param

      • /
        private class Node{
        private E element;
        private Node next;
        public Node(E e, Node n){

          element = e;
          next = n;
        

        }

        public E getElement(){

          return element;
        

        }

        public Node getNext() {

          return next;
        

        }

        public void setNext(Node next) {

          this.next = next;
        

        }
        }

      //头节点
      private Node head;
      //尾节点
      private Node tail;
      //链表长度
      private int size;

      public int size(){

        return size;
      

      }

      public boolean isEmpty(){

        return size == 0;
      

      }

      public E first(){

        if(isEmpty()) return null;
        return head.getElement();
      

      }

      public E last(){

        if(isEmpty()) return null;
        return tail.getElement();
      

      }

      public E removeFirst(){

        if(isEmpty()) return null;
        E e = head.getElement();
        head = head.getNext();
        size -- ;
        if(isEmpty()) tail = null;
        return e;
      

      }

      public void addFirst(E e){

        head = new Node<>(e, head);
        if(isEmpty()) tail = head;
        size ++;
      

      }

      public void addLast(E e){

        Node<E> newNode = new Node<>(e, null);
        if(isEmpty()){
            head = newNode;
        } else {
            tail.setNext(newNode);
        }
        size ++;
        tail = newNode;
      

      }

      public void add(E e){

        addLast(e);
      

      }

      public void print(){

        if(isEmpty()) return;
        Node<E> node = head;
        while(node != null){
            System.out.println(node.getElement());
            node = node.getNext();
        }
      

      }

      @Override
      public boolean equals(Object obj) {

        if (obj == null) return false;
        if(getClass() != obj.getClass()) return false;
        SinglyLinkedList o = (SinglyLinkedList) obj;
        if(size != o.size) return false;
        Node walkA = head;
        Node walkB = o.head;
        while (walkA != null){
            if(!walkA.getElement().equals(walkB.getElement())){
                return false;
            }
            walkA = walkA.getNext();
            walkB = walkB.getNext();
        }
        return true;
      

      }

      @Override
      protected Object clone() throws CloneNotSupportedException {

        SinglyLinkedList other = (SinglyLinkedList) super.clone();
        if (size > 0){
            other.head = new Node<E>(head.getElement(), null);
            Node<E> walk = head.getNext();
            Node<E> otherTail = other.head;
            while (walk != null){
                Node<E> newest = new Node<>(walk.getElement(), null);
                otherTail.setNext(newest);
                otherTail = newest;
                walk = walk.getNext();
            }
        }
        return other;
      

      }
      }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月2日
  • 已采纳回答 8月2日
  • 创建了问题 8月2日

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?