|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/******************************************
**************这是一个C链表程序***********/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student
{
int num;
int score;
struct student *next;
};
void main ()
{
struct student *creat();//链表函数非常特别哦!
void print();
struct student *head;
head=NULL;
head=creat(head);//创建单链表!
print(head);
}
int n=0;
struct student *creat(struct student *head)//头指针,好重要啊!
{
struct student *p1,*p2;//继续申请结构指针变量,不要吝啬啊,呵呵!
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("Please input the num: ");
scanf("%d",&p1->num);
printf("Please input the score: ");
scanf("%d",&p1->score);
p1->next=NULL;//p1前锋,p2后跟!
while(p1->num!=0)
{ n++;
if(head==NULL)
{
head=p1;//空表赋头指针!也是第一次,膜拜ing!
}
else
{
p2->next=p1;//小弟也很重要,节点指针哦!
}
p2=p1;//第一次从此成为p1小弟!(结构指针变量地址!)
p1=(struct student *)malloc(sizeof(struct student));//此乃格式,向申请分配节点!
printf("Please input the num: ");
scanf("%d",&p1->num);
printf("Please input the score: ");
scanf("%d",&p1->score);//前锋啊,膜拜!p2:小弟也!
}
p2->next=NULL;
return head;
}
void print(struct student *head)//头指针,好重要啊!
{
struct student *temp;//创建临时结构指针变量!
temp=head;
printf("the all record is %d",n);
while(temp!=NULL)
{
printf("%d %d\n",temp->num,temp->score);
temp=temp->next;//本函数内有节点指针(地址)指向下一个函数首个地址!
}
} |
|