当前位置:文档之家› 数据结构实验一顺序表问题及实验报告模板 - Copy

数据结构实验一顺序表问题及实验报告模板 - Copy

4.从键盘输入1个整数,表示欲删除结点的位置,输出顺序表所有结点值,观察输出结果。
调试数据:第一次delete the number at "2",第二次delete value "9"
注意:顺序表输出表现形式如下(实验报告上为截图):
顺序表:
第一题
Initially Seq: head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> null
else if (comp < 0) r = m - 1;
else l = m + 1;
}
return NONE;
}
public static void main(String[] args) {
String[] word1 = {"dog", "cat", "rat", "pig", "fox", "eel"};
String target = testcases[t];
System.out.println(target
+ " " + linearSearch1(word1, left1, right1, target) + " "
+ linearSearch2(word2, left2, right2, target)
for (int t = 0; t < testcases.length; t++) {
String target = testcases[t];
System.out.println(target
+ " " + linearSearch1(word1, left1, right1, target) + " "
}
public static int linearSearch2(Comparable[] a, int left, int right, Comparable val)
{
for (int i = left; i <= right; i++)
{ int comp = pareTo(a[i]);
int left, int right, Comparable val)
{
int l = left, r = right;
while (l <= r) {
int m = (l + r) / 2;
int comp = pareTo(a[m]);
if (comp == 0) return m;
int left2 = 0, right2 = word2.length - 1;
String[] testcases = {"ant", "bat", "cat", "dog", "eel","fox", "hen", "pig", "rat"};
for (int t = 0; t < testcases.length; t++) {
实验一顺序表问题
【实验报告】
《数据结构与算法》实验报告一
学院:计算机与信息学院
班级:
学号:
姓名:
日期:
程序名:
一、上机实验的问题和要求:
顺序表的查找、插入与删除。设计算法,实现线性结构上的顺序表的产生以及元素的查找、插入与删除。具体实现要求:
1.从键盘输入10个整数,产生顺序表,并输出结点值。
调试数据:9 8 7 6 5 4 3 2 1
+ linearSearch2(word2, left2, right2, target)
+ " " + binarySearch(word2, left2, ri输出结果:
四、调试和运行程序过程中产生的问题及采取的措施:
int left1 = 0, right1 = word1.length - 1;
String[] word2 = {"cat", "dog", "eel", "fox", "pig", "rat"};
int left2 = 0, right2 = word2.length - 1;
String[] testcases = {"ant", "bat", "cat", "dog", "eel","fox", "hen", "pig", "rat"};
以下程序仅供参考,其结果不代表本实验要求。学生可输入以下程序,调试并运行之,分析输出结果,然后结合书本上的算法进行修改,完成上述实验内容,请实验完成之后,把正确的程序复制在此处。程序一:
public class Searching {
private static final int NONE = -1;
{
for (int i = left; i <= right; i++)
{ int comp = pareTo(a[i]);
if (comp == 0)
return i;
else if (comp < 0)
break;
}
return NONE;
}
public static int binarySearch(Comparable[] a,
第二题
找不到
6
第三题
insert "11" after "6": head -> 9 -> 8 -> 7 -> 6 ->11-> 5 -> 4 -> 3 -> 2 -> 1 -> null
insert"86"at"2":head -> 9 -> 8 -> 86 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 ->null
while (l <= r) {
int m = (l + r) / 2;
int comp = pareTo(a[m]);
if (comp == 0) return m;
else if (comp < 0) r = m - 1;
else l = m + 1;
}
return NONE;
}
public static void main(String[] args) {
String[] word1 = {"dog", "cat", "rat", "pig", "fox", "eel"};
int left1 = 0, right1 = word1.length - 1;
String[] word2 = {"cat", "dog", "eel", "fox", "pig", "rat"};
+ " " + binarySearch(word2, left2, right2, target));
}
}
}
程序二
public class Searching {
private static final int NONE = -1;
public static int linearSearch1(Object[] a,int left, int right, Object val)
public static int linearSearch1(Object[] a,int left, int right, Object val)
{
for (int i = left; i <= right; i++)
if (val.equals(a[i]))
return i;
return NONE;
if (comp == 0)
return i;
else if (comp < 0)
break;
}
return NONE;
}
public static int binarySearch(Comparable[] a,
int left, int right, Comparable val)
{
int l = left, r = right;
第四题
delete the number at "2":head -> 9 -> 8-> 6 -> 5 -> 4 -> 3 -> 2 -> 1 ->null
delete value "9": head -> 8 -> 7 -> 6 -> 5-> 4 -> 3 -> 2 -> 1-> null
二、源程序及注释:
{
for (int i = left; i <= right; i++)
相关主题