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;
}
}
java 数据结构之 链式线性表
最新推荐文章于 2022-11-21 10:15:27 发布