华南农业大学数据结构java版实验二
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告二线性表
华南农业大学信息(软件)学院
《数据结构(JA V A)》综合性、设计性实验成绩单
开设时间:2017学年第二学期
一,实验目的:
(1)理解线性表的逻辑结构、两种存储结构和数据操作,熟练运用JAVA语言实现线性表的基本操作,分析各种操作算法特点和时间复杂度。
(2)掌握单链表的遍历、插入和删除等操作算法,实现多项式相加。
二,实验内容:
1、设计一个有序顺序表(元素已排序,递增或递减),实现插入、删除等操作,元素插入位置由其值决定。
实现:
(1)升序排序顺序表类名为:SortedSeqList,存成文件;
(2)另外编写文件来演示调用排序顺序表
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 || == 0) {
= 0;
} else {
ary = 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[ - 1];
for (int i = 0, j = 0; i < ; 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 < ; i++, j++) {
if (ary[i] >= value) {
newAry[j] = value;
break;
} else {
newAry[j] = ary[i];
}
}
while (i < {
newAry[++j] = ary[i];
i++;
}
ary = newAry;
length++;
return value;
}
public void display() {
"\nList now is: ");
for (int i = 0; i < ; i++) {
v1.0 可编辑可修改 + "\t");
}
}
}
(2)文件来演示调用排序顺序表
public class SortedSeqList_ex {
public static void main(String[] args) throws Exception {
int[] ary = {1, 2, 3, 5, 7};
SortedSeqList list = new SortedSeqList(ary);
();
(4);
();
(2);
();
}
}
(3)实验结果
2、在SinglyLinkedList类中增加下列成员方法。
public SinglyLinkedList(E[] element);
public class Test {
static Integer[] x={3,5,8,11};
static Integer[] x1={3,5,8,11};
static Integer[] x2={2,6,8,9,11,15,20};
static SinglyLinkedList
for(Node temp= "%-5d",;
}
}
public static void concat(){
(List3);
}
public static void Find(){
"请输入需要查找的数:");
Scanner scan=new Scanner;
Integer element=();
Node t=(element);
if(t!=null)
else
"List1中找不到指定的数。");
}
public static void Contain(){
"请输入所需包含的数:");
Scanner scan=new Scanner;
Integer element=();
if(element))
"List3包含指定的数%-5d\n",element);