1
votes

I have been working on a program in C99 which is based heavily around structs. I found that I could create linked lists of structs, and thought to give it a try.

The following is a miserable attempt, reworked about 50 times, that is meant to do the following:

1) Create a struct of type BASE in the main method, which contains the head of a linked list (CHAIN).

2) Pass this BASE struct to another function, which will append additional CHAIN elements to the end of the linked list.

3) Print the linked lists elements in main, as proof to myself that the changes are not just in the other method.

#include <stdlib.h>
#include <stdio.h>
typedef struct Base {
    //many things
    struct Chain *head;
} BASE;

typedef struct Chain {
    int x;
    struct Chain *next;
} CHAIN;

void extendChain(BASE *Data, int length);

int main() {
    BASE Data;
    CHAIN *pointer;
    Data.head = 0;
    int length = 10; //userInput(); // gets integer
    extendChain(&Data, length);
    pointer = Data.head;
    while (pointer) {
        printf("%d\n", pointer->x);
        pointer = pointer->next;
    }
}

void extendChain(BASE *Data, int length) {
    CHAIN *position;
    position = Data->head;
    for (int i=0; i<length; ++i) {
        if (!Data->head) {
            // this will set the first value, the first time this is run.
            Data->head = malloc(sizeof(CHAIN));
            Data->head->x = -1; // set values here. Irrelevant.
            position = Data->head;
        } else if (position) {
            while (position->next) {
                position = position->next;
            }
            position = malloc(sizeof(CHAIN));
            position->next = 0;
            position->x = i; // placeholder
        }
    }
}

This has turned out terribly, and I realize that my example doesn't begin to work even in theory (but I gave it my best shot). I'm beginning to think that the only way to do this is if I do it all in the same method, which I successfully managed to do earlier, however this will quickly become messy, and a method would definitely be best.

Does anyone have a creative way of adding X elements to a linked list when passed only a struct containing the header of this linked list? Much appreciated, as always.

2
you should break your code into small units(methods)..That would help you to focus,manage,maintain,change,find bug easilyAnirudha

2 Answers

1
votes

Logical errors in your code. This code worked:

void extendChain(BASE *Data, int length) {
    CHAIN *position;
    position = Data->head;
    int i;·
    for (i=0; i<length; ++i) {
        if (!Data->head) {
            // this will set the first value, the first time this is run.
            Data->head = malloc(sizeof(CHAIN));
            Data->head->x = -1; // set values here. Irrelevant.
            Data->head->next = NULL; // <========= 
            position = Data->head; 
        } else if (position) {
            while (position->next) {
                position = position->next;
            }
            CHAIN * position_new_node = malloc(sizeof(CHAIN)); // <========= 
            position_new_node->next = 0; // <========= 
            position_new_node->x = i; // placeholder // <========= 
            position->next = position_new_node; // <========= 
        }
    }
}
0
votes

Algorithm

Create the original linked list Send the linked list to be added, to the function Traverse till the end of first linked list (Let the pointer be t) repeat: node next to t=next node of new linked list move both nodes one unit forward till the next node of new linked list is Null