Java——数据结构(链表)

链表,可扩展长度,泛型。

public class Link {
    Node header = null; //头结点
    int length;//当前链表长度
    
    class Node {
        Node next = null;
        private Object data;
        
        public Node(Object data) {
            this.data = data;
        }
        
        public Object GetData() {
            return this.data;
        }
    }
    
    /*
     * 增加一个节点
     */
    public void addNode(Object data) {
        Node node = new Node(data);//使用传入的数据新建一个节点
        
        if(this.header == null) {
            this.header = node;
            this.length++;
        }else {
            Node ex = this.header; //创建一个节点用于遍历链表
            //找到最后一个节点
            while(ex.next != null) {
                ex = ex.next;
            }
            ex.next = node;//将最后一个节点指向想要增加的节点
            this.length++;//增加链表长度
        }
    }
    
    /*
     * 查询第i个节点的数据。
     */
    public Object GetLink(int i) {
        if(this.header == null) {
            return 0;
        }
        if(i > this.length) {
            return 0;
        }
        Node ex = this.header;//创建一个节点用于遍历链表
        int j = 1;
        
        //找到第i个节点
        while(j != i) {
            ex = ex.next;
            j++;
        }
        return ex.GetData();
    }
    /*
     * 在第i个节点后插入数据
     */
    public void LinkInsert(int i, Object data) {
        Node newNode = new Node(data);
        Node ex = this.header;//创建一个节点用于遍历链表
        int j = 1;
        while(j != i) {
            ex = ex.next;
            j++;
        }
        Node ex_next = ex.next;
        ex.next = newNode;
        newNode.next = ex_next;
        this.length++;
    }
    
    /*
     * 删除第i个节点
     */
    public void LinkDelete(int i) {
        Node ex = this.header;
        int j = 1;
        //找到要删除的节点的前一个节点
        while(j != i-1) {
            ex = ex.next;
            j++;
        }
        Node ex_next = ex.next;
        ex.next = ex_next.next;
        this.length--;
    }
    
    /*
     * 查找链表数据中等于data的,并返回其节点位置
     */
    public Object LocateElem(Object data) {
        Node ex = this.header;
        for(int j = 1; j < this.length; j++) {
            if(ex.GetData() == data) {
                return j;
            }
            ex = ex.next;
        }
        return 0;
    }
    public static void main(String[] args) {
        Link list = new Link();
        list.addNode(1);
        list.addNode(1.6);
        list.addNode("字");
        System.out.println("当前链表长度:" + list.length);
        System.out.print("当前链表:");
        for(int i = 1; i <= list.length; i++) {
            System.out.print(list.GetLink(i) + ",");
        }
        System.out.println();
        list.LinkInsert(2, "数据");
        System.out.print("当前链表:");
        for(int i = 1; i <= list.length; i++) {
            System.out.print(list.GetLink(i) + ",");
        }
        System.out.println();
        list.LinkDelete(2);
        System.out.print("当前链表:");
        for(int i = 1; i <= list.length; i++) {
            System.out.print(list.GetLink(i) + ",");
        }
        System.out.println();
        System.out.println(list.LocateElem("数据"));
    }

}

main方法输出:

当前链表长度:3
当前链表:1,1.6,字,
当前链表:1,1.6,数据,字,
当前链表:1,数据,字,
2
原文地址:https://www.cnblogs.com/xxbbtt/p/7563053.html