如何建立单链表
如何建立单链表
2011-11-24 20:19
程序代码:/*结构定义*/
struct student
{
int num; /*学号*/
float score; /*成绩*/
struct student *next; /*指针域*/
};
/*构造单链表如下:*/
struct student *creat() /*此函数返回一个指向链表的头结点*/
{
struct student *head; /*指向student类型变量的指针head为头指针*/
struct student *p,*tail; /*tail指向链表末尾元素,p指向新分配的结点*/
float temp; /*临时数据变量*/
head=NULL;
do
{
p=(struct student *)malloc(sizeof(struct student));/*分配新结点*/
printf(“Number Score:”);
fflush(stdin); /*清除输入缓冲区*/
scanf("%d %f",&p->num,&temp);
if(p->num==0)
{
free(p);
break;
};
p->score=temp; /*取得结点数据*/
p->next=NULL;
if(head==NULL)
{
head=p;
tail=p;
}
else
{
tail->next=p;
tail=p; /*指向尾结点*/
}
}while(p->num!=0);
return(head);
}
多看课本,希望你能学好
2011-11-24 22:47
2011-11-27 12:05
2011-11-27 21:32