java中前驱后继字符编程_数据结构——Java实现顺序表

一、分析

什么是顺序表?顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表。一个标准的顺序表需要实现以下基本操作:

1、初始化顺序表

2、销毁顺序表

3、清空顺序表

4、检测顺序表是否为空

5、返回顺序表的元素个数

6、返回顺序表中指定位置元素的值

7、返回顺序表中第一个与指定值相同的元素的位置

8、返回指定元素的直接前驱

9、返回指定元素的直接后继

10、向指定位置插入元素

11、删除指定位置的元素

12、遍历顺序表

在Java中,可以借助数组来表示一组地址连续的存储单元,通过对数组进行操作来表示对顺序表进行操作。我们可以将顺序表定义成一个类,将顺序表的基本操作定义成类的方法,初始化顺序表就是将这个类实例化成对象,销毁顺序表就是销毁对象。

二、实现

1、定义类属性和构造函数

1 classInitList{2

3 private int length; //顺序表长度,表示顺序表中的元素个数

4

5 private int [] list; //数组,顺序表主体

6

7 public InitList(int max){ //构造函数,用来初始化顺序表时定义顺序表的最大长度

8 this.list = new int[max];9 this.length = 0;10 }11 }

2、清空顺序表

1 public voidclearList() {

2 this.length = 0; //由于是用length属性来表示顺序表中的元素个数,所以清空顺序表只需将length置零即可

3 }

3、检测顺序表是否为空

1 public booleanlistEmpty() {2 if(this.length == 0) { //通过判断length属性是否为零,即可判断顺序表是否为空3 return true;4 }else{5 return false;6 }7 }

4、返回顺序表的元素个数

1 public intlistLength() {2 return this.length;   //同上返回length值即可3 }

5、返回顺序表中指定位置元素的值

1 public int [] getElem(intsite) {2

3 int [] ret = new int[1]; //用来存储获取的值

4

5 if(site < 1 || site > this.length) { //检测输入的位置是否合法

6 return null;7 }8

9 ret[0] = this.list[site - 1]; //获取指定位置的值

10 returnret;11 }

6、返回顺序表中第一个与指定值相同的元素的位置

1 public int locateElem(intvalue) {2

3 for (int i = 0; i < this.length; i++) { //遍历顺序表

4 if(this.list[i] == value) { //逐值比较,如果相同,返回此元素所在位置

5 return i + 1;6 }

7 }8 return 0; //如未找到,返回零

9 }

7、返回指定元素的直接前驱

1 public int [] priorElem(intvalue) {2

3 int [] ret = new int[this.length]; //定义一个与顺序表同等长度的数组,用来存储找到的直接前驱

4 int in = 1;            //从数组的第二个位置开始存储找到的直接前驱,第一个空间用来存储找到的直接前驱的个数

5

6 for(int i = 1; i < this.length; i++) {   //遍历顺序表

7 if(this.list[i] == value) {    //逐值比较,如果相等,存储其直接前驱到数组中

8 ret[in] = this.list[i - 1];9 in++;10 }11 }12

13 if(in != 1) {               //判断是否找到了直接前驱

14 ret[0] = in - 1;             //将直接前驱的个数存入数组中

15 returnret;16 }else{17 return null;18 }19 }

8、返回指定元素的直接后继

1 public int [] nextElem(int value) {    //代码逻辑同直接前驱,此处不再赘述

2

3 int [] ret = new int[this.length];4 int in = 1;5

6 for(int i = 0; i < this.length - 1; i++) {7 if(this.list[i] ==value) {8 ret[in] = this.list[i + 1];9 in++;10 }11 }12

13 if(in != 1) {14 ret[0] = in - 1;15 returnret;16 }else{17 return null;18 }19 }

9、向指定位置插入元素

1 public int listInsert(int site,intvalue) {2

3 if(site < 1 || site > this.length + 1) {        //判断输入的位置是否合法

4 return -1;5 }else if(this.length == this.list.length) {     //判断顺序表是否已满

6 return -2;7 }8

9 for(int i = this.length - 1; i >= site - 1; i--) { //从顺序表的最后一个元素开始,逐个向后移动一位,直到要插入元素的位置,为要插入的元素腾出空间

10 this.list[i+1] = this.list[i];11 }12

13 this.list[site - 1] = value;               //插入元素

14 this.length++;                        //顺序表长度加一

15 return 0;16 }

10、删除指定位置的元素

1 public boolean listDelete(intsite) {2 if(site < 1 || site > this.length) {         //判断输入的位置是否合法

3 return false;4 }else if(site == this.length) {         //如果要删除的是最后一个元素,直接将顺序表长度减一即可

5 this.length--;6 return true;7 }else{8 for (int i = site - 1; i < this.length; i++) { //从要删除元素的位置开始,将后面的元素逐个向前移动一位,填补删除元素后的空缺

9 this.list[i] = this.list[i + 1];10 }11 this.length--;                     //顺序表长度减一

12 return true;13 }14 }

11、遍历顺序表

public String traverseList() {           //遍历顺序表并输出

String s= "";                  //用来存储顺序表中的值

for (int i = 0; i < this.length; i++) {    //遍历顺序表,存入字符串中

s += this.list[i] + "、";

}if(s.length() == 0) {              //如果顺序表为空,直接返回空字符串

returns;

}return s.substring(0,s.length() - 1);     //删去最后一个顿号

}

三、小结

以上就是顺序表用Java的完整实现,由于只定义了整数的数组,因此只能操作整数数据,但顺序表的基本思想都已实现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值