0

一如既往,这个问题来自一本书的练习。我不是在研究数据结构,这本书也不是关于这个的。但是,有一章是“动态数据结构”。我已经读完了这一章。

但是,我有插入问题。在我看来,我的功能正常工作,除了它会产生重复的节点。

我为此做了预防措施,但它不起作用。无论如何,请原谅我因为我愚蠢的错误。好的,这是我的名称列表的结构类型。

typedef struct name_node_s {
char name[11];
struct name_node_s *restp;
}name_node_t;

typedef struct {
    name_node_t *headp;
    int size;
}name_list_t;

place_first 功能:

name_node_t *
place_first(name_list_t *old_listp, char name[11])
{
    name_list_t *new_listp, *cur_listp;
    name_node_t *new_nodep, *temp_nodep;
    temp_nodep = (name_node_t *)malloc(sizeof (name_node_t));
    new_listp = (name_list_t *)malloc(sizeof (name_list_t));
    cur_listp = (name_list_t *)malloc(sizeof (name_list_t));
    new_nodep = (name_node_t *)malloc(sizeof (name_node_t));
    cur_listp->headp = old_listp->headp;
    temp_nodep = old_listp->headp;
    new_listp = old_listp;
    if ( old_listp->headp->name != name ){ // My first precaution for duplication
        while(cur_listp->headp->restp != NULL && cur_listp->headp->name != name) // My second precaution for duplication
        {
            if (old_listp->headp == NULL){
                strcpy(new_listp->headp->name, name);
                new_listp->headp->restp = NULL;
            }
            else if (old_listp->headp->name != name) { // Third one.
                strcpy(new_nodep->name, name);
                new_nodep->restp = NULL;
                new_listp->headp = new_nodep;
                new_listp->headp->restp = temp_nodep;
                ++(old_listp->size);
            }
        cur_listp->headp = cur_listp->headp->restp;
        }
    }
    else{
        new_listp->headp = old_listp->headp;
    }
    return(new_listp->headp);
}

我这样称呼那个函数;

listp->headp = place_first(listp, "Mustafa");
listp->headp = place_first(listp, "Mustafa");

我的输出是这样的:__Mustafa __Mustafa __Ataturk __Ali __Eisenhower __Kennedy 感谢您的提前...

4

2 回答 2

0

这里

if ( old_listp->headp->name != name ){

你正在比较指针,而不是内容,你想要

if (strcmp(old_listp->headp->name,name)){

这同样适用于其他检查。

于 2012-05-25T19:42:41.913 回答
0

Thanks @dfan and @Daniel Fisher.

Now, I understood my silly problem.

I have made some changes to my function.

So what did I...

I've added this statement for checking first node;

if ( strcmp(old_listp->headp->name, name) != 0 ){

And added a for loop for checking other nodes:

for( cur_listp->headp = old_listp->headp;
                cur_listp->headp != NULL && strcmp(cur_listp->headp->name, name)!= 0;
                cur_listp->headp = cur_listp->headp->restp) { }

If it returns null my other statements will execude otherwise the old nodes will return.

if ( cur_listp->headp == NULL){
...

Full function ( maybe somebody will need it):

name_node_t *
place_first(name_list_t *old_listp, char name[11])
{
    name_list_t *new_listp, *cur_listp;
    name_node_t *new_nodep, *temp_nodep;
    temp_nodep = (name_node_t *)malloc(sizeof (name_node_t));
    new_listp = (name_list_t *)malloc(sizeof (name_list_t));
    cur_listp = (name_list_t *)malloc(sizeof (name_list_t));
    new_nodep = (name_node_t *)malloc(sizeof (name_node_t));
    cur_listp->headp = old_listp->headp;
    temp_nodep = old_listp->headp;
    new_listp = old_listp;
    if ( strcmp(old_listp->headp->name, name) != 0 ){
        for( cur_listp->headp = old_listp->headp;
                cur_listp->headp != NULL && strcmp(cur_listp->headp->name, name)!= 0;
                cur_listp->headp = cur_listp->headp->restp) { }
        if ( cur_listp->headp == NULL){
            if (old_listp->headp == NULL){
                strcpy(new_listp->headp->name, name);
                new_listp->headp->restp = NULL;
            }
            else {
                strcpy(new_nodep->name, name);
                new_nodep->restp = NULL;
                new_listp->headp = new_nodep;
                new_listp->headp->restp = temp_nodep;
                ++(old_listp->size);
            }
        }
        else
            new_listp->headp = old_listp->headp;
    }
    else{
        new_listp->headp = old_listp->headp;
    }
    return(new_listp->headp);
}
于 2012-05-25T20:24:00.380 回答