C简单链接列表
我在网上看了很多不同的问题,不知道我做错了什么。我现在可能走错了方向,因为我已经尝试了很多不同的事情 我只是想用C语言制作一个简单的单链接列表。我似乎不知道如何使列表保持连接 我的节点的结构C简单链接列表,c,C,我在网上看了很多不同的问题,不知道我做错了什么。我现在可能走错了方向,因为我已经尝试了很多不同的事情 我只是想用C语言制作一个简单的单链接列表。我似乎不知道如何使列表保持连接 我的节点的结构 typedef struct node { double x; // x-coordinate of this point in the tour double y; // y-coordinate of this point in the tour
typedef struct node
{
double x; // x-coordinate of this point in the tour
double y; // y-coordinate of this point in the tour
struct node* next; // Pointer to the next node in the linked list
} Node;
这是我制作列表的代码,我确实在main中构造了一个空节点first=NULL
Node* addFront(Node* first, double x, double y) {
first = malloc(sizeof(Node));
if (first == NULL) {
first->x = x;
first->y = y;
first->next = NULL;
}
else {
Node * temp = malloc(sizeof(Node));
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
}
//Temp testing
int size = 0;
Node * current = first;
while (current->next != NULL) {
printf("(%.4f, %.4f)\n", current->x, current->y);
current = current -> next;
size++;
}
printf("Size: %d\n", size);
return first;
}
一些注意事项:
检查first是否为null应该是不必要的。。。仅使用else语句就可以构建列表。(我的想法)
在添加if/else语句之后,我得到了一个似乎是无限循环的东西,C只是指向随机内存,最终导致分段错误
我只是不知道还能去哪里。非常感谢您的支持 这个块毫无意义:
first = malloc(sizeof(Node));
if (first == NULL) {
first->x = x;
first->y = y;
first->next = NULL;
}
可能您想要移动first=malloc(sizeof(Node))代码>在块内。它可以工作,但是完全没有必要,因为它在逻辑上等于else
块。所以你可以离开那里的第二个街区:
Node * temp = malloc(sizeof(Node));
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
return first;
// or rather return temp directly
还有一点-您应该在malloc
内存耗尽的情况下添加错误处理,因此您应该检查temp==NULL
,并相应地采取行动(从函数或任何东西返回NULL
。此块毫无意义:
first = malloc(sizeof(Node));
if (first == NULL) {
first->x = x;
first->y = y;
first->next = NULL;
}
可能您想要移动first=malloc(sizeof(Node))代码>在块内。它可以工作,但是完全没有必要,因为它在逻辑上等于else
块。所以你可以离开那里的第二个街区:
Node * temp = malloc(sizeof(Node));
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
return first;
// or rather return temp directly
还有一点-您应该在malloc
内存不足的情况下添加错误处理,因此您应该检查temp==NULL
,并相应地采取行动(从函数或任何东西返回NULL
).对于初学者,甚至函数的第一个语句都是错误的,因为参数first
的值被覆盖
Node* addFront(Node* first, double x, double y) {
first = malloc(sizeof(Node));
//...
该函数可以按以下方式显示
Node * addFront( Node *first, double x, double y )
{
Node *temp = malloc( sizeof( Node ) );
if ( temp != NULL )
{
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
}
return first;
}
或者使用测试代码
Node * addFront( Node *first, double x, double y )
{
Node *temp = malloc( sizeof( Node ) );
if ( temp != NULL )
{
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
}
// start pf inline test
size_t size = 0;
for ( Node *current = first; current != NULL; current = current->next )
{
printf( "(%.4f, %.4f)\n", current->x, current->y );
++size;
}
printf( "Size: %zu\n", size );
// end pf inline test
return first;
}
对于初学者,甚至函数的第一条语句都是错误的,因为参数first
的值被覆盖
Node* addFront(Node* first, double x, double y) {
first = malloc(sizeof(Node));
//...
该函数可以按以下方式显示
Node * addFront( Node *first, double x, double y )
{
Node *temp = malloc( sizeof( Node ) );
if ( temp != NULL )
{
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
}
return first;
}
或者使用测试代码
Node * addFront( Node *first, double x, double y )
{
Node *temp = malloc( sizeof( Node ) );
if ( temp != NULL )
{
temp->x = x;
temp->y = y;
temp->next = first;
first = temp;
}
// start pf inline test
size_t size = 0;
for ( Node *current = first; current != NULL; current = current->next )
{
printf( "(%.4f, %.4f)\n", current->x, current->y );
++size;
}
printf( "Size: %zu\n", size );
// end pf inline test
return first;
}
如果(first==NULL){…}
块没有意义。如果first
为NULL
则不能执行first->x=x代码>等等。这只是意味着malloc
在分配内存之前失败了。如果(first==NULL){…}
块没有意义。如果first
为NULL
则不能执行first->x=x代码>等等。这只是意味着,malloc
以前分配内存失败。非常感谢。我想我在没有if声明的情况下是正确的,我必须承认我只是仓促行事,没有正确测试。非常感谢。我想如果没有if声明的话,我的测试是正确的。我必须承认,我只是仓促行事,没有正确测试。