zoukankan      html  css  js  c++  java
  • 数据结构与算法--链表

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

    写个例子说明列表的创建和遍历:
    public class LinkList {
        private Node head;
    
        private Node current;
    
        private void add(int data) {
            if(head == null) {
                head = new Node(data, null);
                current = head;
            } else {
                //创建新的结点
                Node node = new Node(data, null);
                //新创建的节点和列表进行关联
                current.next = node;
                //移动当前链表的索引位置
                current = node;
            }
        }
    
        private void 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;
    
            public Node() {
            }
    
            public Node(int data, Node next) {
                super();
                this.data = data;
                this.next = next;
            }
    
        }
    
        public static void 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  
     
     



     



  • 相关阅读:
    VS2005编译mysql5.1.68
    用boost库实现traceroute小工具
    linux内核选项部分翻译
    linux 内核中的锁机制RCU
    先装windows 还是linux ?
    U盘装ubuntu
    编译linux内核3.0
    root密码丢失了怎么办?
    linux配置文件
    新一代linux文件系统btrfs
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/4454862.html
Copyright © 2011-2022 走看看