数据结构学习daytwo(二)

* dayTwo(二)

* 线性表的链式表示和实现

* 链式存储结构不要求逻辑上相邻的元素在物理位置上也相邻,因此在做插入和删除操作时,不需要移动大量的数据。

* 但同时,链式存储结构不能像顺序表一样可随机存取。

* 线性表的链式存储结构的特点是:

* 用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的)。

* 数据元素的存储映像成为结点(node)。

* 链表的结点包含两个域:

* 数据域:存储数据元素信息的域。

* 指针域:存储直接后继存储位置的域。

* 今天学习的是线性表当中的链式表示的单链表。

* 整个链表的存取必须从头指针开始进行。

* 1)头指针:指向链表中第一个结点(头结点或无头结点时的开始结点)的指针。

* 2)头结点:在开始结点前附加的一个结点。

* 3)首结点:存储第一个数据元素的结点。

* 用线性链表表示线性表时,数据元素之间的逻辑关系是由结点中的指针指示的,指针为数据元素之间的逻辑关系的映像,

* 则逻辑上相邻的两个数据元素其存储的物理位置不要求紧邻,这种存储结构为非顺序映像或链式映像。

* 以下为本人写的单链表实现(如有错误请大佬指正)

/**
 * 链表实现类
 */
public class LinkList {
    /**
     * node结点
     */
    private class Node {
        Object data;
        Node next;

        private Node() {
            this.data = null;
        }

        private Node(Object object) {
            this.data = object;
        }
    }

    //链表头指针
    private Node head;

    //链表
    private Node list;

    //链表总长度
    private int length;

    /**
     * 创建一条链表
     */
    public LinkList() {
        this.head = new Node();
        this.length = 1;
    }

    /**
     * 创建一条有初始数据元素的链表
     * @param object 要存储的数据元素
     */
    public LinkList(Object object) {
        this.head = new Node(object);
        this.length = 1;
    }

    /**
     * 查找指定位置的元素
     * @param place 输入要查找的位置,从1开始
     * @return 返回查找到的元素
     */
    public Object get(int place) {
        if (place < 1 || place > length) {
            System.out.println("要查找的位置不存在");
            return null;
        }
        this.list = this.head;
        for (int i = 1; i <= length; i++) {
            if (i == place) {
                return this.list.data;
            }
            this.list = this.list.next;
        }
        return null;
    }

    /**
     * 向链表末尾添加一个空结点
     */
    public void add() {
        this.list = this.head;
        for (int i = 1; i < length; i++) {
            this.list = this.list.next;
        }
        this.list.next = new Node();
        length += 1;
    }

    /**
     * 向链表末尾添加一个数据结点
     * @param object 要添加的数据元素
     */
    public void add(Object object){
        if (null == this.head.data){
            this.head.data = object;
            return;
        }
        this.list = this.head;
        for (int i = 1; i < length; i++) {
            this.list = this.list.next;
        }
        this.list.next = new Node(object);
        length += 1;
    }

    /**
     * 向链表指定位置添加一个数据结点
     * @param place 要添加数据结点的位置
     * @param object 要添加的数据元素
     */
    public void add(int place,Object object){
        if (place < 1 || place > length) {
            System.out.println("要插入的位置不存在");
            return;
        }
        if (1 == place){
            this.list = this.head;
            this.head = new Node(object);
            this.head.next = this.list;
            length += 1;
            return;
        }
        this.list = this.head;
        for (int i = 1; i <= length; i++) {
            if (i == place-1){
                Node node = new Node(object);
                node.next = this.list.next;
                this.list.next = node;
                break;
            }
            this.list = this.list.next;
        }
        length += 1;
    }

    /**
     * 清空链表
     */
    public void clear(){
        this.list = this.head;
        this.head = null;
        this.list = null;
        this.length = 0;
    }

    /**
     * 删除指定位置的元素
     * @param place 要删除元素的位置
     */
    public void remove(int place){
        if (place < 1 || place > length) {
            System.out.println("要删除的位置不存在");
            return;
        }
        if (1 == place){
            this.head = this.head.next;
            length -= 1;
            return;
        }
        this.list = this.head;
        for (int i = 1;i < length;i++){
            if (i == place - 1){
                list.next = list.next.next;
                break;
            }
            this.list = this.list.next;
        }
        length -= 1;
    }

    /**
     * 返回链表的长度
     * @return 链表长度
     */
    public int size(){
        return this.length;
    }
}

转载于:https://my.oschina.net/u/3970508/blog/3027563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值