用链表读入学生名次和成绩然后输出
#include
#include
typedef struct stud{
char name[10];
int num;
float score;
struct stud *next;
}student;
student *creat(int n);
void output(student* head);
int main()
{
student *head;
int n;
printf("please input n:");
scanf("%d",&n);
head=creat(n);
output(head);
}
/*链表创建的函数*/
student *creat(int n)
{
student *head,*p,*q;
int i;
if(n>0){
head=(student*)malloc(sizeof(student));
printf("Please input the NO.1 name,num,sore:");
scanf("%s%d%f",head->name,&head->num,&head->score);
p=head;
for(i=2;i<=n;i++){
q=(student*)malloc(sizeof(student));
printf("Please input the NO.%d name,num,sore:",i);
scanf("%s%d%f",q->name,q->num,q->score);
p->next=q;
p=q;
}
q->next=NULL;
}
else
head=NULL;
return head;
}
/*链表输出的函数*/
void output(student* head)
{
student *p;
p=head;
while(p!=NULL)
{
printf("%s,%d,%f\n",p->name,&p->num,&p->score);
p=p->next;
}
在链表中查找某位成员值为给定值的结点历遍+比较,返回值为指针类型(指向查到结点的指针)
struct stud *find(struct stud *p)
{
long num;
printf("请输入你要查找的学号:");
scanf("%ld",&num);
while(p!=NULL){
if(p->num == num)
return p;
p->next;
}
return NULL;
}
student *_delete(student *head,int x)
{
student *p,*pre;
pre = p = head;
while(p!=NULL&&p->num!=x){
pre=p;
p=p->next;
}
if(p->num==x){
if(p==head)
head=p->next;
else
pre->next=p->next;
free(p);
}
student *insert(student *head)
{
student *p,*q;
q=(student*)malloc(sizeof(student));
printf("please input the name,num,score:");
scanf("%s%d%f",q->name,&q->num,&q->score);
q->next=NULL;
if(head==NULL)
head=q;
else{
p=head;
if(head->num>q->num){
q->next=head;
head=q;
return head;
}
while(p->next!=NULL&&p->next->num<q->num)
p=p->next;
if(p->next!=NULL&&p->next->num==q->num)
free(q);
else if(p->next==NULL)
p->next=q;
else{
q->next=p->next;
p->next=q;
}
}
return head;
}