zoukankan      html  css  js  c++  java
  • 链表的实现

    
    
    package yaobo.stack;
    
    public class LinkList {
        public Node head;
        public Node current;
    
        // 方法:向链表中添加数据
        public void add(int data) {
            // 判断链表为空的时候
            if (head == null) {// 如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点
                head = new Node(data);
                current = head;
            } else {
                // 创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)
                current.next = new Node(data);
                // 把链表的当前索引向后移动一位
                current = current.next; // 此步操作完成之后,current结点指向新添加的那个结点
            }
        }
    
        // 定位索引的位置
        public void posIndex(int index) {
            if (head == null) {
                return;
            }
    
            if (index == -1) {
                return;
            }
    
            current = head;
            int j = 1;
            while (current != null && j < index) {
                current = current.next;
                j++;
            }
    
        }
    
        // 删除链表元素
        public void delete(int index) {
            if (head == null) {
                try {
                    throw new Exception("链表为空。。。");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            posIndex(index-1);
            current.setNext(current.next.next);
    
        }
    
        // 方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历
        public void print(Node node) {
            if (node == null) {
                return;
            }
    
            current = node;
            while (current != null) {
                System.out.print(current.data + " ");
                current = current.next;
            }
        }
    
        class Node {
            // 注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。
            int data; // 数据域
            Node next;// 指针域
    
            public Node(int data) {
                this.data = data;
    
            }
    
            public int getData() {
                return data;
            }
    
            public void setData(int data) {
                this.data = data;
            }
    
            public Node getNext() {
                return next;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
    
        }
    
        public static void main(String[] args) {
            LinkList list = new LinkList();
            // 向LinkList中添加数据
            for (int i = 0; i < 10; i++) {
                list.add(i);
            }
    
            list.print(list.head);// 从head节点开始遍历输出
            System.out.println("
    " + "删除元素后:");
            list.delete(4);
            list.print(list.head);
    
        }
    
    }
    
    
    
     

    使用内部类的最大好处是可以和外部类进行私有操作的互相访问。

    注:内部类访问的特点是:内部类可以直接访问外部类的成员,包括私有;外部类要访问内部类的成员,必须先创建对象。

    为了方便添加和遍历的操作,在LinkList类中添加一个成员变量current,用来表示当前节点的索引(03行)。

    这里面的遍历链表的方法(20行)中,参数node表示从node节点开始遍历,不一定要从head节点遍历。

    转:http://www.jb51.net/article/71885.htm

  • 相关阅读:
    python之socket编程
    python之异常处理
    面向对象进阶
    openstack的网络配置
    VLAN,GRE,VXLAN
    三节点搭建openstack-Mitaka版本
    矩阵转换
    判断区域相交的方法
    Centos 6.5 Multipath 初始配置
    CentOS开机自动运行程序的脚本
  • 原文地址:https://www.cnblogs.com/yaobolove/p/6418250.html
Copyright © 2011-2022 走看看