java 数据结构之 链式线性表

package com.xdl.data_stru;

public class Day_Two_LinkNode extends Day_Two_Node{
    
    /*双向链表的node
     * public Day_Two_LinkNode prior;*/
    public Day_Two_LinkNode next;
    
    public Day_Two_LinkNode getNext() {
        return next;
    }

    public void setNext(Day_Two_LinkNode next) {
        this.next = next;
    }

    /**
     * 初始化头部
     */
    public Day_Two_LinkNode() {
	super.nKey =' ';
	super.nNum = 0 ;
    }
    
    public Day_Two_LinkNode(int nKey, int nNum) {
	super(nKey, nNum);
    }
}
package com.xdl.data_stru;

/**
 * @author xudaolong
 *         单链表:引入头结点,可存储线性表长度的信息,且末结点的指针域为null(以^表示,若头结点的指针域为null,则该表为空表)
 *         特点:物理逻辑顺序不一致;顺序存取方式
 *         关键弄明白: p 与  p.next 关系
 * 	   双向链接的:主要处理p.next 和 p.prior 以及 p.next.prior /p.prior.next 关系就ok叻
 */
@SuppressWarnings("unused")
public class Day_Two_LinkTable {

    /**
     * head:头部,len已存在的元素个数
     */
    public Day_Two_LinkNode head;
    private int len;

    /**
     * @return 核心:对头部的初始,跟seqTable相似也要分配好data的大小
     */
    private boolean init() {
	this.head = new Day_Two_LinkNode();
	if (this.head == null) {
	    return false;
	}
	/*循环单链表的初始化
	 * this.head.next= this.head;
	 * this.len = 0;
	 * */
	return true;
    }

    /**
     * 借刀杀人,目的将p.next为空
     */
    private void clear_node() {
	Day_Two_LinkNode p = this.head, q;
	/*循环单链表的判定
	 * p != null改为 p!= this.head*/
	while (p != null) {
	    q = p;
	    p = p.next;
	    q.next = null;// q.next 就是 p 的后一个对象
	    q = null;
	}
    }

    /**
     * 销毁
     */
    private void ruin() {
	clear_node();
	this.len = 0;
	this.head = null;
    }

    /**
     * 清空
     */
    private void clear() {
	clear_node();
	this.len = 0;
    }

    private int get_length() {
	return this.len;
    }

    private boolean isEmpty() {
	if (this.len == 0) {
	    return true;
	} else {
	    return false;
	}
    }

    /**
     * @param i
     * @return 核心:判断i的范围,然后由p遍历i次;
     */
    private Day_Two_LinkNode get_i_elem(int i) {
	if (i < 1 || i > this.len + 1) {
	    return null;
	}
	Day_Two_LinkNode p = this.head.next; // 从第一个开始
	int j = 1;
	while (j != i) {
	    p = p.next;
	    j++;
	}
	return p;
    }

    /**
     * @param e
     * @return 核心:当e.equals(p)为真的时候,则返回当前的i值,再判断i的范围
     */
    private int get_e_elem(Day_Two_LinkNode e) {
	Day_Two_LinkNode p = this.head.next; // 从第一个开始
	int i = 1;// 作用只是能得到第几个值而已
	/*循环单链表的判定
	 * p != null改为 p!= this.head*/
	while (p != null && !e.equals(p) && this.isEmpty()) {
	    p = p.next;
	    i++;
	}
	if (i > this.len) {
	    return 0;
	}
	return i;
    }

    /**
     * @param e
     * @return 核心:跟游戏中换装备一样,先把身上(this.head.nex)放到包裹中(q),然后换上,如此循环
     */
    private Day_Two_LinkNode get_prior_elem(Day_Two_LinkNode e) {
	Day_Two_LinkNode p = this.head.next;
	Day_Two_LinkNode q = this.head;
	/*循环单链表的判定
	 * p != null改为 p!= this.head*/
	while (p != null && !e.equals(p) && this.isEmpty()) {
	    q = p;// 这个就是我们想要的值,返回前驱
	    p = p.next;
	}
	if (p == null) {
	    return null;
	}
	return q;
    }

    /**
     * @param e
     * @return 核心:不用管丢失前驱
     */
    private Day_Two_LinkNode get_next_elem(Day_Two_LinkNode e) {
	Day_Two_LinkNode p = this.head.next;
	/*循环单链表的判定
	 * p != null改为 p!= this.head*/
	while (p != null && !e.equals(p) && this.isEmpty()) {
	    p = p.next;
	}
	if (p != null) {
	    p = p.next;
	}
	return p;
    }

    /**
     * @param i
     * @param e
     * @return 思考,对值的判断,找到第i-1的元素 表达关系是 0 --> <i-1
     */
    private int inser_i_elem(int i, Day_Two_LinkNode e) {
	if (i < 1 || i > this.len + 1) {
	    return 0;
	}
	Day_Two_LinkNode p = this.head;
	int j = 0;
	while (j < i - 1) {
	    p = p.next;
	    j++;
	}
	e.next = p.next;
	p.next = e;
	this.len++;
	return 1;
    }

    private int insert_e_elem(Day_Two_LinkNode a, Day_Two_LinkNode b) {
	Day_Two_LinkNode p = this.head;
	while (p != null && !a.equals(p) && this.isEmpty()) {
	    p = p.next;
	}
	if (p.next == null) {
	    return 0;
	}
	b.next = p.next;
	p.next = b;
	this.len++;
	return 1;
    }

    private int insert_head_elem(Day_Two_LinkNode e) {
	Day_Two_LinkNode p =this. head;
	e.next = p.next;
	p.next = e;
	this.len++;
	return 1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天瞳月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值