[英]debug an array of linked lists in c
我在我的项目中遇到了麻烦,起初我制作了一个链表并且它工作正常,但后来我不得不编辑它并使其成为链表数组,但我猜它只是以错误的方式存储,这里是我的代码,有什么问题? . 谢谢先进。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int coeff;
int power;
struct node* Next;
};
void Insert(int X,int Y, struct node* L, struct node* P)
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->coeff = X;
temp->power = Y;
temp->Next = P->Next;
P->Next = temp;
}
void PrintList(struct node* L)
{
struct node* P = L;
printf("%d %d ",P->coeff,P->power);
printf("\n");
P=P->Next;
}
int main()
{
struct node* head[3] ;
for(int q =0 ; q!= 3 ; q++)
{
head[q] = malloc(sizeof(struct node));
}
Insert(1,2,head[0],&head[0]);
Insert(2,3,head[0],&head[0]);
Insert(1,2,head[1],&head[1]);
Insert(2,2,head[1],&head[1]);
for(int i=0 ;i!=3;i++){
PrintList(head[i]);
}
return 0;
}
在 LinkedList 中只有 1 个头。 您已经分配了 3 个不同的磁头,并尝试将节点添加到这些磁头。 因此,以下答案假设您打算将 3 个不同的 LinkedLists 与每个列表的头部放在一个数组中。
此行中来自malloc()
的 memory
head[q] = malloc(sizeof(struct node));
未初始化。 在这里使用calloc()
显得更加谨慎。 所以,
head[q] = calloc(sizeof(struct node), 1); //Initialize everything to 0
接下来,您的Insert()
中的 arguments 之一是多余的,并且您的调用代码也没有 go 。 它是签名的
void Insert(int ,int , struct node* , struct node*)
但你正在调用它
void Insert(int ,int , struct node* , struct node**)
我猜,在这种情况下,删除最后一个参数应该没问题,因为您没有修改传入的struct node*
指针,而是修改了它的内容。 如果您将struct node*
更改为指向Insert()
function 中的其他内容(例如P = temp;
等),那么传入struct node**
将具有更多意义。 因此,将您的代码更改为
void Insert(int X,int Y, struct node* P)
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->coeff = X;
temp->power = Y;
temp->Next = P->Next;
P->Next = temp;
}
和调用代码
Insert(2,3,head[0]);
Insert(1,2,head[0]);
Insert(1,2,head[1]);
Insert(2,2,head[1]);
接下来,更改您的PrintList
以实际遍历 LinkedList:
void PrintList(struct node* L)
{
struct node* P = L;
while(P) {
printf("%d %d\n",P->coeff,P->power);
P=P->Next;
}
}
除此之外,我只是添加了一些日志记录以更好地阐明正在打印的 LinkedList。
for(int i=0 ;i!=3;i++){
printf("Printing list %d:\n", i);
PrintList(head[i]);
printf("*****\n");
}
这给出了 output:
Printing list 0:
0 0
1 2
2 3
*****
Printing list 1:
0 0
2 2
1 2
*****
Printing list 2:
0 0
*****
显然,LinkedLists 的所有head
都是 0 初始化的,因此显示为0 0
。 它们可以被初始化为合适的,或者可以完全从PrintList()
function 中删除,具体取决于您的程序的期望。
在处理 C 项目时,您应该始终启用编译器警告。 如果你这样做,编译器会发出警告,如
warning: passing argument 4 of ‘Insert’ from incompatible pointer type [-Wincompatible-pointer-types]
这只是代码中的第一个问题。 您在主 function 中分配的节点中也有未初始化的字段。
与修改列表的 function 不同,定义 function 更灵活,它将新节点添加到列表并返回新列表。 下面是另一种方法,它也使用方便的宏函数来分配 memory 并计算数组的长度。 该代码还将指针隐藏在类型定义后面,并定义了一个释放列表的 function。
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN(array) ((int) (sizeof (array) / sizeof (array)[0]))
#define NEW_ARRAY(pointer, length) \
{ \
(pointer) = malloc(((size_t) length) * sizeof (pointer)[0]); \
if ((pointer) == NULL) { \
fprintf(stderr, "Allocating memory with malloc failed: %s\n", strerror(errno)); \
exit(EXIT_FAILURE); \
} \
}
#define NEW(pointer) NEW_ARRAY((pointer), 1)
typedef struct node *List;
struct node {
int coeff;
int power;
struct node *next;
};
List NewList(int coeff, int power, List next)
{
List node;
NEW(node);
node->coeff = coeff;
node->power = power;
node->next = next;
return node;
}
void FreeList(List list)
{
if (list != NULL) {
FreeList(list->next);
free(list);
}
}
void PrintList(List list)
{
while (list != NULL) {
printf("%d %d\n", list->coeff, list->power);
list = list->next;
}
}
int main(void)
{
List head[3] ;
head[0] = NewList(1, 2, NewList(2, 3, NULL));
head[1] = NewList(1, 2, NewList(2, 2, NULL));
head[2] = NULL;
for (int i = 0; i < LEN(head); i++) {
PrintList(head[i]);
FreeList(head[i]);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.