20192314于鲲洋 2020-2021-1 《数据结构与面向对象程序设计》实验六报告
1.链表练习,要求实现下列功能:
通过键盘输入一些整数,建立一个链表;
这些数是你学号中依次取出的两位数。 再加上今天的时间。
例如你的学号是 20172301
今天时间是 2018/10/1, 16:23:49秒
数字就是
20, 17,23,1, 20, 18,10,1,16,23,49
打印所有链表元素, 并输出元素的总数。
在你的程序中,请用一个特殊变量名来纪录元素的总数,变量名就是你的名字。 例如你叫 张三, 那么这个变量名就是
int nZhangSan = 0; //初始化为 0.
做完这一步,把你的程序签入源代码控制(git push)。
1.程序代码
import java.util.Scanner;
public class ex61 {
public static void main(String[] args) {
int[] x = new int[11];
Scanner sc = new Scanner(System.in);
int y = 0;
for (y = 0; y <= 10; y++) {
x[y] = sc.nextInt();
}
sc.close();
ex61zhulei a = new ex61zhulei(x[0]);
ex61zhulei b = new ex61zhulei(x[1]);
ex61zhulei c = new ex61zhulei(x[2]);
ex61zhulei d = new ex61zhulei(x[3]);
ex61zhulei e = new ex61zhulei(x[4]);
ex61zhulei f = new ex61zhulei(x[5]);
ex61zhulei g = new ex61zhulei(x[6]);
ex61zhulei h = new ex61zhulei(x[7]);
ex61zhulei i = new ex61zhulei(x[8]);
ex61zhulei j = new ex61zhulei(x[9]);
ex61zhulei k = new ex61zhulei(x[10]);
ex61zhulei head = a;
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = g;
g.next = h;
h.next = i;
i.next = j;
j.next = k;
ex61.Printlinkedlist(a);
}
public static void Printlinkedlist(ex61zhulei head) {
ex61zhulei point = head;
int Yukunyang = 0;
while(point != null){
System.out.println("number:"+point.number);
point = point.next;
Yukunyang++;
}
System.out.println("Yukunyang:"+Yukunyang);
}
}
public class ex61zhulei {
protected int number;
protected ex61zhulei next = null;
public ex61zhulei(int number) {
this.number = number;
}
@Override
public String toString() {
return "ex61zhulei{" +
"number=" + number +
", next=" + next +
'}';
}
}
2.运行截图
2.链表练习,要求实现下列功能:
实现节点插入、删除、输出操作;
继续你上一个程序, 扩展它的功能,每做完一个新功能,或者写了超过10行新代码,就签入代码,提交到源代码服务器;
从磁盘读取一个文件, 这个文件有两个数字。
从文件中读入数字1, 插入到链表第 5 位,并打印所有数字,和元素的总数。 保留这个链表,继续下面的操作。
从文件中读入数字2, 插入到链表第 0 位,并打印所有数字,和元素的总数。 保留这个链表,并继续下面的操作。
从链表中删除刚才的数字1. 并打印所有数字和元素的总数。
3.链表练习,要求实现下列功能:
使用冒泡排序法或者选择排序法根据数值大小对链表进行排序;
如果你学号是单数, 选择冒泡排序, 否则选择选择排序。
在排序的每一个轮次中, 打印元素的总数,和目前链表的所有元素。
在(2)得到的程序中继续扩展, 用同一个程序文件,写不同的函数来实现这个功能。 仍然用 nZhangSan (你的名字)来表示元素的总数。
我的(2)和(3)的功能在同一个程序中实现
1.程序代码
Main
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Please input your studentnumber and the time (eg: 20,19,23,14,20,20,11,8,14,4,30)");
String sen = scan.nextLine();
int yky = 0, number2;
int[] number = new int[100];//定义数组
String[] numbers = sen.split(",");//用,分割字符串
//将字符串转化为int型并且放入数组
for (int m = 0; m < numbers.length; m++) {
number[m] = Integer.parseInt(numbers[m]);
}
//实例化并且定义Head的表头
Link Head = new Link(number[0]);
//形成数组链表
int nYukunyang = 1;
for (int a = 1; a < numbers.length; a++) {
Link node = new Link(number[a]);
Head.BackLink(Head, node);
nYukunyang++;
}
//创建文件
File file = new File("LinkNum.txt");
if (!file.exists()) {
file.createNewFile();
}
Writer writer1 = new FileWriter(file);
writer1.write("2019 ");
writer1.flush();
writer1.append("2330");
writer1.flush();
Reader reader = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(reader);
StringTokenizer stringTokenizer = new StringTokenizer(bufferedreader.readLine());
number2 = stringTokenizer.countTokens();
int[] ints = new int[number2];
while (stringTokenizer.hasMoreTokens()) {
ints[yky] = Integer.parseInt(stringTokenizer.nextToken());
yky++;
}
Link q = new Link(ints[0]);
Link p = new Link(ints[1]);
System.out.println("The list is: ");
Head.toString();
System.out.println();
System.out.println("There are " + nYukunyang + " numbers in the list");
Link Head1= Link.HeadInsert(Head, p);
System.out.println("There are " + nYukunyang++ + " numbers in the list");
System.out.println();
Link.MidInsert(Head1, q, 5);
System.out.println("There are " + nYukunyang++ + " numbers in the list");
System.out.println();
Link.DeleteLink(Head1, 5);
System.out.println("There are " + nYukunyang-- + " numbers in the list");
System.out.println();
Link.SelectionSort(Head1);
System.out.println("There are " + nYukunyang + " numbers in the list");
System.out.println();
}
}
Link
public class Link {
protected int number;
protected Link next = null;
public Link(int number){
this.number = number;
}
public String toString()
{
Link node =this;
while (node!=null) {
System.out.print(node.number+" ");
node = node.next;
}
System.out.println();
return null;
}
//尾插法
public void BackLink(Link Head,Link node1){
Link temp=Head;
while(temp.next!=null)
temp=temp.next;
temp.next=node1;
}
//头插法
public static Link HeadInsert(Link Head,Link node){
System.out.println("The list headinserted is: ");
node.next=Head;
Head=node;
Head.toString();
return Head;
}
//中间插入
public static void MidInsert(Link Head,Link node1,int place){
System.out.println("The list midinserted is: ");
Link temp= Head;
int a=1;
while(a<4){
temp= temp.next;
a++;
}
node1.next=temp.next;
temp.next=node1;
Head.toString();
}
//删除
public static void DeleteLink(Link Head, int t) {//删除可以指向他的下一个元素
System.out.println("The list deleted is: ");
Link temp = Head;
Link temp1=Head;
Link node=Head;
for (int i = 0; i < t-2; i++) {
temp1 = temp1.next;
}
temp1.next = temp1.next.next;
while (temp1!=null) {
temp1 = temp1.next;
}
Head.toString();
}
//选择排序
public static void SelectionSort(Link Head) {
System.out.println("The list after selectiondort is: ");
int temp;
Link node = Head;
while (node != null) {
Link reNode = node.next;
while (reNode != null) {
if (reNode.number > node.number) {
temp = reNode.number;
reNode.number = node.number;
node.number = temp;
}
reNode = reNode.next;
}
node = node.next;
}
Head.toString();
}
}
2.运行截图