C语言链表
顺序连接创建链表
#include<stdio.h>
#include<stdlib.h>
typedef struct NUM
{
int num;
struct NUM* next;
}list;
list* create(int n)
{
list* head = (list*)malloc(sizeof(list));
list* end = head;
list* node;
while(n--)
{
node = (list*)malloc(sizeof(list));
scanf("%d", &node->num);
end->next = node;
end = node;
}
end->next = NULL;
return head;
}
int main()
{
int n = 0;
scanf("%d", &n);
list* head = create(n);
}
逆序插入创建链表
list* create(int n)
{
list* head = (list*)malloc(sizeof(list));
list* node;
head->next = NULL;
while(n--)
{
node = (list*)malloc(sizeof(list));
scanf("%d", &node->num);
node->next = head->next;
head->next = node;
}
return head;
}