首页 > 解决方案 > 我怎样才能使数据按字母顺序打印在列表中?

问题描述

我正在处理的程序应该将节点添加到链接列表,添加每个节点,以便相关数据按字母顺序保存。我让它打印但是我希望它按字母顺序打印,例如

!!!

苹果

自行车

< ~~~>(放箭头,这样就不会把它写成代码了)

等等

我还有另一个问题,第二次输入后,当我输入短语退出时,它并没有像预期的那样结束程序,我该如何解决

谢谢

这是我当前的代码:

#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;
}

标签: cfunctionlinked-listnodes

解决方案


推荐阅读