标签:tree 问题 pop The 启示 buffer 自己的 添加 展示
课程名称:《程序设计与数据结构》
学生班级:1723班
学生姓名:唐才铭
学生学号:20172319
实验教师:王志强老师
课程助教:张师瑜学姐、张之睿学长
实验时间:2018年11月04日——2018年11月12日
必修/选修:必修
public void add(int number){
Linked_list_node Node = new Linked_list_node(number);
if (this.head==null){
this.head = Node;
}
else {
this.head.addLinked_list_node(Node);
}
}
System.out.println("实验的第一部分:");
System.out.print("Enter some integers and create a linked list : ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] strings = input.split(" ");
Stack<String> Break_up = new Stack<String>();
for (int i = strings.length; i > 0 ; i--){
Break_up.push(strings[i-1]);
}
System.out.print("The contents of the stack are : ");
System.out.println(Break_up);
Linked_list linked_list = new Linked_list();
linked_list.add(0);
while (!Break_up.empty()) {
int tempelement = Integer.parseInt(Break_up.pop());
linked_list.add(tempelement);
}
int ntangcaiming = 0;
ntangcaiming = linked_list.getCount();
System.out.print("The contents of the queue are : ");
System.out.println(linked_list);
System.out.print("The number of linked elements is : ");
System.out.println(ntangcaiming);
运行结果截图:
为了更好地实现需求,我们在链表前端放入了一个取值为0的节点linked_list.add(0);
,以便于在任何地方都能实现插入删除,而打印时将其隐藏。
public void insert(int index,Linked_list_node node){
if(index < 1||index > getCount() + 1){
System.out.println("Wrong position, cannot insert");
return;
}
int length = 1;
Linked_list_node temp = head;
while(head.next != null)
{
if(index == length++){
node.next = temp.next;
temp.next = node;
return;
}
temp = temp.next;
}
}
public void delete(int index){
if(index < 1 || index > getCount()){
System.out.println("Wrong position, cannot be deleted");
return;
}
int length=1;
Linked_list_node temp = head;
while(temp.next != null){
if(index == length++){
temp.next = temp.next.next;
return;
}
temp = temp.next;
}
}
System.out.println("实验的第二部分:");
try {
File file = new File("D:\\huawei\\Javawindows文件","EXP1-First semester of sophomore.txt");
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(reader);
int[] file_word_temp = new int[2];
String[] file_word = bufferedReader.readLine().split(" ");
file_word_temp[0] = Integer.parseInt(file_word[0]);
file_word_temp[1] = Integer.parseInt(file_word[1]);
Linked_list_node Node_insert1 = new Linked_list_node(file_word_temp[0]);
Linked_list_node Node_insert2 = new Linked_list_node(file_word_temp[1]);
linked_list.insert(5,Node_insert1);
System.out.print("The list after inserting 1 at the fifth position is : ");
System.out.println(linked_list);
System.out.print("The number of linked elements is : ");
ntangcaiming = linked_list.getCount();
System.out.println(ntangcaiming);
linked_list.insert(1,Node_insert2);
System.out.print("The list after inserting 2 at the first position is : ");
System.out.println(linked_list);
ntangcaiming = linked_list.getCount();
System.out.print("The number of linked elements is : ");
System.out.println(ntangcaiming);
System.out.print("The list after deleting the inserted number 1 is : ");
linked_list.delete(6);
System.out.println(linked_list);
ntangcaiming = linked_list.getCount();
System.out.print("The number of linked elements is : ");
System.out.println(ntangcaiming);
运行结果截图:
线性结构之链表(2)
冒泡代码实现如下(有删减):
public void Bubble_sort(Linked_list_node Head,Linked_list linked_list){
Linked_list_node temp = null, tail = null;
temp = head;
int count=1;
while(temp.next != tail){
while(temp.next != tail){
if(temp.number > temp.next.number){
int temp_number = temp.number;
temp.number = temp.next.number;
temp.next.number = temp_number;
System.out.print("The list sorted by the "+ count + " truly bubbling sort is : ");
System.out.println(linked_list);
System.out.print("The number of linked elements is : " + linked_list.getCount() + "\n" );
count++;
}
temp = temp.next;
}
tail = temp;
temp = head;
}
}
System.out.println("实验的第三部分:");
System.out.println("Print only the rounds that have implemented the element exchange:");
linked_list.Bubble_sort(linked_list.head,linked_list);
}
catch (IOException E){
System.out.println("错误,指定路径不存在");
}
运行结果截图(仅仅展示部分截图):
本次提交点相关代码如下:
System.out.println("实验的第一部分:");
System.out.print("Enter some integers and create a linked list:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int ntangcaiming = 0 ;
String[] temp_MyArray = input.split(" ");
Array MyArray = new Array(temp_MyArray);
System.out.print("The elements in the array are: ");
System.out.println(MyArray);
System.out.print("The number of elements in the array is: ");
ntangcaiming = MyArray.size();
System.out.println(ntangcaiming);
System.out.println("实验的第二部分:");
try {
File file = new File("D:\\huawei\\Javawindows文件","EXP1-First semester of sophomore.txt");
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(reader);
int[] file_word_temp = new int[2];
String[] file_word = bufferedReader.readLine().split(" ");
file_word_temp[0] = Integer.parseInt(file_word[0]);
file_word_temp[1] = Integer.parseInt(file_word[1]);
System.out.print("The array after 1 is inserted in position 5 is : ");
Array MyArray1 = new Array(MyArray.Array_Insert(4, String.valueOf(file_word_temp[0]))) ;
System.out.println(MyArray1);
System.out.print("The number of elements in the array is: ");
ntangcaiming = MyArray1.size();
System.out.println(ntangcaiming);
System.out.print("The list after inserting 2 at the first position is : ");
Array MyArray2 = new Array(MyArray1.Array_Insert(0, String.valueOf(file_word_temp[1])));
System.out.println(MyArray2);
System.out.print("The number of elements in the array is: ");
ntangcaiming = MyArray2.size();
System.out.println(ntangcaiming);
System.out.print("The array after deleting the inserted number 1 is : ");
Array MyArray3 = new Array(MyArray2.Array_Delete(5));
System.out.println(MyArray3);
System.out.print("The number of elements in the array is: ");
ntangcaiming = MyArray3.size();
System.out.println(ntangcaiming);
public String Array_Selection_sort() {
int[] temp_MyArray = new int[MyArray.length];
for (int i = 0 ; i < MyArray.length; i ++){
temp_MyArray[i] = Integer.parseInt(MyArray[i]);
}
String result = "";
for (int i = 0; i < temp_MyArray.length - 1 ; i++){
for (int j = i + 1;j < temp_MyArray.length; j++ ){
if (temp_MyArray[i]<temp_MyArray[j]){
int temp = temp_MyArray[i];
temp_MyArray[i] = temp_MyArray[j];
temp_MyArray[j] = temp;
String every = "";
for (int data : temp_MyArray){
every += data + " ";
}
result += "The list sorted by the SelectSorting is : " + every + "\n" +
"The number of elements in the array is: :" + MyArray.length + "\n";
}
}
}
return result;
}
System.out.println("实验的第三部分:");
System.out.print(MyArray3.Array_Selection_sort());
Intellj IDEA 简易教程
Android开发简易教程
Android studio项目上传至oschina(码云)教程
标签:tree 问题 pop The 启示 buffer 自己的 添加 展示
原文地址:https://www.cnblogs.com/Tangcaiming/p/9943025.html