English 中文(简体)
无法用我的目标值检查第一个节点值, 以避免重复我的位置_ 第一个函数中的重复
原标题:Can t check first node s value with my target value in order to avoid duplication in my place_first function

As always being this problem comes from a book s exercises. Neither I am not studying data structures, nor the book is about that. But, there is a chapter that is "Dynamic Data Structures". I ve already finished this chapter.

但是,我对插入有异议。在我看来,我的功能是正常的,除了它产生重复的节点之外。

I made a precaution for that but it doesn t work. Anyway, please forgive me because of my silly mistakes. OK, Here my structure types for name lists.

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;

位置_ 第一个函数 :

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");

My output like this: __Mustafa __Mustafa __Ataturk __Ali __Eisenhower __Kennedy Thanks for advance...

最佳回答

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

您是在比较指针,而不是内容,您想要的

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

其他检查也是如此。

问题回答

感谢@dfan和@Daniel Fisher。

现在,我明白了我愚蠢的问题。

我对自己的职能做了一些改变。

那么,我做了什么.......

我为检查第一个节点添加了这一声明;

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){
...

完整功能(也许有人需要):

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




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签