程序如下:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct st
{long num;
char name[20];
float score;
struct st *next;
};
/* 创建结点 */
struct st *creat()
{struct st *head=NULL,*p,*q;
q=p=(struct st *)malloc(sizeof(struct st));
scanf("%ld%s%f",&p->num,p->name,&p->score);
while(p->num)
{if(head==NULL)
head=p;
else
q->next=p;
q=p;
p=(struct st *)malloc(sizeof(struct st));
scanf("%ld%s%f",&p->num,p->name,&p->score);
}
q->next=NULL;
free(p);
return head;
}
/* 输出结点 */
void print(struct st *p)
{printf("\n%s\t%s\t%s\n","号码","姓名","总分");
while(p)
{printf("%03ld\t%s\t%.2f\n",p->num,p->name,p->score);
p=p->next;
}
printf("\n");
}
/* 删除结点 */
struct st *del(struct st *head,long num)
{struct st *p=head,*q;
if(num==head->num)
head=head->next;
else
{do
{q=p;
p=p->next;
}while(p&&num!=p->num);
if(p)
q->next=q->next->next;
else
printf("\nerror!\n");
}
free(p);
return head;
}
/* 插入结点 */
struct st *insert(struct st *head,struct st *p)
{struct st *q=head;
if(p->num<head->num)
{p->next=head;
head=p;
}
else
{while(q->next&&p->num>q->next->num)
q=q->next;
p->next=q->next;
q->next=p;
}
return head;
}
main()
{struct st *head,*p;
char t[20];
long num;
printf(" 全功能链表\n");
printf("\n%s\t%s\t%s\n","号码","姓名","总分");
head=creat();
print(head);
printf("删除(del)\插入(insert)\退出(exit):\n");
do
{scanf("%s",t);
if(strcmp(t,"del")==0)
{scanf("%ld",&num);
head=del(head,num);
}
else if(strcmp(t,"insert")==0)
{p=(struct st *)malloc(sizeof(struct st));
scanf("%ld%s%f",&p->num,p->name,&p->score);
head=insert(head,p);
}
else if(strcmp(t,"exit")==0)
break;
else
printf("\nerror!\n");
print(head);
}while(1);
printf("完美结束!\n\n");
}