我知道在c或java中,将升序排序更改为降序的快速修复方法是找到<或者>某处并扭转它。但是尽可能地尝试,我无法找到相应的(或等效的交换)来改变我编写的代码,而不是提升。 (现在,它可以完美地按升序打印链表)。除了我所包含的内容之外我还有大约100个左右,因为他们没有进行排序,但是他们分配了值 - 所以这不是问题所在。我觉得我只是错过了一些明显的东西?这种变化会不会影响我的印刷方式或插入方式或两者兼而有之?任何帮助,将不胜感激。
loop_to_find_correct_spot:
beqz $t0, if_current_node_next_equals_null
l.s $f0, 0($t0)
c.lt.s $f12, $f0
bc1t if_current_node_does_not_equal_null
or $t1, $t0, $zero
lw $t0, 4($t0)
j loop_to_find_correct_spot
if_current_node_next_equals_null:
# link this node to the previous, $v0 = &(previous node)
# copy address of the new node into the previous node
sw $v0, 4($t1)
# initalize the current node value to the passed in argument on the $f12 register
swc1 $f12, 0($v0)
# initalize the current node next value to null
sw $zero, 4($v0)
j end_insert_if_else
if_current_node_does_not_equal_null:
# initalize the current node value to the passed in argument on the $f12 register
swc1 $f12, 0($v0)
sw $v0, 4($t1)
sw $t0, 4($v0)
j end_insert_if_else
end_insert_if_else:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
allocate_memory_for_node:
addi $sp, $sp, -4
sw $ra, 0($sp)
# allocate 12 bytes in memory for the new node
li $v0, 9
li $a0, 8
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print_linked_list:
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $s0, head # get a pointer to the head node
lw $s0, 4($s0)
begin_loop_print_linked_list:
# while (next != null)
beqz $s0, end_loop_print_linked_list
# get the value for the current node
l.s $f12, 0($s0)
# print out current item
li $v0, 2
syscall
la $a0, end_line_prompt #puts newlines in printed list
li $v0, 4
syscall
#get the pointer to the next node
lw $s0, 4($s0)
b begin_loop_print_linked_list
end_loop_print_linked_list:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra