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 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
- ¥20 怎么在stm32门禁成品上增加查询记录功能
- ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
- ¥50 NT4.0系统 STOP:0X0000007B
- ¥15 想问一下stata17中这段代码哪里有问题呀
- ¥15 flink cdc无法实时同步mysql数据
- ¥100 有人会搭建GPT-J-6B框架吗?有偿