永发信息网

java语言,设计按升序排序顺序表类,实现插入、删除操作,元素插入位置由其值决定。(数据结构)

答案:1  悬赏:10  手机版
解决时间 2021-04-03 04:23
java语言,设计按升序排序顺序表类,实现插入、删除操作,元素插入位置由其值决定。(数据结构)
最佳答案
SortedSeqList.java
public class SortedSeqList {

private int MAX_SIZE = 10;

private int[] ary = new int[MAX_SIZE];

private int length = 0;

public SortedSeqList(int[] array) {
if (array == null || array.length == 0) {
this.length = 0;
} else {
ary = array;
length = array.length;
}
}

public void clear() {
length = 0;
}

public boolean isEmpty() {
return length == 0;
}

public void delete(int index) throws Exception {
if (length == 0) {
throw new Exception("No elment to delete");
}

int newAry[] = new int[ary.length - 1];

for (int i = 0, j = 0; i < ary.length; i++) {
if (i == index) {
continue;
} else {
newAry[j++] = ary[i];
}
}

ary = newAry;
length--;
}

public int insert(int value) throws Exception {
if (length == MAX_SIZE) {
throw new Exception("List is full, can't insert more");
}

int[] newAry = new int[length + 1];

int i = 0, j = 0;
for (; i < ary.length; i++, j++) {
if (ary[i] >= value) {
newAry[j] = value;
break;
} else {
newAry[j] = ary[i];
}
}

while (i < ary.length) {
newAry[++j] = ary[i];
i++;
}

ary = newAry;
length++;

return value;

}

public void display() {
System.out.println("\nList now is: ");
for (int i = 0; i < ary.length; i++) {
System.out.print(ary[i] + "\t");
}
}

}

--------------------------SortedSeqList_ex.java
public class SortedSeqList_ex {

public static void main(String[] args) throws Exception {

int[] ary = {1, 2, 3, 5, 7};

SortedSeqList list = new SortedSeqList(ary);
list.display();
list.insert(4);
list.display();
list.delete(2);
list.display();
}

}
---------------testing result

List now is:
1 2 3 5 7
List now is:
1 2 3 4 5 7
List now is:
1 2 4 5 7
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
YOUaremylifedong什么意思?学霸解答一下谢谢
化纤面料涂层之前是不是一定要先上防水
2014年教师资格证有效期几年
我订票机票,提前5天退票手续费多少
我把手机格式化后没有办法保存图片了。我的手
苏州久保田农业机械公司怎么样???
3,5,6,10先乘后加等于13选择三个数p
HiPee的尿检项目都有哪些有知道的吗?
森歌和美大哪个比较好,是不是更好用
送别朋友的名言或诗句
这句话是purpose?还是purposes?
stat.php这是什么,为什么会自动下载
最近在工作遇到问题,有跳槽打算,在智联浏览
火锅加盟如何成功
(x-2)*2+2*4+4=(x-1)*4+1*6-12 怎么解
推荐资讯
下列不属于家畜的是A.猪B.鹅C.牛D.羊
胡母在哪里见到了阴府判官
萨摩幼犬老咬人怎么办
短篇抗日小说。要短的!
有谁知道这些数字是什么意思?
SD敢达V2突击暴击型好吗,还是突击型好,还是
律师能否查到某人有没有做过委托公证
魔兽世界的术士制作的灵魂碎片怎么用?
一()火
请问使用了固态ssd内存后,还需要使用ddr内存
把我捧在手心里的歌曲鉴赏
主角是秦溪慕谨寒的小说叫什么 一爱而深秦溪
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?