java 数据结构之顺序表

package com.xdl.data_stru;

public class Day_Two_Node {

	public int nKey;

	public int nNum;

	public Day_Two_Node(int nKey, int nNum) {
		this.nKey = nKey;
		this.nNum = nNum;
	}

	public void setValue(int nKey, int nNum) {
		this.nKey = nKey;
		this.nNum = nNum;
	}

	public int getnKey() {
		return nKey;
	}

	public int getnNum() {
		return nNum;
	}

	@Override
	public String toString() {
		StringBuffer toBuffer = new StringBuffer();
		return toBuffer.append("<").append(String.valueOf(nKey)).append(",").append(String.valueOf(nNum)).append(">").toString();
	}

	@Override
	public int hashCode() {
		return 0;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj==null) {
			return false;
		}
		if (obj instanceof Day_Two_Node) {
			Day_Two_Node Dtd=(Day_Two_Node)obj;
			if (this.nKey == Dtd.nKey && this.nNum ==Dtd.nNum) {
				return true;
			}else {
				return false;
			}
		}
		return false;
	}
}

package com.xdl.data_stru;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import org.junit.Test;

/**
 * @author xudaolong
 * 顺序表:
 * 优点:简单,存储顺序对应逻辑顺序,存储的开销较少;解析简单;
 * 缺点:预存储分配;空间利用率不高;增删产生大量的数据元素的移动,时间效率低;
 */
@SuppressWarnings("unused")
public class Day_Two_SeqTable {
    public int len; // 已存在的个数
    public Day_Two_Node[] data; // 元素的结点

    // 初始化,因为ADT是有限的,故固大小
    public void initList(int maxnum) {
	this.data = new Day_Two_Node[maxnum];
	this.len = 0;
    }

    // 销毁,与初始化相对
    private void ruinList() {
	this.len = 0;
	this.data = null;
    }

    // 重置
    private void clearList() {
	this.len = 0;
    }

    // 线性表的长度咯
    public int getLen() {
	return len;
    }

    // 是否为空
    private boolean isEmpty() {
	if (this.len == 0) {
	    return true;
	} else {
	    return false;
	}
    }

    // 取出第i个数据元素
    private Day_Two_Node getElem(int i) {
	if (i < 1 || i > this.len) {
	    return null;
	}
	return this.data[i - 1];
    }

    // 检测数据元素是否存在并返回当前下标值,否则返回最大len的值
    private int check_elem_exist(Day_Two_Node e) {
	int i;
	for (i = 0; i < this.len && !e.equals(this.data[i]); i++)
	    ;
	return i;
    }
    //表是不是满
    private boolean check_list_full() {
	// data[]决定了表大小,len表示已存在的个数
	if (this.len >= this.data.length) {
	    return false;
	}
	return true;
    }

    /**
     * @param e
     *            : 指定Node
     * @param behavior
     *            :分为获取, locate(当前元素),prior(前驱元素),next(后继元素)
     * @return
     */
    private Day_Two_Node getElem(Day_Two_Node e, String behavior) {
	int check_node_exist = check_elem_exist(e);
	switch (behavior) {
	case "locate":
	    if (check_node_exist >= this.len - 1) {
		return null;
	    }
	    return this.data[check_node_exist];// 然后根据 e.getnNum()或者当下的值
	case "prior":
	    if (check_node_exist >= this.len || check_node_exist == 0) {
		System.out.println("该值没有前驱");
		return null;
	    }
	    // 返回e的直接前驱元素
	    return this.data[check_node_exist - 1];
	case "next":
	    if (check_node_exist >= this.len - 1) {
		System.out.println("该值没有后继元素");
		return null;
	    }
	    // 返回e的直接后继元素
	    return this.data[check_node_exist + 1];
	default:
	    break;
	}
	return null;
    }

    // 在i之前插入数据元素
    private boolean insert_i_list(int i, Day_Two_Node e) {
	int j = this.len;
	boolean check_list_full = this.check_list_full();
	// 对i的范围规定,两端可插
	if (i < 1 || i > this.len + 1 || !check_list_full) {
	    return false;
	}
	while (j >= i) {
	    this.data[j] = this.data[j - 1];
	    j--;
	}
	this.data[i - 1] = e;
	this.len++;
	return true;
    }

    private boolean insert_node_list(Day_Two_Node aNode, Day_Two_Node bNode) {
	int  j= this.len;
	boolean check_list_full = this.check_list_full();
	int check_node_exist = check_elem_exist(aNode);
	if (check_node_exist >= this.len || !check_list_full) {
	    return false;
	}
	while (j > check_node_exist) {
	    this.data[j] = this.data[j - 1];
	    j--;
	}
	this.data[check_node_exist]=bNode;
	this.len++;
	return true;
    }

    private boolean insert_end_list(Day_Two_Node e) {
	boolean check_list_full = this.check_list_full();
	if (check_list_full) {
	    this.data[this.len]=e;
	    this.len++;
	}
	return true;
    }

    // 删除第i个元素
    private boolean delete_i_list(int i) {
	if (this.isEmpty()) {
	    return false;
	}
	if (i<1|| i>this.len) {
	    return false;
	}
	int j=i;
	while (j<=this.len-1) {
	    this.data[j-1]=this.data[j];
	    j++;
	}
	this.len--;
	return true;
    }
    
    //删除第一个相同的节点
    private boolean delete_same_list(Day_Two_Node e) {
	if (this.isEmpty()) {
	    return false;
	}
	int check_node_exist = this.check_elem_exist(e);
	if (check_node_exist>=this.len) {
	    return false;
	}
	int j = check_node_exist +1;
	while (j<= this.len -1) {
	    this.data[j-1]=this.data[j];
	    j++;
	}
	this.len--;
	return true;
    }
    
    //删除最后的结点
    private boolean delete_end_list() {
	if (this.isEmpty()) {
	    return false;
	}
	this.len--;
	return true;
    }

    public Day_Two_Node[] getData() {
	return data;
    }

    public void setData(Day_Two_Node[] data) {
	this.data = data;
    }

    public void setLen(int len) {
	this.len = len;
    }

    @Test
    public void getElem_is_run_Test() {
	Day_Two_SeqTable dts = new Day_Two_SeqTable();
	Day_Two_Node one = new Day_Two_Node(1, 1);
	Day_Two_Node two = new Day_Two_Node(2, 2);
	Day_Two_Node three = new Day_Two_Node(3, 3);
	dts.initList(10);
	dts.setLen(3);
	dts.data[0] = one;
	dts.data[1] = two;
	/* dts.data[2] = three; */
	/*assertEquals(dts.check_elem_exist(one), 0);
	assertEquals(dts.check_elem_exist(two), 1);
	assertEquals(dts.check_elem_exist(three), 3);
	assertEquals(dts.getElem(one, "locate").getnNum(), 1);
	assertEquals(dts.getElem(one, "locate"), one);
	assertEquals(dts.getElem(one, "prior"), null);
	assertEquals(dts.getElem(one, "next").getnNum(), 2);
	
	assertEquals(dts.insert_i_list(2, three), true);
	assertEquals(dts.insert_i_list(4, three), true);
	assertEquals(dts.insert_node_list(two, three), true);
	assertEquals(dts.insert_end_list(three), true);*/
	
	/*assertEquals(dts.delete_i_list(2), true);
	assertEquals(dts.delete_same_list(one), true);*/
	assertEquals(dts.delete_end_list(), true);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天瞳月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值