构造链接llist C的appendNode函数的问题



来自tbaMUD 的源代码

文件中;handler.c">,我们有这个"strong>";obj_to_room"函数,该函数接受放置在房间中的任何对象并将其添加到链接列表中。这会创建一个对象堆栈,第一个对象位于堆栈的底部(头(,第二个对象堆叠在第一个对象的顶部,依此类推。这是C.中链表的默认行为

文件中;act.information.c">我们有";look_at_room">函数。这就称之为";list_obj_to_char"函数。这使用了";对于循环">读取列表/堆栈。

当使用";对于循环";要读取列表/堆栈/节点,它从顶部(尾部(到底部(头部(进行读取。这也是C中的默认行为。因此,放置在房间中的对象显示为最近放置的对象位于列表的顶部,第一个对象位于底部。

这就是导致这个问题的原因:www.tbamud.com/forum/2-general/5530-has-any-else-notified

我的目标是颠倒链接列表中对象的顺序。我可能会完成一些技巧,但它们就是这样——技巧,不完全正确,当然也不优雅。我认为最好的解决方案是使用"appendNode">将对象添加到列表的尾部(顶部(,而不是其头部(底部(。

为此,我需要改变这一点:

/* put an object in a room */
void obj_to_room(struct obj_data *object, room_rnum room)
{
if (!object || room == NOWHERE || room > top_of_world)
log("SYSERR: Illegal value(s) passed to obj_to_room. (Room #%d/%d, obj %p)",
room, top_of_world, (void *)object);
else {
object->next_content = world[room].contents;
world[room].contents = object;
IN_ROOM(object) = room;
object->carried_by = NULL;
if (ROOM_FLAGGED(room, ROOM_HOUSE))
SET_BIT_AR(ROOM_FLAGS(room), ROOM_HOUSE_CRASH);
}
}

类似的东西:

/*put an object in a room */
void obj_to_room(struct obj_data *object, room_rnum room)
{
if (!object || room == NOWHERE || room > top_of_world)
{
log("SYSERR: Illegal value(s) passed to obj_to_room. (Room #%d/%d, obj %p)",
room, top_of_world, (void*) object);
}
else
{
/*function to add objects at the tail of the list instead of its head*/
/*everything hinges on this single line and I probably have it all kinds of wrong*/
/*struct node* appendNode(struct node** head, int key)*/
struct world[room].contents* appendNode(struct world[room].contents** object, room_rnum room)
{
/* special case for length 0*/ 
if (object == NULL)
{
*object = world[room].contents;
}
else
{
/* locate the last node */
while (object->next_content != NULL)
{
object = object->next_content;
}
object->next_content = world[room].contents;
world[room].contents = object;
IN_ROOM(object) = room;
object->carried_by = NULL;
if (ROOM_FLAGGED(room, ROOM_HOUSE))
SET_BIT_AR(ROOM_FLAGS(room), ROOM_HOUSE_CRASH);
}
}
}
}

问题1

虽然我熟悉多种编程语言,但C不是其中之一。当谈到语言的特质和技术要点时,我一无所知。这使得阅读C代码和编写C代码变得更具挑战性

问题2

我知道格式应该是:

struct node* appendNode(struct node** head, int key)

我认为headint键是正确的,但我无法识别原始代码中的节点。所以我用了我最好的猜测。

尝试编译此代码产生以下结果并不奇怪:

handler.c: In function ‘obj_to_room’:
handler.c:681:19: error: expected identifier or ‘(’ before ‘[’ token
681 |       struct world[room].contents* appendNode(struct world[room].contents** object, room_rnum room)
|                   ^
make[1]: *** [<builtin>: handler.o] Error 1 

好吧,我怀疑这行有各种各样的问题,但我不知道如何解决。我希望一些优秀的程序员能提供帮助。

这就成功了。

/* put an object in a room */
void obj_to_room(struct obj_data* object, room_rnum room)
{
if (!object || room == NOWHERE || room > top_of_world) {
log("SYSERR: Illegal value(s) passed to obj_to_room. (Room #%d/%d, obj %p)", room, top_of_world, (void *)object);
}
else {
if (world[room].contents == NULL) {  // here, we have an empty list.
world[room].contents = object; // Just add it.
}
else {
struct obj_data* i = world[room].contents; // define a temporary pointer
while (i->next_content != NULL) {
i = i->next_content; // find the first without a next_content
}
i->next_content = object; // add it at the end
}
object->next_content = NULL; // end of the linked list
IN_ROOM(object) = room;
object->carried_by = NULL;
if (ROOM_FLAGGED(room, ROOM_HOUSE)) {
SET_BIT_AR(ROOM_FLAGS(room), ROOM_HOUSE_CRASH);
}
}
}

相关内容

  • 没有找到相关文章

最新更新