20172333 2017-2018-2 "program design and Data Structure" experiment 1 report
Program: Program design and data structure
Class: 1723
Name: King
Study No.: 20172333
Lab Teacher: Wang Zhiqiang
Date of experiment: September 30, 2018
Compulsory/elective: compulsory
1. Experimental Content Task 1:
- List exercise, which requires the following features to be implemented:
- (1) Enter some integers through the keyboard to create a linked list; these numbers are the two digits that you take out of your study number, plus the time of day.
- For example, your school number is 20172301.
- Today's time is 2018/10/1, 16:23:49 seconds.
- Numbers are: 20, 17,23,1, 20, 18,10,1,16,23,49
- Prints all the linked list elements and outputs 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, your name is Zhang San, then this variable name is: int nzhangsan = 0; Initialized to 0.
- After this step, check your program into source code control (git push).
Task 2:
- List exercise, which requires the following features to be implemented:
- (2) Implement node Insert, delete, output operation
- Continue your previous program, expand its functionality, each to complete a new feature, or write more than 10 lines of new code, check in code, submit to the source code server;
- Read a file from disk, this file has two numbers.
- Reads the number 1 from the file, inserts the 5th bit into the list, and prints all the numbers, and the total number of elements. Keep the list, and continue with the following operations.
- Reads the number 2 from the file, inserts the No. 0 bit into the list, and prints all the numbers, and the total number of elements. Keep this list and continue with the following operations.
- Remove the number 1 from the list just now. and prints the total number of all numbers and elements.
- Check in all code.
Task 3:
- List exercise, which requires the following features to be implemented:
- (3) The list is sorted according to the numerical size by using the bubble sort method or selecting the sorting method;
- If you are an odd number, choose bubble sort, otherwise choose sort.
- In each round of the sort, the total number of printed elements, and all elements of the current linked list.
- In (2) The resulting program continues to expand, using the same program file, write different functions to achieve this function. Still use Nzhangsan (your name) to represent the total number of elements.
Task 4:
- Array exercises, which require the following functions to be implemented:
- (1) Enter some integers through the keyboard to create a linked list;
- These numbers are the two digits that you take out of your study number in turn. Plus today's time, for example your school number is 20172301, today is 2018/10/1, 16:23:49 seconds,
- The numbers are 20, 17,23,1, 20, 18,10,1,16,23,49.
- Prints all the array elements and outputs 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 you call Zhang San, then this variable name is int nzhangsan = 0; Initialized to 0.
- After this step, check your program into source code control (git push).
- (2) Implementation of node insertion, deletion, output operation;
- Continue your previous program, expand its functionality, each to complete a new feature, or write more than 10 lines of new code, check in code, submit to the source code server;
- Read a file from disk, this file has two numbers.
- Reads the number 1 from the file, inserts the 5th bit into the array, and prints all the numbers, and the total number of elements. Keep this array and continue with the following operation.
- Reads the number 2 from the file, inserts the No. 0 bit into the array, and prints all the numbers, and the total number of elements. Keep this array, and continue with the following operation.
- Remove the number 1 from the array just now. and prints the total number of all numbers and elements.
- Check in all code.
Task 5:
- Array exercises, which require the following functions to be implemented:
- (3) Using the bubble sorting method or choosing the sorting method to sort the array according to the numerical size;
- If you are an odd number, choose Sort by selection, otherwise choose bubble sort.
- In each round of the sort, the total number of printed elements, and all elements of the current array.
- In (2) The resulting program continues to expand, using the same program file, write different functions to achieve this function. Still use Nzhangsan (your name) to represent the total number of elements.
2. Experimental process and results (altogether five experiments. )-First commit point
process: The first task is simply to set up a list, use the scanner class to get integers from the keyboard to create the list, and then because you want to not swap the input integer data, I need to write a post-interpolation method:
public void add(int num) { node n=new node(num); node p =head; if (head ==null) { n.next=head; head =n; }else { while(p.next!=null) { p=p.next; } p.next=n; } nYanyujun++; }
Finally, the output list and the number of linked lists are required
public String toString() { String result =""; node tem = head; while(tem!=null) { result+= tem.data +" "; tem =tem.next; } return result; } public int size() { return nYanyujun; }
Figure 1 Results
-Second submission Point
Process: The task is to write three methods (post-insert output, pre-plug output, delete output) just start writing the insertion method when I saw the request, I thought only two targets, head and ordinary insertion confused, resulting in the use of ordinary insertion to achieve the head plug, plug in. Head Interpolation method:
public void addHeadNode(int data) { node x=new node(data); if(head==null){ head =x;nYanyujun++; return; } x.next=head; head=x; nYanyujun++;
Insert Method:
public void insert(int index,int data) { node n=new node(data); node cur =head; node pre=head; int pos =0; while (pos!=index-1) { pre=cur; cur=cur.next; pos++; } n.next=cur; pre.next=n; nYanyujun++; }
Delete method:
public Boolean deleteNode(int index) { if(index<1||index>nYanyujun) return false; int i=1; node p=head; while(i!=(index-1)) { p=p.next; i++; } (p.next)=(p.next.next); nYanyujun--; return true; }
Of course, there is the process of reading data from the file, this code to tell the truth I do not understand, directly in the last semester of the code to take a few changes, and tested a bit to use.
Read Data
try {if (!file.exists ()) {file.createnewfile (); }} catch (IOException Exception) {System.out.println ("error, specified path does not exist"); } inputstream inputStream1 = new FileInputStream (file); while (inputstream1.available () > 0) {System.out.println (char) inputstream1.read () + ""); } inputstream1.close (); byte[] buffer = new byte[1024]; String content = ""; int flag = 0; InputStream inputStream2 = new FileInputStream (file); Bufferedinputstream Bufferedinputstream = new Bufferedinputstream (INPUTSTREAM2); while (flag = bufferedinputstream.read (buffer))! =-1) {content + = new String (buffer, 0, flag); } bufferedinputstream.close (); String[] A = new string[10]; A = Content.split (""); String B = a[0]; String C = a[1];//int i = integer.valueof (B). Intvalue ();//INT II = integer.valueof (C). IntvAlue ();
Results Figure 2
-A third submission point
The third task of the process needs to use bubbling or select sort, bubble sort seems to have not learned, but because the requirements of the number is the singular to use bubbles, you can only learn from the online learning to write a function seemingly the same bubble sort, and then the problem, the topic required to be each time the results output, I write the bubble sort is no problem, but in the number of output elements is always 0, and then the first day of the national Day I changed two a child without fruit, mentality and explosion, and then think about anyway, the number of the whole has not changed directly in the number of elements manually written. Bubble sort
public static node bubbleSort(node head){ shiyan shiiyan =new shiyan(); if(head == null || head.next == null) return head; node cur = null, tail = null; cur = head; while(cur.next != tail){ while(cur.next != tail){ if(cur.data > cur.next.data){ int tmp = cur.data; cur.data = cur.next.data; cur.next.data = tmp; } cur = cur.next; } System.out.println("排序后结果:"+shiiyan.toString()); System.out.println("元素数量:12" ); tail = cur; cur = head; } return head; }
Results Figure 3
Top three experiment submission diagrams
-Fourth Submission Point
process This task and the first three tasks need to be carried out in another Java program, in fact, the approximate content is roughly the same as the array to complete the previous task (set up an array, the contents of the array, the number of elements of the array, insert, delete)
Display content Methods
public void tostring(String[]a) { System.out.print("整个数组是:"); for (int k = 0; k<=a.length-1; k++) { System.out.printf(a[k] +" "); nYanYuJun =k+1; } }
Number of array elements
public int size() { return nYanYuJun; }
Array Insertion method
public void insert(int index, String num) { String[] result = new String[nYanYuJun + 1]; if (index == 0) { result[0] = num; for (int a = 0; a < nYanYuJun; a++) { result[a + 1] = K[a]; } } else { if (index == nYanYuJun) { for (int a = 0; a < nYanYuJun; a++) { result[a] = K[a]; } result[nYanYuJun + 1] = num; } else { result[index] = num; for (int a = 0; a < nYanYuJun; a++) { if (a < index) result[a] = K[a]; else result[a + 1] = K[a]; } } } K = result; }
Array Delete method
protected void Delete(int index) { String[] L = new String[nYanYuJun - 1]; if (index == 0) { for (int a = 0; a < nYanYuJun - 1; a++) { L[a] = K[a + 1]; } } else { if (index == nYanYuJun - 1) { for (int a = 0; a < nYanYuJun - 1; a++) { L[a] = K[a]; } } else { for (int a = 0; a < nYanYuJun - 1; a++) { if (a < index) L[a] = K[a]; else L[a] = K[a + 1]; } } } K = L; nYanYuJun--; }
Experimental Results 4
-Fifth Submission Point
Process This task is the implementation of the sorting method of the array, because before I in the row, sorting method to use an int array, finally I only have a string array, need to be converted, and then I asked the classmate, together to get a conversion method so my sorting method will have two. The method name of sort is the last one to use.
public static void sort(int[] arr) { int index; for(int i=1;i<arr.length;i++){ index=0; for(int j=1;j<=arr.length-i;j++){ if(arr[j]>arr[index]){ //查找最大值 index=j; } } //交换在arr.length-i和index(最大值)位置的两个数 int temp=arr[arr.length-i]; arr[arr.length-i]=arr[index]; arr[index]=temp; } for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } } public void Sort() { int[] A = new int[nYanYuJun]; for (int i = 0; i < nYanYuJun; i++) { A[i] = Integer.valueOf(K[i]).intValue(); } shiyan3_4.sort(A); }
Experimental results Figure 5
Upload History of Experiment 4-5
3. Problems encountered during the experiment and the process of solving them
Question 1: In the experimental sequencing, the final output of the sorting result is not a problem, but the intermediate process each step output is simply exploded, just beginning is all the previous sort content is all 0, the number of elements no change diagram.
FIX: Try to modify in the loop statement, the specific code has a reference on the network other People's circular statement, changed to the second appearance, and then the ordering content can be carried out every time, but its presentation process is a bit strange, according to the truth should be every step after the change, But I changed it after the show is each step to move those elements to delete, in the next step to remove the previous steps to put in the back, the next step should be taken to compare the number of deleted, resulting in the number of each step is not the same.
Question 2: The first problem continues, with the problem of sorting content 0 being resolved and the process of presenting the wrong one.
Solution: Because of the bubble sort is extremely unfamiliar, once deleted all methods, and then see the online bubble sort of method, feel more than the other bubble sort of simple, try to use it to each step to display the kind of method, the result is better, got the effect I want, The problem comes out again. The number of elements that come out every time is always 0.
Experimental Code Cloud Address
Others (sentiment, thinking, etc.)
This is my first day in the national day began to do homework, feel OK, from one o'clock in the afternoon to do, to the night eight o'clock back. The concept of a linked list is much clearer, but sometimes it feels a little slow, and the way to do it is not handy.
And the most important thing is that the code cloud manually dragged into the upload file is really real drip easy to use, garbage git push/pull
Resources
Java Cryptography algorithm
20172333 2017-2018-2 "program design and Data Structure" experiment 1 report