C链中的双链表正在断开,不知道在哪里
我正在尝试创建一个双链接列表并打印它 我有两个函数,一个在列表的最前面添加,另一个在后面添加。但是打印不正确。。我觉得我把事情搞砸了,我知道我如何声明我的下一个和上一个指针有问题,但我不太清楚它是什么。。任何反馈都将不胜感激。多谢各位 这是我的密码C链中的双链表正在断开,不知道在哪里,c,doubly-linked-list,C,Doubly Linked List,我正在尝试创建一个双链接列表并打印它 我有两个函数,一个在列表的最前面添加,另一个在后面添加。但是打印不正确。。我觉得我把事情搞砸了,我知道我如何声明我的下一个和上一个指针有问题,但我不太清楚它是什么。。任何反馈都将不胜感激。多谢各位 这是我的密码 #include <stdio.h> #include <stdlib.h> struct node { int val; struct node *next; struct node *prev;
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
struct list
{
struct node *head;
struct node *tail;
};
void initialize(struct list *l)
{
l->head = NULL;
l->tail = NULL;
}
void push_front(struct list *l, int value)
{
if(l->head == NULL)
{
l->head = (struct node *)malloc(sizeof(struct node));
l->head->prev = NULL;
l->head->val = value;
if(l->tail == NULL)
l->head->next = l->tail;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->head->prev = temp;
temp->next = l->head;
temp->val = value;
temp->prev = NULL;
l->head = temp;
}
}
void push_back(struct list *l, int value)
{
if(l->tail == NULL)
{
l->tail = (struct node *)malloc(sizeof(struct node));
l->tail->next = NULL;
l->tail->val = value;
if(l->head == NULL)
l->tail->prev = l->head;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->tail->next = temp;
temp->prev = l->tail;
temp->val = value;
temp->next = NULL;
l->tail = temp;
}
}
void print(struct list *l)
{
if(l->head == NULL)
{
printf("%s", "List is empty..");
} else {
struct node *current;
current = l->head;
while(current != NULL)
{
printf("%d", current->val);
current = current->next;
}
}
}
int main()
{
struct list l;
initialize(&l);
push_front(&l, 6);
push_front(&l, 7);
push_back(&l, 10);
push_front(&l, 8);
push_front(&l, 9);
push_back(&l, 11);
print(&l);
return 0;
}
在以下代码块中不设置l->tail和l->head:
void push_front(struct list *l, int value)
{
if(l->head == NULL)
{
l->head = (struct node *)malloc(sizeof(struct node));
l->head->prev = NULL;
l->head->val = value;
if(l->tail == NULL)
l->head->next = l->tail;
}
void push_back(struct list *l, int value)
{
if(l->tail == NULL)
{
l->tail = (struct node *)malloc(sizeof(struct node));
l->tail->next = NULL;
l->tail->val = value;
if(l->head == NULL)
l->tail->prev = l->head;
}
除其他问题外,您的push_front代码不能确保设置了l->tail。它是:
void push_front(struct list *l, int value)
{
if(l->head == NULL)
{
l->head = (struct node *)malloc(sizeof(struct node));
l->head->prev = NULL;
l->head->val = value;
if(l->tail == NULL)
l->head->next = l->tail;
} else {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
l->head->prev = temp;
temp->next = l->head;
temp->val = value;
temp->prev = NULL;
l->head = temp;
}
}
它应该检查内存分配是否成功。它应该确保在l->head为空时设置l->tail。等等
void push_front(struct list *l, int value)
{
struct node *node = (struct node *)malloc(sizeof(*node));
if (node == 0)
err_error("Failed to allocate memory for a node\n"); // Does not return
node->val = value;
node->prev = NULL;
if (l->head == NULL)
{
assert(l->tail == NULL);
l->head = node;
node->next = NULL;
l->tail = node;
}
else
{
l->head->prev = node;
node->next = l->head;
l->head = node;
}
}
类似的更改将在push_back中进行:
有一个“向后打印”和一个“向前打印”功能是值得的。您可以在每次操作后都使用它们,以确保列表有意义。您可以添加更多断言以确保事情有意义
修订印刷品和主要材料:
样本输出:
Push front 6: 6
Push front 7: 7 6
Push back 10: 7 6 10
Push front 8: 8 7 6 10
Push front 9: 9 8 7 6 10
Push back 11: 9 8 7 6 10 11
这看起来很合理。您至少应该在列表中数字的一侧或另一侧打印一个空格,并且在末尾添加一个换行符才有意义。你得到了什么输出?您是否在每次操作后都尝试打印前推或后推以查看故障发生的位置?为什么不呢?屏幕上的电子和光子很便宜?通过调用函数的方式,我得到以下输出:9 8 7 6。应用标准调试技术。使用调试器。使用调试打印语句。将测试代码减少到最低限度,调试该用例直到通过,然后继续下一个更复杂的测试用例,依此类推。不要随便挑选一个测试用例,当失败的时候就举手以示失败。要有条理,要严谨,谢谢。真不敢相信我完全错过了。非常感谢。我不知道我怎么从来没有正确地定好头尾。
void print(struct list *l)
{
if (l->head == NULL)
printf("%s\n", "List is empty..");
else
{
struct node *current;
current = l->head;
while (current != NULL)
{
printf(" %2d", current->val);
current = current->next;
}
putchar('\n');
}
}
int main(void)
{
struct list l;
initialize(&l);
printf("Push front 6: ");
push_front(&l, 6);
print(&l);
printf("Push front 7: ");
push_front(&l, 7);
print(&l);
printf("Push back 10: ");
push_back(&l, 10);
print(&l);
printf("Push front 8: ");
push_front(&l, 8);
print(&l);
printf("Push front 9: ");
push_front(&l, 9);
print(&l);
printf("Push back 11: ");
push_back(&l, 11);
print(&l);
return 0;
}
Push front 6: 6
Push front 7: 7 6
Push back 10: 7 6 10
Push front 8: 8 7 6 10
Push front 9: 9 8 7 6 10
Push back 11: 9 8 7 6 10 11