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日

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启