c语言用单向链表做英语词典,单词保存在结构体数据域的字符数组中,如何根据各单词的字母顺序进行排序(从a到z),并逐一显示出来(和纸质的词典相同)
2条回答 默认 最新
- 技术专家团-小桥流水 2021-11-20 08:53关注
参考如下代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct StData { char name[30]; //int score; }; typedef struct link_node { struct StData data; struct link_node* next; }node,*linklist; linklist createList() { struct StData x; linklist L; char ch; L = (linklist)malloc(sizeof(node)); if (L == NULL) { printf("error"); return 0; } linklist n,p; p = L; printf("请输入字符串:\n"); //输入系列并以回车结束 while(1) { scanf("%s",x.name); n = (linklist)malloc(sizeof(node)); n->data = x; n->next = NULL; p->next = n; p = n; if( (ch=getchar()) == '\n') break; } return L; } void print(linklist head) { linklist p; p = head->next; while(p) { printf("%s ",p->data.name); p = p->next; } printf("\n"); } //排序 void BubbleSort(linklist List) { node * p, * q, * tail; tail = NULL; while((List->next->next) != tail) { p = List; q = List->next; while(q->next != tail) { if( strcmp(q->data.name ,q->next->data.name)> 0 ) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } } //释放内存 void release(linklist head) { linklist p; while(head) { p = head->next; free(head); head = p; } } int main() { linklist head = createList(); printf("链表信息:"); print(head); BubbleSort(head); printf("排序后:"); print(head); release(head); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)
- ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。