zoukankan      html  css  js  c++  java
  • Java面向对象_数据结构之链表

    链表:是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里村到下一个节点的指针。

             在链表数据结构中,需要使用到递归算法。递归算法是一种直接或间接地调用自身算法的过程。

     1 public class Practice14 {
     2     
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         jiecheng(5);
     6         System.out.println(jiecheng2(5));
     7     }
     8     //递归写法(很简单)
     9     //注意递归必须要有出口,递归过多会造成内存栈溢出
    10     public static int jiecheng2(int num){
    11         if(num==1)return 1;
    12         return num*jiecheng2(num-1);
    13     }
    14     //求阶乘(普通写法)
    15     public static void jiecheng(int num){
    16         
    17         int sum=num;
    18         int i=num-1;
    19         do{
    20             sum=sum*i;    
    21             i--;
    22         }while(i>1);
    23         System.out.println(sum);
    24     }
    25     
    26 }

    链表数据结构用于适合频繁进行添加、插入、删除操作,举个例子:

     1 //链表数据结构用于适合频繁进行添加、插入、删除操作
     2 public class Practice14 {
     3     
     4     public static void main(String[] args) {
     5         // TODO Auto-generated method stub
     6         NodeManager nm=new NodeManager();
     7         nm.addNode("1");
     8         nm.addNode("2");
     9         nm.addNode("3");
    10         nm.addNode("4");
    11         nm.printNode();
    12         nm.delNode("3");
    13         nm.printNode();
    14     }
    15     
    16 }
    17 //节点管理类
    18 class NodeManager{
    19     private Node root;//根节点
    20     public void addNode(String name){
    21         if(root==null){
    22             root=new Node(name);
    23         }else{
    24             root.add(name);
    25         }
    26     }
    27     
    28     public void delNode(String name){
    29         if(root.getName().equals(name)){
    30             root=root.next;
    31         }else{
    32             root.del(name);
    33         }
    34         
    35     }
    36     
    37     public void printNode(){
    38         if(root!=null){
    39             System.out.print(root.getName()+"->");
    40             root.print();
    41             System.out.println();
    42         }
    43         
    44     }
    45     //每个节点对象
    46     class Node{
    47         private String name;
    48         private Node next;//表示当前节点的下一个节点
    49         public Node(String name){
    50             this.name=name;
    51         }
    52         
    53         public String getName() {
    54             return name;
    55         }
    56         public void setName(String name) {
    57             this.name = name;
    58         }
    59         public Node getNext() {
    60             return next;
    61         }
    62         public void setNext(Node next) {
    63             this.next = next;
    64         }
    65         //添加节点
    66         public void add(String name){
    67             if(this.next==null){
    68                 this.next=new Node(name);
    69             }else{
    70             this.next.add(name); //递归
    71             }
    72         }
    73         //删除节点
    74         public void del(String name){
    75             if(this.next!=null){
    76                 if(this.next.name.equals(name)){
    77                     this.next=this.next.next;
    78                 }else{
    79                     this.next.del(name);
    80                 }
    81             }
    82         }
    83         //输出节点
    84         public void print(){
    85             if(this.next!=null){
    86                 System.out.print(this.next.getName()+"->");
    87                 this.next.print();
    88             }
    89         }
    90     }
    91 }
  • 相关阅读:
    导出表格,导出表格为excel,导出表格带样式,怎么导出表格数据为excel并且带样式呢
    webpack打包文件 可以npm cdn使用
    Webpack的externals的使用
    如何在微信小程序中使用iconfont
    如何发布和取消发布 NPM 包?
    js中数组对象去重的方法
    小程序列表性能优化
    wepy全局拦截器
    js中prototype与__proto__的关系详解
    JavaScript中本地对象、内置对象和宿主对象
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/5076611.html
Copyright © 2011-2022 走看看