开发者

Struct linking to linked list

I am new to c++, and am trying to port a program i have made in python to c++. I have a struct that is a linked list with a list of parts. Each of these parts contains one or more components. So i have tried to create two structs, where one struct links to the other struct.

But i dont seem to get the list_part to link to component_list.

struct list_part {
    char partname[100];
    int parttype;
    component_list * comp;
    list_part * next;
};

struct component_list {
    char compname[100];
    component_list * next;
};

I use the following function to add the part to the bottom of the list.

void addpart(char partname[], int parttype, component_list *newcomp) {
    struct list_part *temppart;
    struct list_part *currentpart;

    temppart = (struct list_part *)malloc(sizeof(struct list_part));
    strcpy_s(temppart->partname,partname);
    temppart->parttype = parttype;
    temppart->comp = newcomp;

    currentpart = head;

    if (head == NULL) {
        head = temppart;
        head->next = NULL;
    } else {
        while (currentpart->next != NULL) {
            currentpart = currentpart->next;
        }
        temppart->next = NULL;
        currentpart->next = temppart;
    }
}

And a similar function to add the component to a list.

void addcomp(char compname[]) { 
    struct component_list *tempcomp;
    struct component_list *currentcomp;

    tempcomp = (struct component_list *)malloc(sizeof(struct list_part));
    strcpy_s(tempcomp->compname,compname);

    currentcomp = newcomp;

    if (currentcomp == NULL) {
        currentcomp = tempcomp;
        currentcomp->next = NULL;
    } else {
        while (currentcomp->next != NULL) {
            currentcomp = currentcomp->next;
        }

        tempcomp->next = NULL;
        currentcomp->next = tempcomp;
    }
}

When the first component in a part is present i try to add it with.

struct component_list *newcomp = NULL;
strcpy_s(compname,temp.c_str());
addcomp(compname);

and the rest of the components i was planing to add with these commands

strcpy_s(compname,temp.c_str());
addcomp(compname);

And finally this is added as a part with

addpart(partname,fluidname, parttype, newcomp);

When i do it this way the newcomp only returns 00000000, but i need it to return a pointer to the list with components for th开发者_如何学Pythonis part. I have no idea how to do this really, i am used to dynamic languages, and this is not an issue there. I have figured this is the best way to go about this, but am very open to suggestions for other solutions. As data structures is something i am very fresh at.


Since you are open for suggestions, I think the best suggestion is You should be using std::list. instead of your own linked list implementaion.

std::list is a ready to use STL container provided by the C++ Standard library and it is always going to be more efficient that any list implementation you write.


You have 2 big mistakes in addcomp function, this should work: (also moved some things)

void addcomp(char compname[]) { 
    struct component_list *tempcomp;
    struct component_list *currentcomp;

    tempcomp = (struct component_list *)malloc(sizeof(struct component_list/*FIX 1*/));
    strcpy_s(tempcomp->compname,compname);
    tempcomp->next = NULL; /*Better do it here*/

    if (newcomp == NULL) {
        newcomp = tempcomp;/*FIX 2*/
    } else {
        currentcomp = newcomp; /*Better do it here*/
        while (currentcomp->next != NULL) {
            currentcomp = currentcomp->next;
        }
        currentcomp->next = tempcomp;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜