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

算法--链表

时间:2015-04-24 23:56:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

首先链表分三种:
单链表,双链表,循环单链表

技术分享

写个例子说明列表的创建和遍历:
  1. publicclassLinkList{
    privateNode head;
    
    privateNode current;
    
    privatevoid add(int data){
    if(head ==null){
     head =newNode(data,null);
     current = head;
    }else{
    //创建新的结点
    Node node =newNode(data,null);
    //新创建的节点和列表进行关联
    current.next = node;
    //移动当前链表的索引位置 
    current = node;
    }
    }
    
    privatevoid print(Node node){
    if(node ==null){
    return;
    }
    Node current = node;
    while(current !=null){
    System.out.println(current.data);
     current = current.next;
    }
    }
    
    /**
     * @ClassName: Node
     * @Description: TODO(定义Node数据类型)
     */
    classNode{
    int data;
    
    Node next;
    
    publicNode(){
    }
    
    publicNode(int data,Node next){
    super();
    this.data = data;
    this.next = next;
    }
    
    }
    
    publicstaticvoid main(String[] args){
    LinkList list =newLinkList();
    for(int i =0; i <10; i++){
     list.add(i);
    }
     list.print(list.head);
    }
    
    }
    

      

运行结果:
0
1
2
3
4
5
6
7
8
9  
 
 



 



算法--链表

标签:

原文地址:http://www.cnblogs.com/androidsuperman/p/4454862.html

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