20192305 2020-2021-1 《数据结构与面向对象程序设计》实验六报告

20192305 2020-2021-1 《数据结构与面向对象程序设计》实验六报告

课程:《程序设计与数据结构》
班级: 1923
姓名: 王梓全
学号:20192305
实验教师:王志强
实验日期:2020年11月9日
必修/选修: 必修

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)。
  • (2)实现节点插入、删除、输出操作;
    继续你上一个程序, 扩展它的功能,每做完一个新功能,或者写了超过10行新代码,就签入代码,提交到源代码服务器;
    从磁盘读取一个文件, 这个文件有两个数字。
    从文件中读入数字1, 插入到链表第 5 位,并打印所有数字,和元素的总数。 保留这个链表,继续下面的操作。
    从文件中读入数字2, 插入到链表第 0 位,并打印所有数字,和元素的总数。 保留这个链表,并继续下面的操作。
    从链表中删除刚才的数字1. 并打印所有数字和元素的总数。
  • (3)使用冒泡排序法或者选择排序法根据数值大小对链表进行排序;
    如果你学号是单数, 选择冒泡排序, 否则选择选择排序。
    在排序的每一个轮次中, 打印元素的总数,和目前链表的所有元素。
    在(2)得到的程序中继续扩展, 用同一个程序文件,写不同的函数来实现这个功能。 仍然用 nZhangSan (你的名字)来表示元素的总数。

2. 实验过程及结果

实验一



import java.util.Scanner;

public class shiyanliuTest1 {
public static void main(String[] args) {
int nwangziquan = 0,j=0;
Scanner scan = new Scanner(System.in);
System.out.println("Please input numbers");
String a = scan.nextLine();
String[] b = a.split(" ");
ShiYanLiu_1[] list = new ShiYanLiu_1[20];
for(int i=0;i<b.length;i++)
{
int num=Integer.parseInt(b[i]);
list[i] = new ShiYanLiu_1(num);
nwangziquan++;
}
ShiYanLiu_1 head = list[0];
for(int i=0;i<nwangziquan;i++)
{
list[j].setNext(list[++j]);
}
System.out.println("\n"+"元素个数:"+nwangziquan);
System.out.println(head.print(head));
}
}

实验二




import java.io.*;
import java.util.Scanner;

public class shiyanliuTest2 {
public static void main(String[] args) throws IOException {
int i,num;
File file = new File("C:\text", "text.txt");
if (!file.exists()) {
file.createNewFile();
}
Reader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String content = bufferedReader.readLine();

    String[] ss1 = content.split(" ");
    ShiYanLiu_1[] list1 = new ShiYanLiu_1[2];
    for(i=0;i<ss1.length;i++)
    {
        num=Integer.parseInt(ss1[i]);
        list1[i] = new ShiYanLiu_1(num);
    }

    int nwangziquan = 0,j=0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input numbers");
    String a = scan.nextLine();
    String[] b = a.split(" ");
    ShiYanLiu_1[] list = new ShiYanLiu_1[20];
    for(i=0;i<b.length;i++)
    {
        num=Integer.parseInt(b[i]);
        list[i] = new ShiYanLiu_1(num);
        nwangziquan++;
    }
    ShiYanLiu_1 head = list[0];
    for(i=0;i<nwangziquan;i++)
    {
        list[j].setNext(list[++j]);
    }
    System.out.println("\n"+"元素个数:"+nwangziquan);
    System.out.println(head.print(head));

    int q=5;
    System.out.println("插入元素1:");
    head.insert(head,q-1,list1[0]);
    nwangziquan++;
    System.out.println(head.print(head));

    System.out.println("插入元素2:");
    head.insert(head,0,list1[1]);
    head = list1[1];
    nwangziquan++;
    System.out.println(head.print(head));

    System.out.println("删除元素1:");
    head.delete(head,5);
    System.out.println(head.print(head));
    nwangziquan--;
    System.out.println("元素个数:"+nwangziquan);


}

}

实验三



import java.io.*;
import java.util.Scanner;

public class shiyanliuTest3 {
private static int nwangziquan;

public static void main(String[] args) throws IOException {
    int i,num;
    File file = new File("C:\\text", "text.txt");
    if (!file.exists()) {
        file.createNewFile();
    }
    Reader reader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String content = bufferedReader.readLine();

    String[] ss1 = content.split(" ");
    ShiYanLiu_1[] list1 = new ShiYanLiu_1[2];
    for(i=0;i<ss1.length;i++)
    {
        num=Integer.parseInt(ss1[i]);
        list1[i] = new ShiYanLiu_1(num);
    }

    int nwangziquuan = 0,j=0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input numbers");
    String a = scan.nextLine();
    String[] b = a.split(" ");
    ShiYanLiu_1[] list = new ShiYanLiu_1[20];
    for(i=0; i<b.length; i++)
    {
        num=Integer.parseInt(b[i]);
        list[i] = new ShiYanLiu_1(num);
        nwangziquan ++;
    }
    ShiYanLiu_1 head = list[0];
    for(i=0;i<nwangziquan ;i++)
    {
        list[j].setNext(list[++j]);
    }
    System.out.println("\n"+"元素个数:"+nwangziquan );
    System.out.println(head.print(head));

    System.out.println("学号:20192305,冒泡排序");

    head.sort(head,nwangziquan );
    System.out.println(head.print(head));
    System.out.println("\n"+"元素个数:"+nwangziquan );

}

}

实验中遇到的问题和解决过程

  • 问题一:一开始我没有给head赋值,老出现地址越界的错误。
  • 问题一解决方法:直接在有了第一个数之后才开始定义这个head,这样的话它一出生就是有初值的,不会越界。

其他(感悟、思考等)

本次实验学会了怎样建立一个链表,以及对链表进行输出、插入、删除、冒泡排序的操作。这次Java的排序其实思路很简单,但是对于链表的使用和各种方法的实现却让我觉得很难,还是要积极自主敲代码。

参考资料

posted @ 2020-11-10 18:12  王梓全  阅读(160)  评论(0编辑  收藏  举报