c - 我怎样才能使数据按字母顺序打印在列表中?
问题描述
我正在处理的程序应该将节点添加到链接列表,添加每个节点,以便相关数据按字母顺序保存。我让它打印但是我希望它按字母顺序打印,例如
!!!
苹果
自行车
猫
狗
< ~~~>(放箭头,这样就不会把它写成代码了)
等等
我还有另一个问题,第二次输入后,当我输入短语退出时,它并没有像预期的那样结束程序,我该如何解决
谢谢
这是我当前的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct IntNode_struct {
char dataVal[50];
struct IntNode_struct* next;
} IntNode;
void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode){
IntNode *current = thisNode;
while (current->next != NULL && strcmp(newNode->dataVal, current->next->dataVal) >= 0){
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
void IntNode_Printlist(IntNode* current){
if(current == NULL)
return;
printf("%s\n", current->dataVal);
IntNode_Printlist(current = current->next);
}
int main(int argc, const char * argv[]) {
IntNode a,b;
IntNode *head;
IntNode *current;
a.next = &b;
b.next = NULL;
head = &a;
strcpy(a.dataVal, "!!!");
strcpy(b.dataVal, "~~~");
while(1){
current = (IntNode*)malloc(sizeof(IntNode));
printf("Enter Next Name");
char dummy[50];
gets(dummy);
if(strcmp(dummy, "quit") == 0)
return 0;
strcpy(current->dataVal, dummy);
IntNode_InsertAfter(head, current);
IntNode_Printlist(head);
printf("\n");
}
return 0;
}
解决方案
推荐阅读
- c++ - UE4 如何使用 UMG ListView 显示结构列表?
- azure-active-directory - 不支持带过滤器的结束
- android - 为 Xamarin 项目设置 Firebase
- java - LOAD DATA INFILE LINES TERMINATD 不适用于 \n
- html - 如何使跨度输入组文本在html中具有相同的大小?
- laravel - Laravel URL 在页面加载时更改(但页面没有)
- c++ - 带有 Gazebo 的 PX4 多车辆模拟:什么是无 ROS?
- flutter - Flutter 滚动视图到列上的焦点小部件
- microsoft-dynamics - 销售订单的行完全形成并且整个过帐过程完成的时刻是什么时候?
- flutter - 按问题类型修复 Dart