[英]Unable to reverse a linked list
I was trying to reverse a linked list, however whenever I execute the following function, I get only the last element. 我试图反向链接列表,但是,每当执行以下功能时,我只会得到最后一个元素。 For example, if the list contained 11,12,13 earlier. 例如,如果列表包含11、12、13之前的时间。 After executing the function, it contains only 13. Kindly point out the bug in my code 执行该函数后,它仅包含13个。请指出我代码中的错误
void reverselist() {
struct node *a, *b, *c;
a = NULL;
b = c = start;
while (c != NULL) {
c = b->next;
b->next = a;
a = b;
b = c;
}
start = c;
}
I would have made a prepend function, and done the following: 我会做一个前置函数,并执行以下操作:
struct node* prepend(struct node* root, int value)
{
struct node* new_root = malloc(sizeof(struct node));
new_root->next = root;
return new_root;
}
struct node* reverselist(struct node* inlist)
{
struct node* outlist = NULL;
while(inlist != NULL) {
struct node* new_root = prepend(outlist, inlist->value);
outlist = new_root;
inlist = inlist->next;
}
return outlist;
}
Have not tested this, but guess you grasp the idea of it. 尚未测试过,但是您猜想您已经掌握了它的想法。 Might be just your variable names, which don't describe anything, but I think this approach is cleaner, and easier to understand what actually happens. 可能只是您的变量名,不会描述任何内容,但是我认为这种方法更简洁,更容易理解实际发生的情况。
EDIT: 编辑:
Got a question why I don't do it inplace, so I'll answer it here: 有一个问题为什么我不就地做,所以我在这里回答:
Thing is, this is a first implementation. 事实是,这是第一个实现。 It should work, and not be optimized. 它应该工作,而不是优化。 It should also have a test written before this implementation is even thought of, and you should keep this slow, un-optimized implementation until the test passes, and you have proved that it's to slow for your use! 在还没有考虑到该实现之前,还应该编写一个测试,并且应该保持这种缓慢的,未经优化的实现,直到测试通过为止,并且已经证明使用起来会很慢!
When you have a passing unit test, and proven the implementation to be to slow, you should optimize the code, and make sure it still passes the test, without changing the test. 当您通过了单元测试并证明实现速度很慢时,您应该优化代码,并确保代码仍然通过测试,而无需更改测试。
Also, is it necessary inplace operations which is the answer? 另外,答案是否必须是就地操作? What about allocating the memory before reverting it, this way you only have one allocation call, and should hopefully get a nice performance boost. 在还原内存之前先分配内存,这样您就只有一个分配调用,并且有望获得不错的性能提升。
This way everyone is happy, you have a cleaner code and avoid the risk of having Uncle Bob showing up at your door with a shotgun. 这样,每个人都很高兴,您的代码更简洁,并且避免了鲍伯叔叔拿着a弹枪出现在您家门口的风险。
// You should assume that Node has a Node* called next that // points to the next item in a list // Returns the head of the reversed list if successful, else NULL / 0 Node *reverse( Node *head ) { Node *prev = NULL; while( head != NULL ) { // Save next since we will destroy it Node *next = head->next; // next and previous are now reversed head->next = prev; // Advance through the list prev = head; head = next; } return previous; }
c is a helper pointer. c是一个辅助指针。
void reverselist()
{
struct node *a, *b, *c;
a=NULL;
b=start;
while(b!=NULL)
{
c=b->next
b->next=a;
a=b
b=c
}
start=a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.