代码示例:
//创建链表——头插法
/*头插法:在头结点H之后插入数据,其特点是读入的数据顺序与线性表的逻辑顺序正好相反。*/
#include<stdio.h>
#include<stdlib.h>
#define N 10
struct Data
{
int num;
struct Data *next;
};
int main(int argc,char *argv[])
{
int data,i;
struct Data*head,*p;
head=(struct Data*)malloc(sizeof(struct Data));
if(head == NULL)
return -1;
head->next=NULL;
for(i=0;i<N;i++)
{
p=(struct Data*)malloc(sizeof(struct Data));
p->num=i;
p->next=head->next;
head->next=p;
}
printf("tou_cha_fa list:\n");
while(p!=NULL)
{
printf("%d\t",p->num);
p=p->next;
}
printf("\n");
return 0;
}