【发布时间】:2011-05-05 21:26:22
【问题描述】:
我在下面编写了一个函数,它接受一个指向链表前面的指针,并确定该链表中的值是否以严格的升序存储。如果是这种情况,函数应该返回 1;否则它应该返回 0。
struct listnode {
int data;
struct listnode* next;
};
int ascendingOrder(struct listnode* front) {
struct listnode* current = front;
if(current->data == NULL)
return current->data;
while(current->next != NULL) {
if(current->data < current->next->data)
return 1;
}
else
return 0;
}
}
这行得通吗?如果行不通怎么办?
【问题讨论】:
-
sn-p 甚至无法编译。有一个
else没有对应的if。 -
同时检查一个有 1 个单节点的列表会发生什么:)
-
我编辑了代码,但我不确定我是否做了正确的更正
-
另外,请始终缩进您的代码。
-
即使在您编辑之后,它仍然有一个悬空的
else。与您的缩进级别保持一致,以便轻松找到这些错误。
标签: c linked-list