这是一道C语言的题内容如下:
“建立一个结构体,含有n名学生的通讯录,对这n名学生按姓名进行排序。”谢谢!我是一初学者,帮帮忙!请按字母排列!谢谢了!急用!!!...
“建立一个结构体,含有n名学生的通讯录,对这n名学生按姓名进行排序。”谢谢!我是一初学者,帮帮忙!
请按字母排列!谢谢了!急用!!! 展开
请按字母排列!谢谢了!急用!!! 展开
2个回答
展开全部
用链表来做
定义一个结构体:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct stu
{
int num;// 学生学号
char name[10];//学生姓名
struct stu *next;
};
int main()
{
int x,num;
char name[10],temp_str[2];
struct stu *h,*p;
struct stu *head = (struct stu*)malloc(sizeof(struct stu));
head->next = NULL;
printf("Enter num (0 for end):");
scanf("%d",&num);
gets(temp_str);
while(num)
{
h = head;
printf("Enter name:");
scanf("%d %s",name);
gets(temp_str);
p = (struct stu *)malloc(sizeof(struct stu));
p -> next = NULL;
p->num = num;
strcpy(p->name,name);
while((h->next != NULL) && strcmp(h->next->name,p->name) > 0))
{
h = h->next;
}
p -> next = h -> next;
h -> next = p;
printf("Enter num(0 for end):");
scanf("%d",&num);
gets(temp_str);
}
printf("\n\tThe list as below\n");
printf("\tname\tnum\n");
h = head;
while(h -> next != NULL)
{
h = h->next;
printf("\t%s\t%d\n",h->name,h->num);
}
}
还没调试 有问题补充
定义一个结构体:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct stu
{
int num;// 学生学号
char name[10];//学生姓名
struct stu *next;
};
int main()
{
int x,num;
char name[10],temp_str[2];
struct stu *h,*p;
struct stu *head = (struct stu*)malloc(sizeof(struct stu));
head->next = NULL;
printf("Enter num (0 for end):");
scanf("%d",&num);
gets(temp_str);
while(num)
{
h = head;
printf("Enter name:");
scanf("%d %s",name);
gets(temp_str);
p = (struct stu *)malloc(sizeof(struct stu));
p -> next = NULL;
p->num = num;
strcpy(p->name,name);
while((h->next != NULL) && strcmp(h->next->name,p->name) > 0))
{
h = h->next;
}
p -> next = h -> next;
h -> next = p;
printf("Enter num(0 for end):");
scanf("%d",&num);
gets(temp_str);
}
printf("\n\tThe list as below\n");
printf("\tname\tnum\n");
h = head;
while(h -> next != NULL)
{
h = h->next;
printf("\t%s\t%d\n",h->name,h->num);
}
}
还没调试 有问题补充
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询