美文网首页
单链表操作

单链表操作

作者: 刘大平 | 来源:发表于2018-10-11 16:58 被阅读0次

    简单的单链表操作

    
    public class ListNote {
    
    T data;
    
      ListNotenext;
    
      public ListNote(T value) {
    
    this.data = value;
    
      }
    
    }
    

    下面是操作类

    public class ListNoteAction {
    
    /**
    
      * 添加一个新的节点
    
      * @param head 当前头结点
    
      * @param newHead
    
      */
    
      public static void addHead(ListNote head, ListNote newHead) {
    
    ListNote oldHead = head;
    
        head = newHead;
    
        head.next = oldHead;
    
      }
    
    /**
    
      * 在当前尾节点添加一个新的尾节点
    
      * @param tail 当前尾节点
    
      * @param newTail
    
      */
    
      public static void tailAdd(ListNote tail, ListNote newTail) {
    
    ListNote oldTail = tail;
    
        tail = newTail;
    
        oldTail.next = tail;
    
        tail.next =null;
    
      }
    
    /**
    
      *  在 midNote 后面插入一个新的节点
    
      * @param midNote
    
      * @param newNote
    
      */
    
      public static void insert(ListNote midNote, ListNote newNote) {
    
    ListNote odlNext = midNote.next;
    
        midNote.next = newNote;
    
        newNote.next = odlNext;
    
      }
    
    /**
    
      * 遍历当前链表
    
      * @param head
    
      */
    
      public static void listAll(ListNote head) {
    
    if (Objects.isNull(head)) {
    
    return;
    
        }
    
    ListNote tempHead = head;
    
        while (tempHead !=null) {
    
    System.out.print(tempHead.data +" ");
    
          tempHead = tempHead.next;
    
        }
    
    }
    
    /**
    
      * 根据index 查询节点
    
      * @param head
    
      * @param index
    
      * @return
    
      */
    
      public static ListNoteindex(ListNote head, int index) {
    
    if (Objects.isNull(head)) {
    
    return null;
    
        }
    
    int j =0;
    
        while (Objects.nonNull(head) && j < index) {
    
    head = head.next;
    
          j++;
    
        }
    
    System.out.println();
    
        System.out.println(head.data);
    
        return head;
    
      }
    
    /**
    
      * 删除当前这个节点的后续节点
    
      * @param delAfterThisNode
    
      */
    
      public static void del(ListNote delAfterThisNode){
    
    ListNote currentNext = delAfterThisNode.next;
    
        ListNote currentNextNext = currentNext.next;
    
        delAfterThisNode.next = currentNextNext;
    
        currentNext.next =null;
    
      }
    
    public static void main(String[] args) {
    
    ListNote node1 =new ListNote<>(1);
    
        ListNote node2 =new ListNote<>(2);
    
        ListNote node3 =new ListNote<>(3);
    
        ListNote node4 =new ListNote<>(4);
    
        node1.next = node2;
    
        node2.next = node3;
    
        node3.next = node4;
    
        node4.next =null;
    
        System.out.println(node1);
    
        ListNote node0 =new ListNote<>(0);
    
        addHead(node1, node0);
    
        ListNote node5 =new ListNote<>(5);
    
        tailAdd(node4, node5);
    
        ListNotenode2_2=new ListNote<>(22);
    
        insert(node2, node2_2);
    
        listAll(node0);
    
        index(node0, 3);
    
        del(node2);
    
        listAll(node0);
    
      }
    
    }
    

    相关文章

      网友评论

          本文标题:单链表操作

          本文链接:https://www.haomeiwen.com/subject/cuobaftx.html