我已经绞尽脑汁一周了,我就是不知道如何编写一个循环显示 9、4、14 的代码,说明如下:有人可以帮助我吗?我将不胜感激就这么多了。谢谢!
在Marie汇编器中创建以下静态链表,并添加Marie代码,使用循环遍历链表并显示每个节点值,如9、4、14。14后结束循环。
/node, data link
next = head // Head always points to the 1st node.
while next != None
print node value
advance next pointer to point to next node
wend
我的代码是“//定义节点和指针
我的代码:
ORG 100
HEAD, DEC 102 / Head pointer points to the first node
NODE1, DEC 9 / Value of Node 1
NODE2, DEC 4 / Value of Node 2
NODE3, DEC 14 / Value of Node 3
NEXT1, DEC 9 / Link from Node 1 to Node 2
NEXT2, DEC 4 / Link from Node 2 to Node 3
NEXT3, DEC 14 / Link from Node 3 to None (end of the list)
NEXT4, DEC 0
// Traversal loop to display node values
LOOP, Load HEAD / Load the head pointer into AC
Output / Output the value of the current node
Load HEAD / Load the next pointer into AC
Store HEAD / Update the HEAD pointer
SKIPCOND 400 / Skip if the pointer is not None
JUMP LOOP / Continue looping if the pointer is not None
Halt / Halt the program when reaching the end of the list
// End of the program
HALT
以下对伪代码进行了细化:
struct node { struct node *nextNode, int data; }
struct node node1 { &node2, 9 };
struct node node2 { &node3, 4 };
struct node node3 { null, 14 }
struct node *head = &node1;
struct node *next = head;
while ( next != null ) {
print next->data;
next = next->nextNode;
}
尝试将其放入组件中。
您当前的汇编代码有 2 个数组,其中一个与另一个数组重复。您需要一个项目对数组 - 只需一个数组,其中元素是项目对,而不是两个单独的数组。