如题,动态创建十个这样的节点,构成链表
.......................................
#include <stdio.h>
typedef struct _snode
{
char name[20];
char id[20];
float cscore;
unsigned int idx;
_snode *next;
}sNode,*pSNode;
void create(pSNode head,int count)
{
pSNode q = head;
for(int i=0;i<count;i++)
{
pSNode p = (pSNode)malloc(sizeof(sNode));
scanf("%s%s%f",p->name,p->id,&p->cscore);
p->idx = i;
p->next = NULL;
q->next = p;
q = p;
}
}
void query(pSNode head,char *name)
{
if(head == NULL || head->next == NULL)
return;
pSNode p = head->next;
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("姓名:%s\n",p->name);
printf("学号:%s\n",p->id);
printf("C成绩:%.1f\n",p->cscore);
break;
}
p = p->next;
}
}
void print(pSNode head)
{
if(head == NULL || head->next == NULL)
return;
pSNode p = head->next;
while(p != NULL)
{
printf("姓名:%s\t学号:%s\tC成绩:%.1f\t原序号:%d\n",p->name,p->id,p->cscore,p->idx);
p = p->next;
}
}
void sort(pSNode head)
{
if(head == NULL || head->next == NULL)
return;
pSNode p = head->next;
while(p != NULL && p->next != NULL)
{
pSNode q = p->next;
pSNode m = p;
while(q != NULL)
{
if(q->cscore < m->cscore)
m = q;
q = q->next;
}
if(m != p)
{
char name[20];
char id[20];
float cscore;
unsigned int idx;
strcpy(name,m->name);
strcpy(id,m->id);
cscore = m->cscore;
idx = m->idx;
strcpy(m->name,p->name);
strcpy(m->id,p->id);
m->cscore = p->cscore;
m->idx = p->idx;
strcpy(p->name,name);
strcpy(p->id,id);
p->cscore = cscore;
p->idx = idx;
}
p = p->next;
}
}
int main()
{
pSNode head = (pSNode)malloc(sizeof(sNode));
head->next = NULL;
printf("请分别输入10名学生信息,每行一个学生信息:\n");
create(head,10);
rintf("请输入要搜索的学生姓名:");
char name[20];
scanf("%s",name);
query(head,name);
sort(head);
printf("排序后信息为:\n");
print(head);
return 0;
}