# 20202324 2021-2022-1 data structure and object oriented programming experiment 4 report
Course: programming and data structure
Class: two thousand and twenty-three
Name: Xiao Zhiyu
Student No.: 20202324
Experimental teacher: Wang Zhiqiang
Experiment date: October 28, 2021
Compulsory / elective: compulsory
1, Experimental content
1. Linked list exercise requires the following functions:
- Input some integers through the keyboard to establish a linked list;
These numbers are the two digits taken out in turn from your student number. Plus today's time.
For example, your student number is 20172301
Today's time is October 1, 2018, 16:23:49 seconds
Numbers are
20, 17,23,1, 20, 18,10,1,16,23,49
Print all linked list elements and output the total number of elements.
In your program, please use a special variable name to record the total number of elements. The variable name is your name. For example, if your name is Zhang San, the variable name is
int nZhangSan = 0; // Initialize to 0
After this step, check your program into source code control (git push).
2. Linked list exercises are required to achieve the following functions:
- Implement node insertion, deletion and output operations;
Continue with your previous program and expand its functions. Every time you finish a new function or write more than 10 lines of new code, check in the code and submit it to the source code server;
Read a file from disk. The file has two numbers.
Read the number 1 from the file, insert it into the fifth digit of the linked list, and print all numbers and the total number of elements. Keep the linked list and continue with the following operations.
Read the number 2 from the file, insert it into position 0 of the linked list, and print all numbers and the total number of elements. Keep the linked list and continue with the following operations.
Delete the number 1 from the linked list and print the total number of all numbers and elements.
3. Linked list exercises are required to achieve the following functions:
- Use bubble sorting method or selective sorting method to sort the linked list according to the value size;
If your student number is singular, select bubble sort, otherwise select sort.
In each round of sorting, print the total number of elements and all elements of the current linked list.
Continue to expand in the program obtained in (2), and write different functions with the same program file to realize this function. Still use nZhangSan (your name) to represent the total number of elements.
4. Implement experiments (1) and (2) on android
5. Experiment on android platform (3)
2, Experimental process and results
1. Linked list exercise requires the following functions:
- Input some integers through the keyboard to establish a linked list;
These numbers are the two digits taken from your student number in turn. Add today's time.
For example, your student number is 20172301
Today's time is October 1, 2018, 16:23:49 seconds
Numbers are
20, 17,23,1, 20, 18,10,1,16,23,49
Print all linked list elements and output the total number of elements.
In your program, please use a special variable name to record the total number of elements. The variable name is your name. For example, if your name is Zhang San, then the variable name is
int nZhangSan = 0; / / initialize to 0
After this step, check your program into source code control (git push).
Experimental screenshot
2. Linked list exercises are required to achieve the following functions:
- Implement node insertion, deletion and output operations;
Continue with your previous program and expand its functions. Every time you finish a new function or write more than 10 lines of new code, check in the code and submit it to the source code server;
Read a file from disk. The file has two numbers.
Read the number 1 from the file, insert it into the fifth position of the linked list, and print all numbers and the total number of elements. Keep the linked list and continue the following operation.
Read the number 2 from the file, insert it into position 0 of the linked list, and print all numbers and the total number of elements. Keep the linked list and continue the following operation.
Delete the number 1 from the linked list and print the total number of all numbers and elements.
3. Linked list exercises are required to achieve the following functions:
- Use bubble sorting method or selective sorting method to sort the linked list according to the value size
If your student number is singular, select bubble sort, otherwise select sort.
In each round of sorting, print the total number of elements and all elements of the current linked list.
Continue to expand the program obtained in (2) and write different functions with the same program file to realize this function. Still use nZhangSan (your name) to represent the total number of elements.
Experimental screenshot
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 //Enter and create the header first 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 //Enter and connect until you enter“ 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")){ //Prevent error caused by input string of integer linked list 23 break; 24 } 25 Chain chain = new Chain(Integer.parseInt(str)); 26 connect(head, chain); 27 }while (!str.equals("stop")); 28 29 //delete 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 //insert 38 System.out.println("Insert?(y/n)"); 39 if(scan.next().equals("y")){ //Not here next.Line,Otherwise, it will read the rest of the linked list 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 //output 49 PrintLinkedList(head); 50 51 //1.File creation (file class instantiation) 52 File file = new File("D:\\test6","Chain.txt"); 53 //2.File read and write, byte stream read and write, write first and then read 54 OutputStream outputStream = new FileOutputStream(file); 55 byte[] insert2 = { 11 , 13 }; //Define byte stream array 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){ //Read and store byte stream with array 62 num[by] = inputStream.read(); 63 by++; 64 } 65 outputStream.close(); 66 inputStream.close(); //end 67 68 //Output file content 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 //Insert the first number of the file into the fifth digit of the linked list,And output 76 insert(head, num[0], 5); 77 PrintLinkedList(head); 78 //Insert the second number of the file into position 0 of the linked list,And output 79 insert(head, num[1], 1); 80 PrintLinkedList(head); 81 //Delete the first number of the file from the linked list,And output 82 delete(head, 6); 83 PrintLinkedList(head); 84 85 rank(); 86 PrintLinkedList(head); 87 } 88 89 //Connection between linked lists, tail interpolation, traversal 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 //Static method for output 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) { //Delete header node 114 Chain_2.head = head.getNext(); 115 } 116 else { //Traverse to the node to delete 117 for(int i = 1; i < node - 1; i++){ 118 temp = temp.getNext(); 119 } 120 } 121 122 temp.setNext(temp.getNext().getNext()); //Skip this node to delete it 123 nXiaoZhiYv--; //Total minus 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; //Double pointer for sorting 147 int t , w = 1, e = 1; //t The maximum value after storing the current position of the linked list, w Record the pointer position, e Record the position of the number of duplicates after sorting 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); //Put the maximum value in the header 160 delete(head, e); //Number of remaining after deleting the move 161 PrintLinkedList(head); 162 w = i + 1; 163 if(e <= w) temp = temp.getNext(); //Difficulties are encountered here. The problem is that after the following numbers are inserted into the current position, temp Has been moved back one bit,So add if 164 temp1 = temp; 165 if(temp1.getNext() == null) break; //Prevention of cross-border 166 } 167 } 168 }
↑ is all the codes of the first few steps of Experiment 6,
Fortunately, all the experimental results were right
Congratulations! Congratulations
4. Implement experiments (1) and (2) on android
5. Experiment on android platform (3)
I'm working on it. I'll get it out right away...
3, Problems and solutions in the experiment
-Question 1: the Android programming platform is crazy about reporting errors, one step at a time, or even two or three at a time. It's terrible
-Solution to the problem: Baidu, Baidu, or * * Baidu, csdn contracted my solution to report errors at every step
-Question 2: in the linked list
-Problem solution: use String a = "" + t (user-defined variable) inside the method to convert the user-defined variable to String type,
4, Experimental feeling
It's still very difficult. The Android platform has a better mentality than the previous server, and the online tutorials are not completely right. I just stubbornly did it bit by bit. I thought I wouldn't be able to program anything now. As a result, I still thought about the experimental topic for a long time, and then compiled it slowly. This experiment hasn't been completed, and the next experiment has been released, Tired, destroy it. The next experiment should be simpler than this, but why is there an Android platform? Tired, that's it