数据结构--单链表-循环链表-双向循环链表--单链表中删除节点--插入节点到单向链表 的理解和以及代码实现

1.单链表:

何为链表?

通过地址的方式,找到数据。

比如 到银行办理业务,业务员根据票号,来找到下一个人进行办理业务。

同样地,链表的意思是,通过一个地址,找到数据。

1.1数据内容为int型的链表
public class LinkList{
    public static void main(String[] args){
 
        Node n1=new Node(9);
        Node n2=new Node(8);
        Node n3=new Node(7);
        //追加节点
        n1.append(n2);
        n2.append(n3);
        System.out.println(n1.next().next().getData());//7

    }

}
//构建一个节点类
class Node{
    //节点内容
    int data;
    //下一个节点
    Node next;
    public Node(int value){
        this.data=value;
    }

    //为当前节点追加下一个节点
    public void append(Node node){
        //将当前节点赋值给currentNode
        Node currentNode=this;
        //循环向后找
        while(true){
            
            Node nextNode=currentNode.next;
            //如果下一个节点为空,则退出循环
            if(nextNode==null){
                break;
            }
            //将下一个节点赋给当前节点
            currentNode=nextNode;
        }
        //因为退出循环的时候,是当下一节点为空的时候,所以把需要返回的节点追加为当前节点的下一节点
        currentNode.next=node;
    }
    //返回下一个节点
    public Node next(){
        return this.next;
    }
    //获得节点内容
    public int getData(){
        return this.data;
    }

}

1.2数据内容为数组的链表
import java.util.Arrays;
import java.util.Set;

//import com.sun.corba.se.impl.orbutil.graph.Node;
public class LinkList{
    public static void main(String[] args){
        Node n1=new Node(new int[]{1,2,4,5,6});        
        Node n2=new Node(new int[]{7,8,9,10}); 
        Node n3=new Node(new int[]{11,12,13,14});
        n1.append(n2);
        n2.append(n3);
        //System.out.println(n1.next().getdata());
        n1.next().getdata();//7,8,9,10  
        }   
        
    }

 class Node{
    //节点内容
    int[] data;
    //下一个节点
    Node next;
    public Node(int[] value){
        this.data=value;
    }
    //追加节点
    public void append(Node node){
        Node currentNode=this;
        //往后寻找
        while(true){
            Node nextNode=currentNode.next;
            if(nextNode==null){
                break;
            }
            // 追加下一节点为当前节点;
            currentNode=nextNode;
        }
            //由于当前节点的后一节点为空,因此将node放到当前节点的下一节点       
            currentNode.next=node;
    }
    //返回下一个节点
    public Node next(){
        return this.next;
    }
    public void getdata(){
        for(int i=0;i<data.length;i++){
            System.out.print(data[i]+"\t");
        }
    }
    

}
2.删除插入单链表中的节点
删除节点:
public void remove(){
        //取出下下节点
        Node newNext=next.next;
        //将下下节点  赋值给当前节点的下一节点
        this.next=newNext;

思路:

取出当前节点的下下节点,将下下节点赋值给 当前节点的下一节点。那么原来 当前节点的下一节点就被删除了。

插入节点:

我们插入节点只能插入当前节点的下一节点,而不能直接插入给当前节点。

思路:1.先将当前节点下一节点取出 2.再将需要插入的节点赋值给当前节点的下一节点3.把之前取出的节点作为新节点的下一节点

 //插入节点
    public void add(node){
        //取出下一节点,作为下下节点
        Node nextNode=this.next;
        //将需要插入的节点,作为当前节点的下一节点
        this.next=node;
        //将之前的下下节点作为新节点的下一节点
        node.next=newNode;
    }
3.循环链表

之前的单向链表是通过一个节点,找下一个节点,但是我们却不能通过其他节点来找第一个节点,那么循环链表的意思就是通过最后一个节点,来寻找到第一个节点。整个节点就串联起来了。

在这里插入图片描述

整个循环链表和单链很相似。只需要将下一节点变为当前节点

import java.util.Arrays;
import java.util.Set;
/**循环链表 */

public class LinkList2{
    public static void main(String[] args){
        LoopNode n1=new LoopNode(1);        
        LoopNode n2=new LoopNode(2); 
        LoopNode n3=new LoopNode(3);
        //追加节点
        n1.add(n2);
        n2.add(n3);
        System.out.println(n3.next().getdata());
        
        
       
        
        
        }   
        
    }

 class LoopNode{
    //节点内容
    int data;
    //这里就是与单链表的区别,将下一节点初始化为当前节点
    LoopNode next=this;
    public LoopNode(int value){
        this.data=value;
    }
   
    //插入节点
    public void add(LoopNode node){
        //取出下一节点,作为下下节点
        LoopNode nextNode=this.next;
        //将需要插入的节点,作为当前节点的下一节点
        this.next=node;
        //将之前的下下节点作为新节点的下一节点
        node.next=nextNode;
    }
    
   
    //返回下一个节点
    public LoopNode next(){
        return this.next;
    }
    public int getdata(){
        return this.data;
    }
    

}
4.双向循环链表

通过每个节点可以寻找到他的上一个和下一个节点。因此叫双向。这个相较于单向循环链表,就是多了一个向前的节点。

核心代码处理解:

在这里插入图片描述

  public class DoubleLoopLinkList{
    public static void main(String[] args){
       DoubleNode n1=new DoubleNode(2);
       DoubleNode n2=new DoubleNode(3);
       DoubleNode n3=new DoubleNode(4);
       n1.after(n2);
       n2.after(n3);
       System.out.println(n1.next().getdata());//3
       System.out.println(n2.pre().getdata());//2

    }
}
class DoubleNode{
    //节点数据
    int data;
    //上一个节点
    DoubleNode pre=this;
    DoubleNode next=this;
    
    public DoubleNode(int value){
        this.data=value;
    }
    /*增加节点   双向链表核心代码*/
    public void after(DoubleNode node){
        //原来节点的下一节点,
        DoubleNode nextNext=next;
        //将新节点作为当前节点的下一节点
        this.next=node;
        //把新节点作为当前节点的前一节点;
        node.pre=this;
        //让原来的节点的下一节点作为当前节点的下一节点
        node.next=nextNext;
       //将原来节点的上一节点作为新节点
        nextNext.pre=node;
    } 
    //下一个节点
    public DoubleNode next(){
         return this.next;
    }
    //上一个节点
    public DoubleNode pre(){
        return this.pre;
    }
    public int getdata(){
        return this.data;
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值