#include <stdio.h>
#include <stdlib.h>
struct node //定义结点的结构
{
int num; //数据域
struct node * next;//指针域
};
typedef struct node Node;//给结构体命名为Node
typedef struct node * link;//给指向结构体的指针命名为link
void create_link(link * head)
{
*head = NULL;
}
void insert_code(link * head,link new_code)
{
new_code->next = * head;
* head = new_code;
}
void display_node(link head)
{
link tmp;
tmp = head;
while(tmp != NULL)
{
printf("num = %d\n",tmp->num);
tmp = tmp->next;
}
}
int main()
{
link head = NULL; //定义头指针
link new_code = NULL;//定义一个新的结点
int i;
create_link(&head);
for(i = 0;i <= 9;i++)
{
new_code = (link)malloc(sizeof(Node));
if(new_code == NULL)
{
printf("malloc error!\n");
exit(-1);
}
new_code->num = i;
insert_code(&head,new_code);
}
display_node(head);
return 0;
}
链表之头插
最新推荐文章于 2022-04-11 23:33:42 发布