在链表中按字母顺序添加字符串时,我需要帮助

时间:2019-05-16 17:42:19

标签: c file pointers linked-list

将节点添加到链表时遇到问题。应该按字母顺序添加它们,但是无法将第一个元素添加到列表中。

Local insert_place(Place first, char* string){

    Place n = create_place();
    Place aux = first;

    if(n==NULL)
        return NULL;

    while((aux->abcnext!=NULL)&&(strcmp(aux->abcnext->place, string)<0))
        aux = aux->abcnext;

    n->place= malloc(strlen(string)+1);
    strcpy(n->place, string);
    n->abcnext= aux->abcnext;
    aux->abcnext = n;
}


Place create_place(){
    Place aux;
    aux=(Place)malloc(sizeof(place_node));

    if (aux!=NULL){
        aux->place=malloc(25*sizeof(char));
        aux->abcnext=NULL;
    }
    return aux;
}


typedef struct placenode*Place;

typedef struct placenode{
    char *place;
    Place abcnext;
}place_node;

我的结果是:假设您有4个字符串-“ P”,“ Z”,“ W”,“ L”。我第一次打印时只打印垃圾。第二次打印垃圾,然后按字母顺序打印最低的字符串,依此类推。

0 个答案:

没有答案
相关问题