java语言,设计按升序排序顺序表类,实现插入、删除操作,元素插入位置由其值决定。(数据结构)
菜鸟一只总做不出来,求高手指点~在线等T_T最好详细代码!!升序排序顺序表类名为:SortedSeqList,存成SortedSeqList.java文件;另外编写Sor...
菜鸟一只总做不出来,求高手指点~在线等T_T 最好详细代码!!
升序排序顺序表类名为:SortedSeqList,存成SortedSeqList.java文件;另外编写SortedSeqList_ex.java文件来演示调用排序顺序表)
怎么调用文件演示不怎么理解 展开
升序排序顺序表类名为:SortedSeqList,存成SortedSeqList.java文件;另外编写SortedSeqList_ex.java文件来演示调用排序顺序表)
怎么调用文件演示不怎么理解 展开
1个回答
展开全部
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询