20202315 实验五和六《数据结构与面向对象程序设计》实验报告

课程:《程序设计与数据结构》
班级: 2023
姓名: 王梦欣
学号:20202315
实验教师:王志强
实验日期:2021年10月28日
必修/选修: 必修

## 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 (你的名字)来表示元素的总数。

4.在android上实现实验(1)和(2)

5.在android平台上实现实验(3)

 

## 2. 实验过程及结果

1.链表练习,打印所有链表元素并输出元素的总和。

链接1(创建链表并编写基本功能):https://gitee.com/besti2023javads/wang-mengxin-20202315/blob/master/Chain.java

链接2(产品代码实现链表练习1的功能):https://gitee.com/besti2023javads/wang-mengxin-20202315/blob/master/Chain1.java

代码1:

import java.util.Scanner;

public class Chain1 {
    public static Chain head;
    public static int nWangMengxin = 0;

    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        System.out.println("input the first number:");
        String str = sca.nextLine();
        Chain chain0 = new Chain(Integer.parseInt(str));
        head = chain0;

        do {
            nWangMengxin++;
            System.out.println("input number or type no if you want to stop");
            str = sca.nextLine();
            if (str.equals("no")) {
                break;
            }
            Chain chain = new Chain(Integer.parseInt(str));
            Chain.push(head, chain);
        } while (!str.equals("no"));

        Chain.Print(head);
        System.out.println("total numbers of elements:" + nWangMengxin);

运行结果截图:

 

 

 

 

2.链表练习,实现节点插入、删除、输出操作,读取文件中的数字插入链表并输出链表及元素总和。

链接:https://gitee.com/besti2023javads/wang-mengxin-20202315/blob/master/Chain1.java

https://gitee.com/besti2023javads/wang-mengxin-20202315/blob/master/Chain_File.java

代码2:实现节点插入、删除、输出操作,并加入选择结构。(包含代码1)

import java.util.Scanner;

public class Chain1 {
    public static Chain head;
    public static int nWangMengxin = 0;

    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        System.out.println("input the first number:");  //创建表头
        String str = sca.nextLine();
        Chain chain0 = new Chain(Integer.parseInt(str));
        head = chain0;

        do {
            nWangMengxin++;
            System.out.println("input number or type no if you want to stop");
            str = sca.nextLine();  //输入链表元素
            if (str.equals("no")) {
                break;  //跳出循环
            }
            Chain chain = new Chain(Integer.parseInt(str));
            Chain.push(head, chain);
        } while (!str.equals("no"));

        Chain.Print(head);
        System.out.println("total numbers of elements:" + nWangMengxin);

        System.out.println("insert? yes/no");
        String choose1 = sca.nextLine();
        if(choose1.equals("yes")) {
            System.out.println("input what you want to insret");
            int insert = sca.nextInt();
            System.out.println("input where you want to insret");
            int node = sca.nextInt();
            Chain.insert(head, insert, node);
            Chain.Print(head);
            System.out.println("total numbers of elements:" + chain0.size(head));
        }

        System.out.println("delete? yes/no");
        String choose2 = sca.next();  //此处不可next.Line,否则会读走剩下的链表
        if(choose2.equals("yes")) {
            System.out.println("input where you want to delete");
            int node = sca.nextInt();
            Chain.delete(head, node);
            Chain.Print(head);
            System.out.println("total numbers of elements:" + chain0.size(head));
        }
    }
}

代码3:实现读取文件中的数字插入链表并输出链表及元素总和的功能。(包含代码1)

import java.util.Scanner;

public class Chain_File {
    public static Chain head;
    public static int nWangMengxin=0;

    public static void main(String[] args) throws IOException {
        Scanner sca = new Scanner(System.in);
        File file =new File("D:\\大二上学期\\Java 数据结构\\java程序","test5.txt");
        try{
            if(!file.exists()){
                file.createNewFile();
                Writer writer = new FileWriter(file,true);
                writer.append("33 44");
                writer.flush();
            }
        }catch (IOException e){
            System.out.println(e);
        }
        InputStream inputStream = new FileInputStream(file);
        String str1 ="";
        while(inputStream.available()>0){
            str1 =str1+(char)inputStream.read();
        }
        System.out.println(str1);
        String[] strs = str1.split(" ");
        int x1 = Integer.parseInt(strs[0]);
        int x2 = Integer.parseInt(strs[1]);

        System.out.println("input the first number:");
        String str = sca.nextLine();
        Chain chain0 = new Chain(Integer.parseInt(str));
        head = chain0;
        do {
            nWangMengxin++;
            System.out.println("input number or type no if you want to stop");
            str = sca.nextLine();
            if(str.equals("no")){
                break;
            }
            Chain chain = new Chain(Integer.parseInt(str));
            Chain.push(head,chain);
        }while(!str.equals("no"));

        Chain.Print(head);
        System.out.println("Number of elements:" + nWangMengxin);
        Chain.insert(head,x1,5);
        Chain.Print(head);
        System.out.println("Number of elements:" +chain0.size(head));
        Chain.insert(head,x2,1);
        Chain.Print(head);
        System.out.println("Number of elements:" +chain0.size(head));

运行结果截图:

 

 

 

 

 

 

3.链表练习,使用冒泡排序法根据数值大小对链表进行排序。

链接:https://gitee.com/besti2023javads/wang-mengxin-20202315/blob/master/Chain_File.java

代码4:选择排序(在代码3的基础上扩充)(原本我的学号应该写冒泡排序,但是一直出错找不到问题出在哪里,给超人发消息超人也不回,

莫得办法,敬请谅解,选择排序凑合看吧。)

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

public class Chain_File {
    public static Chain head;
    public static int nWangMengxin=0;

    public static void main(String[] args) throws IOException {
        Scanner sca = new Scanner(System.in);
        File file =new File("D:\\大二上学期\\Java 数据结构\\java程序","test5.txt");
        try{
            if(!file.exists()){
                file.createNewFile();
                Writer writer = new FileWriter(file,true);
                writer.append("33 44");
                writer.flush();
            }
        }catch (IOException e){
            System.out.println(e);
        }
        InputStream inputStream = new FileInputStream(file);
        String str1 ="";
        while(inputStream.available()>0){
            str1 =str1+(char)inputStream.read();
        }
        System.out.println(str1);
        String[] strs = str1.split(" ");
        int x1 = Integer.parseInt(strs[0]);
        int x2 = Integer.parseInt(strs[1]);

        System.out.println("input the first number:");
        String str = sca.nextLine();
        Chain chain0 = new Chain(Integer.parseInt(str));
        head = chain0;
        do {
            nWangMengxin++;
            System.out.println("input number or type no if you want to stop");
            str = sca.nextLine();
            if(str.equals("no")){
                break;
            }
            Chain chain = new Chain(Integer.parseInt(str));
            Chain.push(head,chain);
        }while(!str.equals("no"));

        Chain.Print(head);
        System.out.println("Number of elements:" + nWangMengxin);
        Chain.insert(head,x1,5);
        Chain.Print(head);
        System.out.println("Number of elements:" +chain0.size(head));
        Chain.insert(head,x2,1);
        Chain.Print(head);
        System.out.println("Number of elements:" +chain0.size(head));

        Chain temp = Chain_File.head;
        Chain temp1 =Chain_File.head;
        int t,w=1,e=1;

        for(int i=1;i<nWangMengxin;i++){
            t= temp.getElement();
            while(temp1.getNext()!=null){
                temp1 = temp1.getNext();
                w++;
                if(temp1.getElement()>t){
                    t= temp1.getElement();
                    e=w+1;
                }
            }
            Chain.insert(head,t,i);
            Chain.delete(head,e);
            Chain.Print(head);
            w=i+1;
            if(e<=w)temp =temp.getNext();
            temp1=temp;
            if(temp1.getNext()==null) break;
        }
 /*Chain temp = Chain_File.head;
        int m=0,n=0;
        for(int i=1;i<nWangMengxin-1;i++){
            for(int j=0;j<(nWangMengxin-i);j++){
                if((int)temp.get(j+1)>(int)temp.get(j)){

                }
            }
        }
        System.out.println("Bubble sort:");
        Chain.Print(temp);
        System.out.println("Number of elements:" +chain0.size(temp));*/
    }
}

运行结果截图:

 

 

4.在android上实现实验(1)和(2)

 

5.在android平台上实现实验(3)

 

 

 

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

- 问题1:在插入与删除的时候加了选择结构之后,第二次选择结构直接跳过,不能实现。
- 问题1解决方案:查阅资料并询问之后发现第二次不能用next.Line,否则会读走剩下的链表。
- 问题2:文件写入的时候,一开始想键盘输入赋值,然后用writer.append写入文件,但一直报错。
- 问题2解决方案:后来查阅了JDK发现不能那么用,改为直接输入数字。
- 问题3:冒泡排序,持续报错,尝试了三四个小时,很多很多很多很多种方法都不可行
- 问题3解决方案:找老师寻求帮助,但是老师不理我,改为选择排序。

 

## 其他(感悟、思考等)

没什么感悟,Android studio真不是人写的,哭得好大声。。。。

信女愿一生荤素搭配,祈求Java书上的知识能全部飞进自己的大脑。

 

 

 

posted @ 2021-11-07 21:37  王梦欣  阅读(16)  评论(0编辑  收藏  举报