# 20192307 2020-2021-1 《数据结构与面向对象程序设计》实验六报告
20192307 2020-2021-1 《数据结构与面向对象程序设计》实验六报告
- 课程:《数据结构与面向对象程序设计》
- 班级: 1923
- 姓名: 常万里
- 学号: 20192307
- 实验教师:王志强老师
- 实验日期:2020年11月5日
- 必修/选修: 必修
一、实验内容
- 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)
二、实验过程及结果
(一)链表练习
package NEB93;
import NBE92.Student;
import java.util.Scanner;
/**
* \* Created with IntelliJ IDEA.
* \* User: Shape Of My Heart
* \* Date: 2020/11/5
* \* Time: 18:11
* \* Besides,some of the best things in life are total mistakes.
* \* Description:
* \
**/
public class LinkedListExample51 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number:");
Num a = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num b = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num c = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num d = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num e = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num f = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num g = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num h = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num i = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num j = new Num(in.nextInt());
System.out.println("Please enter a number:");
Num k = new Num(in.nextInt());
Num 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;
// 头插法
// niu.next = head;
// head = niu;
// 尾插法
// Num point = head;
// while (point.next != null) {
// point = point.next;
// }
// point.next = niu;
print(head);
}
public static void print(Num Head) {
int nChangWanLi = 0;
Num point = Head;
// 遍历链表
while (point != null) {
System.out.println("Number: " + point.number);
point = point.next;
nChangWanLi++;
}
System.out.println("Total number of elements: "+nChangWanLi);
}
}
package NEB93;
import java.util.Objects;
/**
* \* Created with IntelliJ IDEA.
* \* User: Shape Of My Heart
* \* Date: 2020/11/5
* \* Time: 21:36
* \* Besides,some of the best things in life are total mistakes.
* \* Description:
* \
**/
public class Num implements Comparable{
protected int number;
protected Num next = null;
public Num(int number) {
this.number = number;
}
public Num(){
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Num)) {
return false;
}
Num num = (Num) o;
return getNumber() == num.getNumber() &&
Objects.equals(next, num.next);
}
@Override
public int hashCode() {
return Objects.hash(getNumber(), next);
}
@Override
public String toString() {
return "Num{" +
"number=" + number +
", next=" + next +
'}';
}
@Override
public int compareTo(Object o) {
return 0;
}
}
运行结果截图
(二)链表练习
package NEB93;
import java.io.*;
import java.util.Scanner;
/**
* \* Created with IntelliJ IDEA.
* \* User: Shape Of My Heart
* \* Date: 2020/11/5
* \* Time: 22:32
* \* Besides,some of the best things in life are total mistakes.
* \* Description:
* \
**/
public class LinkedListExample52 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\我的大学\\学习\\JAVA程序设计", "test.txt");
try {
if (!file.exists()) {
file.createNewFile();
Writer writer = new FileWriter(file, true);
writer.append("12 36");
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]);
// Scanner in = new Scanner(System.in);
// System.out.println("Please enter a number:");
// Num a = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num b = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num c = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num d = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num e = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num f = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num g = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num h = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num i = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num j = new Num(in.nextInt());
// System.out.println("Please enter a number:");
// Num k = new Num(in.nextInt());
// Num 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;
// 头插法
// niu.next = head;
// head = niu;
// 尾插法
// Num point = head;
// while (point.next != null) {
// point = point.next;
// }
// point.next = niu;
// print(head);
inputStream.close();
}
public static void print(Num Head) {
int nChangWanLi = 0;
Num point = Head;
// 遍历链表
while (point != null) {
System.out.println("Number: " + point.number);
point = point.next;
nChangWanLi++;
}
System.out.println("Total number of elements: " + nChangWanLi);
}
}
运行结果截图
(三)链表练习
package NEB93;
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
/**
* \* Created with IntelliJ IDEA.
* \* User: Shape Of My Heart
* \* Date: 2020/11/6
* \* Time: 21:06
* \* Besides,some of the best things in life are total mistakes.
* \* Description:
* \
*
* @author Shape Of My Heart
*/
public class LinkedListExample53 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\我的大学\\学习\\JAVA程序设计", "test1.txt");
try {
if (!file.exists()) {
file.createNewFile();
Writer writer = new FileWriter(file, true);
writer.append("12 36");
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]);
LinkedList<Integer> sites = new LinkedList<Integer>();
Scanner in = new Scanner(System.in);
int i = 0;
while (i < 11) {
System.out.println("Please enter a number:");
sites.add(in.nextInt());
i++;
}
System.out.println(sites);
System.out.println("Number of elements:" + sites.size());
sites.add(4, x1);
System.out.println(sites);
System.out.println("Number of elements:" + sites.size());
sites.addFirst(x2);
System.out.println(sites);
System.out.println("Number of elements:" + sites.size());
sites.remove(5);
System.out.println(sites);
System.out.println("Number of elements:" + sites.size());
for ( i = 1; i < sites.size() - 1; i++) {
for (int j = 0; j < (sites.size() - i ); j++) {
if (sites.get(j + 1) < sites.get(j)) {
sites.add(j,sites.get(j + 1));
sites.remove(j+2);
}
}
}
System.out.println("\nBubble sort:");
System.out.println(sites);
System.out.println("Number of elements:" + sites.size());
}
}
运行结果截图
(四)在android上进行链表练习
linkedlist
package com.example.linkedlist;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editname;
private EditText editdetail;
private EditText linkedlist;
private Button btnsave;
private Button btnclean;
private Button btnread;
private Button btnsort;
private Button btnlist;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
bindViews();
}
private void bindViews() {
editdetail = (EditText) findViewById(R.id.editdetail);
editname = (EditText) findViewById(R.id.editname);
linkedlist = (EditText) findViewById(R.id.linkedlist);
btnclean = (Button) findViewById(R.id.btnclean);
btnsave = (Button) findViewById(R.id.btnsave);
btnread = (Button) findViewById(R.id.btnread);
btnlist = (Button) findViewById(R.id.btnlist);
btnsort = (Button) findViewById(R.id.btnsort);
btnclean.setOnClickListener(this);
btnsave.setOnClickListener(this);
btnread.setOnClickListener(this);
btnlist.setOnClickListener(this);
btnsort.setOnClickListener(this);
}
@Override
public void onClick(View v) {
LinkedList<Integer> sites = new LinkedList<>();
String[] strs;
String[] listdetail;
StringBuilder str1 = new StringBuilder();
switch (v.getId()) {
case R.id.btnclean:
editdetail.setText("");
editname.setText("");
break;
case R.id.btnsave:
FileHelper fHelper = new FileHelper(mContext);
String filename = editname.getText().toString();
String filedetail = editdetail.getText().toString();
try {
fHelper.save(filename, filedetail);
Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnread:
String detail = "";
FileHelper fHelper2 = new FileHelper(getApplicationContext());
try {
String fname = editname.getText().toString();
detail = fHelper2.read(fname);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
break;
case R.id.btnlist:
String detail1 = "";
FileHelper fHelper22 = new FileHelper(getApplicationContext());
try {
String fname = editname.getText().toString();
detail1 = fHelper22.read(fname);
} catch (IOException e) {
e.printStackTrace();
}
strs = detail1.split(" ");
String listdetail1 = linkedlist.getText().toString();
listdetail = listdetail1.split(" ");
for (int size = 11, i = 0; i < size; i++) {
sites.add(Integer.valueOf(listdetail[i]));
}
sites.add(4, Integer.valueOf(strs[0]));
sites.addFirst(Integer.valueOf(strs[1]));
for (int size = sites.size(), i = 0; i < size; i++) {
str1.append(sites.get(i)).append(" ");
}
Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_SHORT).show();
break;
case R.id.btnsort:
String detail2 = "";
FileHelper fHelper4 = new FileHelper(getApplicationContext());
try {
String fname = editname.getText().toString();
detail2 = fHelper4.read(fname);
} catch (IOException e) {
e.printStackTrace();
}
strs = detail2.split(" ");
String listdetail2 = linkedlist.getText().toString();
listdetail = listdetail2.split(" ");
for (int size = 11, i = 0; i < size; i++) {
sites.add(Integer.valueOf(listdetail[i]));
}
sites.add(4, Integer.valueOf(strs[0]));
sites.addFirst(Integer.valueOf(strs[1]));
for (int i = 1; i < sites.size() - 1; i++) {
for (int j = 0; j < (sites.size() - i); j++) {
if (sites.get(j + 1) < sites.get(j)) {
sites.add(j, sites.get(j + 1));
sites.remove(j + 2);
}
}
}
for (int size = sites.size(), i = 0; i < size; i++) {
str1.append(sites.get(i)).append(" ");
}
Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_SHORT).show();
break;
}
}
}
package com.example.linkedlist;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHelper {
private Context mContext;
public FileHelper() {
}
public FileHelper(Context mContext) {
super();
this.mContext = mContext;
}
/*
* 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流
* */
public void save(String filename, String filecontent) throws Exception {
//这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦
FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
output.write(filecontent.getBytes()); //将String字符串以字节流的形式写入到输出流中
output.close(); //关闭输出流
}
/*
* 这里定义的是文件读取的方法
* */
public String read(String filename) throws IOException {
//打开文件输入流
FileInputStream input = mContext.openFileInput(filename);
byte[] temp = new byte[1024];
StringBuilder sb = new StringBuilder("");
int len = 0;
//读取文件内容:
while ((len = input.read(temp)) > 0) {
sb.append(new String(temp, 0, len));
}
//关闭输入流
input.close();
return sb.toString();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/main_background"
tools:context="com.example.linkedlist.MainActivity">
<TextView
android:layout_width="238dp"
android:layout_height="47dp"
android:text="请输入文件名"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColorHint="#673AB7" />
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="59dp"
android:textColorHint="#673AB7"/>
<TextView
android:layout_width="260dp"
android:layout_height="65dp"
android:text="请输入文件内容"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColorHint="#673AB7"/>
<EditText
android:id="@+id/editdetail"
android:layout_width="match_parent"
android:layout_height="52dp"
android:textColorHint="#673AB7"
android:minLines="2" />
<TextView
android:layout_width="260dp"
android:layout_height="65dp"
android:text="请输入链表数据"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColorHint="#673AB7" />
<EditText
android:id="@+id/linkedlist"
android:layout_width="match_parent"
android:layout_height="52dp"
android:minLines="2"
android:textColorHint="#673AB7"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnclean"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColorHint="#673AB7"
android:text="清空" />
<Button
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#673AB7"
android:text="写入" />
<Button
android:id="@+id/btnlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#673AB7"
android:text="链表" />
<Button
android:id="@+id/btnsort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#673AB7"
android:text="排序" />
</LinearLayout>
<Button
android:id="@+id/btnread"
android:layout_width="354dp"
android:layout_height="wrap_content"
android:text="读取文件内容"
android:textColorHint="#673AB7" />
<Button
android:id="@+id/button"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:text="Button"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
运行结果截图
三、实验过程中遇到的问题和解决过程
- 没有遇到大的问题
四、心得体会
- 在这次实验过程中,我遇到了许多问题,其中既有知识上的漏洞,也有不细心导致的马虎,这一切都补充,完善,丰富,扩展了我的计算机知识体系。在不断修复问题的过程中,我使用了很多方式去查询资料,例如:《数据结构与面向对象程序设计》,博客园平台,CSDN平台,码云平台,知乎app,等。进一步熟悉了Android studio这个平台的使用与运行方式,提高了自己自主学习的能力,为我接下来学习数据结构以及JAVA语言程序设计打下了坚实的基础,并在不断探索的过程中逐步提升了自己。