/*P33用头插法建立带头结点的单链表*/
#include "stdio.h"
#define NULL 0
#define LEN sizeof(linklist)
typedef struct node
{int data;
struct node *next;
}linklist;
linklist *head;
void hhead_creat()/*用头插法建立带头结点的单链表*/
{int x;
linklist *p;
head=(struct node*)malloc(LEN);
head->data=-999;
head->next=NULL;
printf("\n\n\t\t请随机输入一组正整数以0作为结束符:\n\n\t\t");
scanf("%d",&x);
while(x!=0)
{ p=(struct node*)malloc(LEN);
p->data=x;
p->next=head->next;
head->next=p;
scanf("%d",&x);
}
}/*hrear_creat*/
void print_linklist(head)/*打印该链表*/
linklist *head;
{linklist *p;
int n=0;
p=head->next;
printf("\n\n\t\t");
while(p!=NULL)
{ printf("%5d",p->data);
p=p->next; n=n+1;
if((n+1)%10==0) printf("\n\t\t");
}
}/*print_linklist*/
main()
{hhead_creat(head);
print_linklist(head);
}
参考资料:《数据结构》田鲁怀编写 电子工业出版社(本人编写的书)