码迷,mamicode.com
首页 > 编程语言 > 详细

Java面向对象_数据结构之链表

时间:2015-12-25 18:49:47      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

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

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

 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 }

 

Java面向对象_数据结构之链表

标签:

原文地址:http://www.cnblogs.com/shenhainixin/p/5076611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!