java实现链表

关于链表的基本操作在数据结构(c语言实现)哪里已经做了详细的说明,现在这里我将采用java实现单链表
就不进行详细的说明了

0.编写一个Node类来充当结点的模型。

/*
     * 链表中的节点,data代表节点的内容,next指向下一个节点的引用
     * */
    private class Node{
        private int data;
        private Node next;
        public Node(int data) {
            
         this.data = data;
        }
    }

1.编写Linklist

public class Linkedlist {
    private static int length;
    private Node head; //头节点
    public Linkedlist() {
        length=0;
        head=null;
    }

2.节点的插入这里我采用头插法和尾插法两种操作

头插法

/*采用头插法插入节点*/
   public Object addHead(int obj) {
       Node newHead=new Node(obj);
       if(length==0) {
           head=newHead;
       }else {
           newHead.next=head;
           head=newHead;
       }
       length++;
       
    return obj;
       
   }
   

尾插法

//采用尾插法插入节点
   public void addNode(int obj) {
       Node newNode=new Node(obj);
       if(head==null) {
           head=newNode;
           return;
       }
       Node temp=head;
       while(temp.next!=null)
       {
           temp=temp.next; //后移
       }
       temp.next=newNode;
       length++;
    
   }

3.节点的删除

1在表头删除元素

///在表头删除元素
   public int delHead() {
      int obj=head.data;
       head=head.next;
       length--;  //长度减一
    return obj;   
   }

2删除指定元素,删除第index个节点

  public boolean delNode(int index){ 
    if(index<1||index>length) {
     return false; 
    }
    if(index==1) {
        head=head.next;
        length--;
        return true;
    }
    int i=2;
    Node previous=head;
    Node current=previous.next;
    while(current!=null) {
        if(i==index) {
        previous.next=current.next;
        length--;
        return true;
        }
        previous=current;
        current=current.next;
        i++;
    }
     return true;
}

4.链表的遍历操作

 public void printlist() {
      if(length>0) {
          Node temp=head;
          int tempsize=length;
          if(tempsize==1) {
              System.out.print("["+temp.data+"]");
              return;
          }
          while(tempsize>0) {
              if(temp.equals(head)) {
                  System.out.print("["+temp.data+"]");
              }else if(temp.next==null){
                  System.out.print(temp.data+"]");
              }else {
                  System.out.print(temp.data+"->");
              }
              temp=temp.next;
              tempsize--;
          }
      }
  }

测试代码

public class LinkedlistTest{
    public static void main(String[] args) {
      Linkedlist list = new Linkedlist();
      list.addHead(5);
      list.addHead(3);
      list.addHead(1);
      list.addNode(2);
      list.addNode(55);
      list.addNode(36);
      System.out.println("head.data:" + list.head.data);
      list.printlist();
      System.out.println("链表长度是"+length);
      list.delNode(4);
      System.out.println("删除节点4之后");
      list.printlist();
      System.out.println("删除后的链表的长度是"+length);
  }
}

输出结果

这里前三个节点我采用的是头插法插入,后三个节点采用的是尾插法插入
代码写的很粗糙,如有不足之处还请指出,禁止转载。

原文地址:https://www.cnblogs.com/xaimicom/p/9038617.html