C 创建节点的链接列表
我想写一个main函数,它将创建节点的链表,其中每个节点在下一个字段之外的两个字段中保留两个int值。(在main类之外创建一个struct节点。)第一个int是节点数的计数。另一个int是从1开始的Fibonacci序列中的下一个数字。继续在列表中添加节点,直到新节点的第二个字段中的值超过第一个字段中的值*1000 添加每个节点时,将计数输出到一行。(不要在链表中添加最后一个节点。)它应该基本上打印第二个字段的值,在列中,每行5个 输出应如下所示:C 创建节点的链接列表,c,linked-list,nodes,C,Linked List,Nodes,我想写一个main函数,它将创建节点的链表,其中每个节点在下一个字段之外的两个字段中保留两个int值。(在main类之外创建一个struct节点。)第一个int是节点数的计数。另一个int是从1开始的Fibonacci序列中的下一个数字。继续在列表中添加节点,直到新节点的第二个字段中的值超过第一个字段中的值*1000 添加每个节点时,将计数输出到一行。(不要在链表中添加最后一个节点。)它应该基本上打印第二个字段的值,在列中,每行5个 输出应如下所示: 1 2 3 4 5 6 7 8 9 10 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765
10946 17711
我知道如何制作结构节点,我知道斐波那契码
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int x;
int y;
struct node * next;
} node_t;
int main()
{
int first = 0, second = 1, nxt, c;
int num = 0;
for ( c = 1 ; c < 23 ; c++ )
{
if ( c <= 1 )
nxt = c;
else
{
nxt = first + second;
first = second;
second = nxt;
}
printf("%d\n", nxt);
}
return 0;
}
#包括
#包括
类型定义结构节点{
int x;
int-y;
结构节点*下一步;
}节点t;
int main()
{
int first=0,second=1,nxt,c;
int num=0;
(C=1;C<23;C++)
{
如果(c创建节点
应使用动态内存分配(malloc)创建单个节点,请参见创建_节点()
链表创建
需要一个指向第一项的指针,另一个指向当前项的指针。第一项必须存储在第一项节点中。创建每个项后,必须将其附加到当前项。当前项节点必须更新为指向最后一项。下面的代码演示了创建链接列表的示例
node_t *create_node(int nr, int fib );
int main()
{
node_t *first_node=NULL; /* First node */
node_t *curr_node=NULL; /* Current node */
node_t *tmp_node=NULL;
/* Creating a list of 20 items*/
for(i=0;i<20;i++){
if (first_node==NULL){
curr_node=create_node(i,i*10);
first_node=curr_node;
}
else{
curr_node->next=create_node(i,i*10); /*appending to current */
curr_node = curr_node->next; /* make it as current */
}
}
i=0;
/* Traversing Linked list */
for(tmp_node=first_node;tmp_node!=NULL;tmp_node=tmp_node->next){
printf("%d --- %d\n",tmp_node->x, tmp_node->y);
i++;
}
}
node_t *create_node(int nr, int fib ){
node_t *node;
node = (node_t*) malloc(sizeof(node_t));
node->x=nr;
node->y=fib;
node->next=NULL;
return node;
}
node\u t*创建节点(int-nr,int-fib);
int main()
{
node_t*first_node=NULL;/*first node*/
node\u t*curr\u node=NULL;/*当前节点*/
node_t*tmp_node=NULL;
/*创建20个项目的列表*/
对于(i=0;inxt=create_节点(i,i*10);/*附加到当前*/
当前节点=当前节点->下一步;/*将其设为当前节点*/
}
}
i=0;
/*遍历链表*/
对于(tmp_节点=第一个节点;tmp_节点!=NULL;tmp_节点=tmp_节点->下一个){
printf(“%d---%d\n”,tmp\u节点->x,tmp\u节点->y);
i++;
}
}
节点\u t*创建\u节点(int nr,int fib){
节点节点;
node=(node_t*)malloc(sizeof(node_t));
节点->x=nr;
节点->y=fib;
节点->下一步=空;
返回节点;
}
网上有好的资源,做一个谷歌搜索“c链表实现”