gpt4 book ai didi

创建一个包含 100 个节点的链表,每个节点包含一个随机数 C

转载 作者:行者123 更新时间:2023-11-30 16:47:03 27 4
gpt4 key购买 nike

**我正在尝试理解链表,我想制作一个包含 100 个包含随机数的节点的链表。我的代码块说没有错误,但当我尝试运行它时,我的程序崩溃了。我不知道我做错了什么。

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int number;
struct node * next;
}Node;

typedef Node*Nodeptr;

int main() //creating the linked list
{
int i;
Nodeptr head = NULL;
Nodeptr here = head;
Nodeptr newnode = NULL;
head = malloc(sizeof(Node));

if(head == NULL){ //was told build the 1st node out of the loop
return 1;
}
for (i=0; i<100; i++){
newnode->number = rand()%100;
newnode->next = NULL;
here->next = newnode;
here = here->next;
}

printf("%d, %d, %p\n", i, here->number, here->next);
return 0;
}

最佳答案

这是检查错误并正确初始化每个节点的代码版本

#include <stdio.h>   // printf(), NULL
#include <stdlib.h> // malloc()

struct node
{
int number;
struct node * next;
};



int main( void ) //creating the linked list
{
int i;
struct node *head = malloc(sizeof( struct node ));
if(head == NULL)
{ //was told build the 1st node out of the loop
perror( "malloc for for node failed" );
exit( EXIT_FAILURE );
}

// implied else, malloc successful

head->next = NULL;
head->number = rand()%100;
printf("node: %2.2d, %2.2d, %p\n", 0, head->number, head->next);

struct node *here = head;

for (i=1; i<100; i++)
{
here->next = malloc( sizeof( struct node ) );
if( !(here->next) )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}

// implied else, malloc successful

here = here->next;
here->number = rand()%100;
printf("node: %2.2d, %2.2d, %p\n", i, here->number, here->next);
}

return 0; // << in modern compilers, this line not needed
// << when returning from 'main()' and value is 0
} // end function: main

典型的输出是:

node: 00, 83, (nil)
node: 01, 86, (nil)
node: 02, 77, (nil)
node: 03, 15, (nil)
node: 04, 93, (nil)
node: 05, 35, (nil)
node: 06, 86, (nil)
node: 07, 92, (nil)
node: 08, 49, (nil)
node: 09, 21, (nil)
node: 10, 62, (nil)
node: 11, 27, (nil)
node: 12, 90, (nil)
node: 13, 59, (nil)
node: 14, 63, (nil)
node: 15, 26, (nil)
node: 16, 40, (nil)
node: 17, 26, (nil)
node: 18, 72, (nil)
node: 19, 36, (nil)
node: 20, 11, (nil)
node: 21, 68, (nil)
node: 22, 67, (nil)
node: 23, 29, (nil)
node: 24, 82, (nil)
node: 25, 30, (nil)
node: 26, 62, (nil)
node: 27, 23, (nil)
node: 28, 67, (nil)
node: 29, 35, (nil)
node: 30, 29, (nil)
node: 31, 02, (nil)
node: 32, 22, (nil)
node: 33, 58, (nil)
node: 34, 69, (nil)
node: 35, 67, (nil)
node: 36, 93, (nil)
node: 37, 56, (nil)
node: 38, 11, (nil)
node: 39, 42, (nil)
node: 40, 29, (nil)
node: 41, 73, (nil)
node: 42, 21, (nil)
node: 43, 19, (nil)
node: 44, 84, (nil)
node: 45, 37, (nil)
node: 46, 98, (nil)
node: 47, 24, (nil)
node: 48, 15, (nil)
node: 49, 70, (nil)
node: 50, 13, (nil)
node: 51, 26, (nil)
node: 52, 91, (nil)
node: 53, 80, (nil)
node: 54, 56, (nil)
node: 55, 73, (nil)
node: 56, 62, (nil)
node: 57, 70, (nil)
node: 58, 96, (nil)
node: 59, 81, (nil)
node: 60, 05, (nil)
node: 61, 25, (nil)
node: 62, 84, (nil)
node: 63, 27, (nil)
node: 64, 36, (nil)
node: 65, 05, (nil)
node: 66, 46, (nil)
node: 67, 29, (nil)
node: 68, 13, (nil)
node: 69, 57, (nil)
node: 70, 24, (nil)
node: 71, 95, (nil)
node: 72, 82, (nil)
node: 73, 45, (nil)
node: 74, 14, (nil)
node: 75, 67, (nil)
node: 76, 34, (nil)
node: 77, 64, (nil)
node: 78, 43, (nil)
node: 79, 50, (nil)
node: 80, 87, (nil)
node: 81, 08, (nil)
node: 82, 76, (nil)
node: 83, 78, (nil)
node: 84, 88, (nil)
node: 85, 84, (nil)
node: 86, 03, (nil)
node: 87, 51, (nil)
node: 88, 54, (nil)
node: 89, 99, (nil)
node: 90, 32, (nil)
node: 91, 60, (nil)
node: 92, 76, (nil)
node: 93, 68, (nil)
node: 94, 39, (nil)
node: 95, 12, (nil)
node: 96, 26, (nil)
node: 97, 86, (nil)
node: 98, 94, (nil)
node: 99, 39, (nil)

所以,也许您想显示当前节点的地址而不是“下一个”字段的内容。

那么 printf() 语句将是:

printf("node: %2.2d, %2.2d, %p\n", 0, head->number, head);

printf("node: %2.2d, %2.2d, %p\n", i, here->number, here);

关于创建一个包含 100 个节点的链表,每个节点包含一个随机数 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com