20172318 2017-2018-2 "program design and Data Structure" experiment 4 Report
Program: Program design and data structure
Class: 1723
Name: Ludasui
Study No.: 20172318
Lab Teacher: Wang Zhiqiang
Date of experiment: September 30, 2018
Compulsory/elective: compulsory
1. Experimental content
- The
-
List exercise requires the following functions:
(1) Enter some integers through the keyboard to create a linked list (1 points);
These numbers are the two digits you take out of your study number. Plus today's time.
For example your school number is 20172301
today is 2018/10/1, 16:23:49 seconds
number is
, 17,23,1, 18,10,1,16,23,49
Print all linked list elements, and output the total number of elements.
in your program, use a special variable name to record the total number of elements, and the variable name is your name. For example, if your name is Zhang San, then this variable name is
int nzhangsan = 0;//initialized to 0.
(2) Implement node Insert, delete, output operation
Continue your last program, expand its function, each to complete a new function, or write more than 10 lines of new code, check in code, submit to the source code server;
reads a file from disk with two numbers. The
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. The
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. The
removes just the number from the list 1. and prints the total number of all numbers and elements.
(3) Use the bubble Sort method or select the sorting method to sort the linked list according to the numerical size (2 points);
If you are the odd number, choose bubble sort, otherwise choose sort. The
prints the total number of elements in each round of the sort, and all elements of the current list.
Array exercises, which require the following functions to be implemented:
(1) Enter some integers through the keyboard to create a linked list (1 points);
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'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 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 your name is Zhang San, then this variable name is
int Nzhangsan = 0; Initialized to 0.
(2) Implementation of node insertion, deletion, output operation (2 points, 3 points of knowledge according to the actual situation, as appropriate deduction);
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.
(3) Using the bubble sorting method or selecting the sorting method according to the numerical size of the array to sort (2 points);
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.
2. Experimental process and results the first part is to enter some integers through the keyboard to create a linked list
exp1 exp1 = new exp1(); Scanner scanner = new Scanner(System.in); System.out.println("输入学号、日期、时间"); String string = scanner.nextLine(); StringTokenizer stringTokenizer = new StringTokenizer(string); for (int i = 0; i < string.length(); i++) { while (stringTokenizer.hasMoreTokens()) { String f = stringTokenizer.nextToken(); int num = Integer.parseInt(f); exp1.add(num); } } System.out.println("打印所有元素:" + exp1.toString() + "元素总数:" + exp1.Size());
Part II
File file = new File("D:\\shuzi.txt"); FileReader fileReader = null; try { fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String str = ""; str = bufferedReader.readLine(); String[] strings = str.split(" "); int num1=Integer.parseInt(strings[0]); int num2=Integer.parseInt(strings[1]); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
- Insert, delete, export operations
public void insert( int data, int index) { NumberNode node = new NumberNode(data); NumberNode current = list; NumberNode pre = list; while (t != index) { pre = current; current = current.next; t++; } node.next = current; pre.next = node; t = 0; nLudayue++; }
public void delete(int num){ NumberNode node = new NumberNode(num); NumberNode current, temp; if(list.Num == num) { current = list.next; list = current; } else{ current = list; while(current.next.Num != num) current = current.next; temp = current.next.next; current.next = temp; } nLudayue --; }
public String toString(){ String result = ""; NumberNode current = list; while (current != null){ result += current.Num + " "; current = current.next; } return result; }
Part Three Select sort
public void Selection(){ NumberNode current; current = list; int[] A = new int[nLudayue]; for(int i = 0; i < nLudayue; i++) { A[i] = current.Num; current = current.next; } int[] B = selectionSort(A); list = null; int top2 = nLudayue; for(int i =0;i< top2; i++){ int num = B[i]; add(num); } }
public int[] selectionSort(int[] list) { int min; int temp; for (int index = 0; index < list.length - 1; index++) { min = index; for (int scan = index + 1; scan < list.length; scan++) if (list[scan] - (list[min]) < 0) min = scan; temp = list[min]; list[min] = list[index]; list[index] = temp; } return list; }
Part IV implements INSERT, delete, and output operations with arrays
- Insert, delete, export operations
public void insert(int num ,int index){ int[] number = Number; if(index != 0){ for(int i = nLudayue ;i>= index ;i--) { number[i]=number[i-1]; } number[index-1]= num; } else { for (int i = nLudayue;i > index;i--) number[i] = number[i-1]; number[0] = num; } nLudayue++; }
public void delete(int num){ int[] number = Number; int index = 0; while (number[index]!=num) { index++; } for (int i = index +1; i< nLudayue; i++) { number[i-1] = number[i]; } nLudayue--; }
public String toString() { String result = ""; for(int i = 0;i<nLudayue;i++) result += Number[i] + " "; return result; }
Part Five bubble sort
public void Sort() { int[] number = Number; for (int i = 0; i < nLudayue; i++) { for (int j = 0; j < nLudayue - i - 1; j++) { if (number[j] > number[j + 1]) { int temp = number[j+1]; number[j+1] = number[j]; number[j] = temp; } } } }
3. Problems encountered during the experiment and the process of solving them
Question 1:
There was an issue where 8 digits were entered and the first four were read together.
Problem 1 Solution: I later found out that I wrote 20172318 and then separated them by a space.
Others (sentiment, thinking, etc.)
This experiment not only need to use linked lists and arrays, but also to use some time to review the file input and reading knowledge, test our ability to master the knowledge of many aspects
Resources
- Java software Architecture and data Structure Tutorial (fourth edition)
20172318 2018-2019-1 "program design and Data Structure" experiment 1 report