嵌入式学习——C语言学习:链表!!!
#include <stdio.h>
#include <stdlib.h>struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef struct node * Link;
void create_link(Link *head)
{
*head = NULL;
}
void insert_node_head(Link * head,Link new_node)
{
new_node->next = *head;
*head = new_node;
}
void display_link(Link head)
{
Link p;
p = head;
while(p != NULL)
{
printf("num = %d\n",p->num);
p = p->next;
}
}
int main()
{
Link head = NULL;
create_link(&head);
Link new_node = NULL;
int i = 0;
for(i = 0; i < 10;i++)
{
new_node = (Link)malloc(sizeof(Node));
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
new_node->num = i + 1;
insert_node_head(&head,new_node);
}
display_link(head);
return 0;
}