Java数据结构之链表增删改查与递归算法

链表 (Linked list )是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到是下一个节点的指针(Pointer
在链表数据结构中,我们需要使用到递归算法。 
递归算法是一种直接或者间接地调用自身算法的过程。
在计算机编写程序中,递归算法对解决一大类问题是十分有效的,
它往往使算法的描述简洁而且易于理解。

下面先来一个递归算法实现阶乘的示例:
public class Demo {
    public static void main(String[] args) {
      int result=Factorial(5);
        System.out.println(result);
    }
    public static int Factorial(int num)
    {
        if(num==1)
        {
            return 1;
        }
        return num*Factorial(num-1);
    }
}

(注意:1、递归必须要有出口
    2、递归内存消耗大,容易发生内存溢出
    3、层次调用越多,越危险)

链表与数组:都是线性数据结构
    数组适合查找,遍历,固定长度
    链表适合插入,删除,不宜过长,否则会导致遍历性能下降 

在链表中对元素进行增删改查源代码

import org.w3c.dom.ls.LSOutput;

public class Demo {
    public static void main(String[] args) {
        Root r=new Root();
        r.add(5);
        r.add(3);
        r.add(50);
        r.add(330);
        r.add(10);
        r.print();
        System.out.println();//换行操作,下同
        r.update(3,555);
        r.print();
        r.insert(2,999);
        System.out.println();
        r.print();
        System.out.println();
        System.out.println(r.find(999));
    }

}
class Root
{

    private Node root;
    private int currentIndex = 0;//节点的序号,每次操作从0开始
    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
    //向链表中添加元素
    public void add(int data)
    {
        if(root==null){
            root=new Node(data);
        }else{
            root.addNode(data);
        }

    }
    //打印链表中的元素值
    public void print()
    {
        if (root!=null)
        {
            System.out.print("-->" +root.getData());
            root.printNode();
        }
    }
    //删除链表中的元素
    public void del(int data)
    {
        if (root==null)return;
        if(root.getData()==data)
        {
            root.nr(root);
        }
        else{
            root.delNode(data);
        }
    }
    //更新链表中的元素值
    public void update(int index,int data)
    {
        if (root==null)
        {
            return;
        }
        if (index==0)
        {
            root.setData(data);
        }
        else
        {
            root.updateNode(index,data,0);
        }
    }
    public void insert(int index,int data)
    {
        if (root==null){
            return;
        }
        if (index==currentIndex)
        {
           Node n1=new Node(data);
           n1.next=root;
           root=n1;
        }
        else
        {
            root.insertNode(index,data,0);
        }
    }
    //查找链表中的元素值
        public boolean find(int data){
            if(root==null)return false;
            if(root.getData()==data){
                return true;
            }else{
                return root.findNode(data);
            }
        }
}
class Node
{
private int data;
public Node next;
Node(int data){
    this.data=data;
}

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
    public void nr(Node node)
    {
        node=node.next;
    }
    public void addNode(int data)
{
    if(this.next==null)
    {
       this.next= new Node(data);
    }
    else{
        this.next.addNode(data);
    }
}
public void printNode()
{
    if (this.next!=null)
    {
        System.out.print("-->" + this.next.data);
        this.next.printNode();
    }
}
public void delNode(int data)
{
    if (this.next.getData()==data)
    {
        this.next=this.next.next;
    }
    else{
        this.next.delNode(data);
    }
}
    public void updateNode(int index,int data,int count) {

        if (this.next != null) {
            if (count != index) {  count++;
                this.next.updateNode(index, data,count);
            } else {
                this.setData(data);
            }
        }
    }
    public void insertNode(int index,int data,int count)
    { count++;
        if (this.next != null) {
            if (count != index) {
                this.next.insertNode(index, data,count);
            } else {
                Node n1=new Node(data);
                n1.next=this.next;
                this.next=n1;
            }
        }
    }
    public boolean findNode(int data)
    {
     if (this.next!=null)
     {
         if (this.getData()==data)
         {
             return true;
         }
         else{
             return this.next.findNode(data);
         }
     }
     return false;
    }
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心态特好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值