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

# 20202324 2021-2022-1 《数据结构与面向对象程序设计》实验四报告

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

一、实验内容

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)

二、实验过程及其结果

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

     

     


     

实验截图

 1 public class Chain {
 2     protected int element;
 3     protected Chain next = null;
 4 
 5     public Chain(int element) {
 6         this.element = element;
 7         this.next = next;
 8     }
 9 
10     public int getElement() {
11         return element;
12     }
13 
14     public void setElement(int element) {
15         this.element = element;
16     }
17 
18     public Chain getNext() {
19         return next;
20     }
21 
22     public void setNext(Chain next) {
23         this.next = next;
24     }
25 }
  1 import java.io.*;
  2 import java.util.Scanner;
  3 
  4 public class Chain_2 {
  5     public static Chain head;
  6     public static int nXiaoZhiYv = 0;
  7 
  8     public static void main(String[] args) throws IOException {
  9         Scanner scan=new Scanner(System.in);
 10 
 11         //先输入并创建表头
 12         System.out.println("Enter numbers("+(nXiaoZhiYv + 1)+").Type stop if you want to stop.");
 13         String str = scan.nextLine();
 14         Chain chain0 = new Chain(Integer.parseInt(str));
 15         head = chain0;
 16 
 17         //一直输入并连接,直到输入“stop”
 18         do{
 19             nXiaoZhiYv++;
 20             System.out.println("Enter numbers("+(nXiaoZhiYv + 1)+").Type stop if you want to stop.");
 21             str = scan.nextLine();
 22             if(str.equals("stop")){     //防止整数型链表输入字符串导致错误
 23                 break;
 24             }
 25             Chain chain = new Chain(Integer.parseInt(str));
 26             connect(head, chain);
 27         }while (!str.equals("stop"));
 28 
 29         //删除
 30         System.out.println("Delete?(y/n)");
 31         if (scan.nextLine().equals("y")){
 32             System.out.println("Please enter the node you want to delete:");
 33             int node = scan.nextInt();
 34             delete(head, node);
 35         }
 36 
 37         //插入
 38         System.out.println("Insert?(y/n)");
 39         if(scan.next().equals("y")){                      //此处不可next.Line,否则会读走剩下的链表
 40             System.out.println("Please enter the location where you want to insert the node:");
 41             int node = scan.nextInt();
 42             System.out.println("Please enter an element for this node:");
 43             int element = scan.nextInt();
 44             insert(head, element, node);
 45         }
 46 
 47 
 48         //输出
 49         PrintLinkedList(head);
 50 
 51         //1.文件创建(文件类实例化)
 52         File file = new File("D:\\test6","Chain.txt");
 53         //2.文件的读写,字节流读写,先写后读
 54         OutputStream outputStream = new FileOutputStream(file);
 55         byte[] insert2 = { 11 , 13 };                //定义字节流数组
 56         outputStream.write(insert2);
 57 
 58         InputStream inputStream = new FileInputStream(file);
 59         int[] num = new int[2];
 60         int by = 0;
 61         while (inputStream.available() > 0){                 //读取,用数组存储字节流
 62             num[by] = inputStream.read();
 63             by++;
 64         }
 65         outputStream.close();
 66         inputStream.close();                     //结束
 67 
 68         //输出文件内容
 69         System.out.print("File content is: ");
 70         for(int i = 0; i < 2; i++) {
 71             System.out.print(num[i] + " ");
 72         }
 73         System.out.print("\n");
 74 
 75         //将文件的第一个数字插入到链表第5位,并输出
 76         insert(head, num[0], 5);
 77         PrintLinkedList(head);
 78         //将文件的第二个数字插入到链表第0位,并输出
 79         insert(head, num[1], 1);
 80         PrintLinkedList(head);
 81         //将文件的第一个数字从链表中删除,并输出
 82         delete(head, 6);
 83         PrintLinkedList(head);
 84 
 85         rank();
 86         PrintLinkedList(head);
 87     }
 88 
 89     //链表之间的连接,尾插法,遍历
 90     public static void connect(Chain head, Chain chain2){
 91         Chain head2 = head;
 92         while (head2.getNext() != null){
 93             head2 = head2.getNext();
 94         }
 95         head2.setNext(chain2);
 96     }
 97 
 98     //用来输出的静态方法
 99     public static void PrintLinkedList(Chain head){
100         Chain temp = head;
101         String list = "";
102         while (temp != null){
103             list = list + " " + temp.getElement();
104             temp = temp.getNext();
105         }
106         System.out.println(list);
107         System.out.println("Total number of elements:" + nXiaoZhiYv);
108     }
109 
110     public static void delete(Chain head, int node){
111         Chain temp = head;
112 
113         if(node == 1) {                //删除头节点
114             Chain_2.head = head.getNext();
115         }
116         else {                          //遍历至要删除的节点
117             for(int i = 1; i < node - 1; i++){
118                 temp = temp.getNext();
119             }
120         }
121 
122         temp.setNext(temp.getNext().getNext());    //跳过该节点,达到删除的目的
123         nXiaoZhiYv--;                              //总数减1
124     }
125 
126     public static void insert(Chain head, int element, int node){
127         Chain insert = new Chain(element);
128         Chain temp = head;
129 
130         if(node == 1){
131             insert.setNext(head);
132             Chain_2.head = insert;
133         }
134         else {
135             for(int i = 0; i < node - 2; i++){
136                 temp = temp.getNext();
137             }
138             insert.setNext(temp.getNext());
139             temp.setNext(insert);
140         }
141         nXiaoZhiYv++;
142     }
143 
144     public static void rank(){
145         Chain temp = Chain_2.head;
146         Chain temp1 = Chain_2.head;              //双指针用于排序
147         int t , w = 1, e = 1;            //t储存链表当前位置后的最大值,w记录指针位置,e记录排序后重复的数的位置
148 
149         for(int i = 1; i < nXiaoZhiYv; i++){
150             t = temp.getElement();
151             while (temp1.getNext() != null){
152                 temp1 = temp1.getNext();
153                 w++;
154                 if(temp1.getElement() > t){
155                     t = temp1.getElement();
156                     e = w + 1;
157                 }
158             }
159             insert(head, t, i);        //将最大值放在表头
160             delete(head, e);           //删除移动后残留的数
161             PrintLinkedList(head);
162             w = i + 1;
163             if(e <= w) temp = temp.getNext();          //此处遇到困难,问题出在后面的数插到当前位置后,temp已经后移一位,所以要加if
164             temp1 = temp;
165             if(temp1.getNext() == null) break;         //防止越界
166         }
167     }
168 }

↑是实验六前几步的全部代码,

所幸的是所有的实验结果都能跟预期对的上

可喜可贺可喜可贺

 

 

 

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

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

正在弄,马上搞出来。。。

 

三. 实验过程中遇到的问题和解决过程
- 问题1:安卓编程平台疯狂报错,一步一错甚至一步两三错,折磨惨了
- 问题解决方案: 百度,百度,还是**的百度,csdn承包了我每一步报错的解决方法

- 问题2:链表中的
- 问题解决方案: 通过在方法内部使用String a = “”+t(自定义变量)来实现将自定义的变量转换为String类型,

 

四.实验感想

还是好难啊,安卓平台比上一个服务器还搞心态,网上的教程也不完全都是对的,就硬着头皮一点一点的搞,本来以为学到现在应该不会出现编个程序啥都不会的情况,结果还是对着实验题目想很久,才慢慢悠悠编出来,这个实验都还没搞完下个实验又发布了,累了,毁灭吧,看着下个实验应该比这个简单点,但为什么还有安卓平台啊,累了,就这样吧

 

posted @ 2021-11-04 17:44  薮猫多聚体  阅读(72)  评论(0编辑  收藏  举报