20202312郭威 實驗五和六《線性結構之連結串列》實驗報告
20202312 2021-2022-1《資料結構與面向物件程式設計》實驗五和六報告
課程:《程式設計與資料結構》
班級: 2023
姓名: 郭威
學號:20202312
實驗教師:王志強
實驗日期:2021年11月4日
必修/選修: 必修
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.通過鍵盤輸入一些整數,建立一個連結串列;這些數是你學號中依次取出的兩位數。 再加上今天的時間。
package Week9;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyLinkedTest {
public static void main(String[] args) throws IOException {
String id,time, total;
Num[] num =new Num[15];
total=getTotal();
int i=0;
for(int j=0;j<total.length();i++,j+=2){
num[i]=new Num((Integer.parseInt(total.substring(j,j+2))));
}
for(int t=0;t<i;t++) {
num[t].next=num[t+1];
}
Print(num[0]);
}
public static String getTotal(){
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date=new Date();
String id,time, total;
Scanner scan=new Scanner(System.in);
System.out.print("請輸入學號:");
id=scan.next();
time=dateFormat.format(date);
total=id;
System.out.println("學號:"+id+" "+"時間:"+time);
StringTokenizer m=new StringTokenizer(time,"/: ",false);
while(m.hasMoreTokens())
{total+=m.nextToken();}
return total;
}
public static void Print(Num head){
Num point=head;
System.out.print("[");
while(point!=null){//遍歷列表!!!
System.out.print(point.num+" ");
point=point.next;
}
System.out.print("]");
System.out.println("元素總數:"+Count(head));
}
public static int Count(Num head){
Num point=head;
int guowei=0;
while(point!=null){
point=point.next;
guowei++;
}
return guowei;
}
}
2.實現節點插入、刪除、輸出操作;
繼續你上一個程式, 擴充套件它的功能,每做完一個新功能,或者寫了超過10行新程式碼,就簽入程式碼,提交到原始碼伺服器;
從磁碟讀取一個檔案, 這個檔案有兩個數字。
package Week9;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyLinkedTest {
public static void main(String[] args) throws IOException {
String id,time, total;
Num[] num =new Num[15];
total=getTotal();
int i=0;
File file=new File("C:\\Users\\86139\\IdeaProjects\\wuxinxin\\src","myfile");
FileWriter fw=new FileWriter("myfile",true);
fw.write("12");
fw.close();
FileReader fr=new FileReader("myfile");
char [] a=new char[10];
fr.read(a);
for(int j=0;j<total.length();i++,j+=2){
num[i]=new Num((Integer.parseInt(total.substring(j,j+2))));
}
for(int t=0;t<i;t++) {
num[t].next=num[t+1];
}
Print(num[0]);
Num insert1=new Num(a[0]-'0');
Num insert2=new Num(a[1]-'0');
num[0]=Insert(num[0],insert1,5);
System.out.println("加入數字1:");
Print(num[0]);
System.out.println("加入數字2:");
num[0]=Insert(num[0],insert2,1);
Print(num[0]);
Delete(num[0],insert1);
System.out.println("刪除數字1:");
Print(num[0]);
}
public static String getTotal(){
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date=new Date();
String id,time, total;
Scanner scan=new Scanner(System.in);
System.out.print("請輸入學號:");
id=scan.next();
time=dateFormat.format(date);
total=id;
System.out.println("學號:"+id+" "+"時間:"+time);
StringTokenizer m=new StringTokenizer(time,"/: ",false);
while(m.hasMoreTokens())
{total+=m.nextToken();}
return total;
}
public static void Print(Num head){
Num point=head;
System.out.print("[");
while(point!=null){//遍歷列表!!!
System.out.print(point.num+" ");
point=point.next;
}
System.out.print("]");
System.out.println("元素總數:"+Count(head));
}
public static int Count(Num head){
Num point=head;
int guowei=0;
while(point!=null){
point=point.next;
guowei++;
}
return guowei;
}
public static Num Insert(Num head,Num insert,int location){
Num point=head;
int i=1;
if(location>1) {
while (i < location - 1) {
point = point.next;
i++;
}
insert.next = point.next;
point.next = insert;
}
else {
insert.next=point;
head=insert;
}
return head;
}
public static void Delete(Num head,Num delete){
Num point=head;
while(point.next!=delete){
point=point.next;
}
point.next=delete.next;
}
}
3.使用氣泡排序法或者選擇排序法根據數值大小對連結串列進行排序;
如果你學號是單數, 選擇氣泡排序, 否則選擇選擇排序。(選擇排序)
package Week9;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyLinkedTest {
public static void main(String[] args) throws IOException {
String id,time, total;
Num[] num =new Num[15];
total=getTotal();
int i=0;
File file=new File("C:\\Users\\86139\\IdeaProjects\\wuxinxin\\src","myfile");
FileWriter fw=new FileWriter("myfile",true);
fw.write("12");
fw.close();
FileReader fr=new FileReader("myfile");
char [] a=new char[10];
fr.read(a);
for(int j=0;j<total.length();i++,j+=2){
num[i]=new Num((Integer.parseInt(total.substring(j,j+2))));
}
for(int t=0;t<i;t++) {
num[t].next=num[t+1];
}
Print(num[0]);
Num insert1=new Num(a[0]-'0');
Num insert2=new Num(a[1]-'0');
num[0]=Insert(num[0],insert1,5);
System.out.println("加入數字1:");
Print(num[0]);
System.out.println("加入數字2:");
num[0]=Insert(num[0],insert2,1);
Print(num[0]);
Delete(num[0],insert1);
System.out.println("刪除數字1:");
Print(num[0]);
System.out.println("排序:");
Sequence(num[0]);
}
public static String getTotal(){
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date=new Date();
String id,time, total;
Scanner scan=new Scanner(System.in);
System.out.print("請輸入學號:");
id=scan.next();
time=dateFormat.format(date);
total=id;
System.out.println("學號:"+id+" "+"時間:"+time);
StringTokenizer m=new StringTokenizer(time,"/: ",false);
while(m.hasMoreTokens())
{total+=m.nextToken();}
return total;
}
public static void Print(Num head){
Num point=head;
System.out.print("[");
while(point!=null){//遍歷列表!!!
System.out.print(point.num+" ");
point=point.next;
}
System.out.print("]");
System.out.println("元素總數:"+Count(head));
}
public static int Count(Num head){
Num point=head;
int guowei=0;
while(point!=null){
point=point.next;
guowei++;
}
return guowei;
}
public static Num Insert(Num head,Num insert,int location){
Num point=head;
int i=1;
if(location>1) {
while (i < location - 1) {
point = point.next;
i++;
}
insert.next = point.next;
point.next = insert;
}
else {
insert.next=point;
head=insert;
}
return head;
}
public static void Delete(Num head,Num delete){
Num point=head;
while(point.next!=delete){
point=point.next;
}
point.next=delete.next;
}
public static void Sequence(Num head){
Num a=head;
while(a.next!=null){
Num c=a;
Num b=a.next;
while(b!=null){
if (b.num > c.num) {
c=b;
}
b=b.next;
}
int temp;
temp=c.num;
c.num=a.num;
a.num=temp;
Print(head);
a=a.next;
}
}
4.在android上實現實驗(1)和(2)
5.在android平臺上實現實驗(3)
3.感悟
Android studio上依然有很多自己不會,不理解的東西,以後應當加強對其的學習。
## 參考資料
- [《Java和Andriod開發學習指南(第二版)人民郵電出版社》]
- [《Java軟體結構與資料結構(第三版)清華大學出版社》]