- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做作业,经过几天的努力,我无法弄清楚为什么在实现归并排序后,我的列表仅包含链接列表中的最后一个对象。它不输出我的整个链表,只输出最后一个对象。如何更改代码以阻止列表在一个对象之后变为 null
。
请注意:虽然我称它们为立方体,但我知道它们不是立方体,因为它们具有随机的长度、宽度和高度。分配指定它们被称为立方体,但具有这些随机数据字段。请忽略这一点。
public class SortingCubes {
public static void main(String[] args) {
System.out.println(" ");
System.out.println("-----MY LINKED LIST-----");
Cube headCubeLL = new Cube(); //Create head cube
int LLnum = 5; //Change number of linked list items desired here
for(int i = 0; i < LLnum; i++) {
headCubeLL.length = Math.random() * 99 + 1; //Create random L,W,H for each cube (between 1-100)
headCubeLL.width = Math.random() * 99 + 1;
headCubeLL.height = Math.random() * 99 + 1;
headCubeLL.next = null; //Sets end of list to null
if(headCubeLL.next == null) { //creates new cube until desired number is reached in the for loop
Cube curr = new Cube(headCubeLL.length, headCubeLL.width, headCubeLL.height);
headCubeLL = curr;
}
headCubeLL.next = null; //Sets last cube (next) to null to end the list.
System.out.println(headCubeLL.toString() + " "); //Print my Linked list until end
}
System.out.println(" ");
System.out.println("-----NEW LINKED LIST AFTER MERGE SORT METHOD IS IMPLEMENTED (Asending Order by Volume)-----");
long startTimeMergeSort = System.nanoTime();
mergeSort(headCubeLL);
long timeElapsedMergeSort = (System.nanoTime()- startTimeMergeSort);
printList(headCubeLL); //Method to print linked list
System.out.println("Objects in list " + count(headCubeLL));
long startTimeInsertionSort = System.nanoTime();
System.out.println(" ");
System.out.println("Time Record: ");
System.out.println("Time elapsed for Merge sort on a Linked List: " + timeElapsedMergeSort + " Nanos");
} //End of Main
public static void printList(Cube headCubeLL){
while(headCubeLL != null){
System.out.println(headCubeLL.toString() + " ");
headCubeLL = headCubeLL.next;
}
System.out.println();
}
public static int count(Cube head){
int count = 0;
while(head != null){
//System.out.println(head);
count++;
head = head.next;
}
return count;
}
public static Cube mergeSort(Cube headCubeLL) {
if(headCubeLL == null || headCubeLL.next == null) { //checking list is null
return headCubeLL;
}
int count = 0; //To count the total number of elements
Cube temp = headCubeLL; //Temporary head of list
while(temp != null) { //break up the list into two parts
count++; //while not empty, count one and move temp to temp.next to evaluate
temp = temp.next;
}
int middle = count/2; //create an integer called middle and divide the length of your list (count) by 2
Cube a = headCubeLL; //another temp head cube for 1st split list
Cube b = null; //another cube that is null
Cube temp2 = headCubeLL; //create another temp head cube for 2nd list
int countHalf = 0; //start at half 0 again
while(temp2 != null) { //while temp I have for 2nd list is not null....
countHalf++; //add count to get length of linked list
Cube theNext = temp2.next; //Create new cube that will be assigned to cube after head
if(countHalf == middle) { //once it reaches middle number
temp2.next = null; //end list (set head.next to null for list 2)
b = theNext; //Take my empty null cube and assign it to end with null.
}
temp2 = theNext; //Otherwise - move along and my temp head will be the temp head.next
}
//Now I have 2 parts, List a and List b.
Cube half1 = mergeSort(a); //Recursively call to sort each half
Cube half2 = mergeSort(b);
//Merge together
Cube merged = merge(half1, half2); //Call 2nd method to merge the 2 halves together
//System.out.println(merged.toString()); //Print the L,W,H and Volume of each cube in my array
return merged; //return merged list.
}
public static Cube merge(Cube a, Cube b) { //parameters are the 2 half's of the original list
Cube pt1 = a; //2 parts that I am going to merge as ascending lists according to volume
Cube pt2 = b;
Cube tempHead = new Cube(); //Temp head of my New list that cubes will be merged into
Cube ptNew = tempHead; //Temp Head for List my new list
while(pt1 != null || pt2 != null) { //While either list is not empty
if(pt1 == null) { //but if first is null
ptNew.next = new Cube(pt2.cubeVolume()); //create new cube for every cube volume
pt2 = pt2.next; //loop
}
else if(pt2 ==null){ //but if 2nd is null
ptNew.next = new Cube(pt1.cubeVolume()); //create new cube for every cube volume
pt1 = pt1.next; //loop
ptNew = ptNew.next; //move on by assigning moving to next cube creating when pt2 is == null
}
else {
if(pt1.cubeVolume() < pt2.cubeVolume()) { //moving while merging - comparing volumes of the head of each list
ptNew.next = new Cube(pt1.cubeVolume()); //When one is greater then new cube creating in new list and cube is placed accordingly
pt1 = pt1.next; //loop
ptNew = ptNew.next; //loop through new merged list for next comparison
}
else if(pt1.cubeVolume() == pt2.cubeVolume()) { //statement if the cubes volumes are equal
ptNew.next = new Cube(pt1.cubeVolume());
ptNew.next.next = new Cube(pt1.cubeVolume());
ptNew = ptNew.next.next;
pt1 = pt1.next; //place one next to the other
pt2 = pt2.next;
}
else {
ptNew.next = new Cube(pt2.cubeVolume()); //else made new cube in new list and place pt2 head
pt2 = pt2.next; //loop
ptNew = ptNew.next; //loop
}
}
}
return tempHead.next; //return new merged list
}
} //End of Class
我的Cube类:(全部正确仅供引用)
public class Cube {
double length;
double width;
double height;
Cube next = null;
public Cube() { //Default Constructor
}
public Cube(double volume) {
volume = this.cubeVolume();
}
public Cube(Double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
this.next = next;
}
public String toString() { //Print into a String
return "CUBE Volume: (" + cubeVolume() + ") ----- CUBE Length: ("+ this.length +") ----- CUBE Width (" + this.width + ") ----- CUBE Height (" + this.height + ")";
}
//Set length
public void setLength(double length) {
this.length = length;
}
//Get length
public double getLength() {
return this.length;
}
//Set Width
public void setWidth(double width) {
this.width = width;
}
//Get Width
public double getWidth() {
return this.width;
}
//Set Height
public void setheight(double height) {
this.height = height;
}
//Set Height
public double setHeight() {
return this.height;
}
//Set Next
public void setNext(Cube next) {
this.next = next;
}
//Get Next
public Cube getNext() {
return this.next;
}
public double cubeVolume () {
double volume = (length*width*height);
//System.out.println("TEST: " + volume);
return volume;
}
} //End of Class
输出
-----MY LINKED LIST-----
CUBE Volume: (14550.645379921463) ----- CUBE Length: (19.526751823368887) ----- CUBE Width (54.77537177724803) ----- CUBE Height (13.604009167732666)
CUBE Volume: (1631.5144309742377) ----- CUBE Length: (40.72878317573845) ----- CUBE Width (22.07526604887876) ----- CUBE Height (1.8146109949933575)
CUBE Volume: (17837.576670179817) ----- CUBE Length: (4.606784797762423) ----- CUBE Width (78.5447210731351) ----- CUBE Height (49.29704940251802)
CUBE Volume: (113668.01972101796) ----- CUBE Length: (24.366242383656253) ----- CUBE Width (54.33809524521938) ----- CUBE Height (85.85099307890556)
CUBE Volume: (432771.5800393206) ----- CUBE Length: (83.95704403819472) ----- CUBE Width (56.0616051224998) ----- CUBE Height (91.94668276748753)
-----NEW LINKED LIST AFTER MERGE SORT METHOD IS IMPLEMENTED (Asending Order by Volume)-----
CUBE Volume: (432771.5800393206) ----- CUBE Length: (83.95704403819472) ----- CUBE Width (56.0616051224998) ----- CUBE Height (91.94668276748753)
Objects in list 1
Time Record:
Time elapsed for Merge sort on a Linked List: 11831 Nanos
最佳答案
问题出在您的 main
方法中,而不是您的排序方法中。这个区 block
headCubeLL.next = null; //Sets end of list to null
if(headCubeLL.next == null) { //creates new cube until desired number is reached in the for loop
Cube curr = new Cube(headCubeLL.length, headCubeLL.width, headCubeLL.height);
headCubeLL = curr;
}
headCubeLL.next = null;
将始终进入if
部分。您根本没有设置 curr
的 next
字段。因此,当您将 curr
分配给 headCubeLL
时,您会丢失 headCubeLL
之前的值。这意味着您的列表中永远只有一个对象。每次创建新对象时,您都会丢弃该对象。
你需要
headCubeLL.next = null;
curr
后添加新行 curr.setNext(headCubeLL);
。这样,前面的 headCubeLL
将被记住,作为您刚刚创建的新对象的 next
元素。
关于java - 对象链表的归并排序(升序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707745/
我正在做作业,经过几天的努力,我无法弄清楚为什么在实现归并排序后,我的列表仅包含链接列表中的最后一个对象。它不输出我的整个链表,只输出最后一个对象。如何更改代码以阻止列表在一个对象之后变为 null。
我想对一列进行排序(它是一个带有 Y/N 的标志列)。它应该在每次点击时在升序/降序之间切换。 我的代码不起作用..我是 VBA 新手。请提供任何帮助。 Private Sub CommandButt
我对如何让它正常工作有点困惑。我需要从用户那里获取数字(直到他们输入负数或达到最大大小),并且对于他们添加的每个数字,将其按升序插入到正确的索引中。现在,由于某种原因,即使我定义了常量 10,我的数组
我相当困惑如何创建一个按钮,将打印到 php 文件的表中的数据按升序或降序排序。 "> Order by Week Sort Week 这是我想要实现的一个简单示例,我只是停留在 php
我在使用 C++ 中的 priority_queue 时遇到问题,我有一个优先级队列 vector ,优先级队列包含多个 Person 对象。现在,我希望 priority_queue 根据年龄对 P
我正在使用 Lodash 按列对表中的数据进行排序。当我单击表格列标题中的箭头时,该特定表格列将按升序或降序排序。但是,我希望每一列首先按升序排序,而不管其他列的当前顺序如何。现在,我的函数只根据当前
如果事先知道哪些列可用,则以下代码可以重新排列列,但如果想按降序/升序重新排列列怎么办? StackOverflow 上有一些类似的帖子,但没有一篇可以在事先不知道哪些列可用的情况下这样做。 ty
在 woocommerce 中,我使用以下代码添加了自定义费用: add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
您好,我正在使用以下内容对表适配器返回的数据表的结果进行排序 Dim spots = myDataTable.Where(Function(t) t.UserID = 1).OrderByDesce
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
我正在尝试获取数据库中最近的 n 个条目的列表,但将它们按升序排序。 显然我可以使用以下方法获取前 n 个条目: SELECT owner_id,message FROM messages WHERE
我尝试使用此方法将数据提取到 mysql 表 $query=$conn->query("SELECT * FROM users ORDER BY id_user ASC"); 这是我的表结构 用户 i
我正在使用 NSFetchedResultsController 在列表中显示对象 Event。 Event 对象具有 startDate 属性和 eventType 属性,它是 CheckIn 类型
我有以下代码/数据: import numpy as np data = np.array([ [12, 1, 0.7, 0], [13, 2, 0.5, 1], [41, 3
所以我是 C++ 的新手,我正在尝试一些初学者练习,这是问题所在:我必须按升序和降序对整数数组进行排序,但每次我尝试按升序排序时,都会出现 0在我的数组中无处替换以前的数组整数。只有当我使用“升序”选
在我的应用程序中,我有一个任务列表(不,它不仅仅是另一个待办事项应用程序),我使用 NSFetchedResultsController 在 UITableView 中显示任务。这是相关的初始化代码:
本人由于项目开发中需要对查询结果list进行排序,这里根据的是每一个对象中的创建时间降序排序。本人讲解不深,只实现目的,如需理解原理还需查阅更深的资料。 1.实现的效果 2.创建排序的对象
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
我需要插入两个值 num1 = 50和 num2 = 80成一个已按升序排序的数组。我不能使用动态数组或列表。也没有结构或类。这是一个类作业,所以我必须遵循指导方针。教授建议我新建一个数组,newar
我是一名优秀的程序员,十分优秀!