Platon Makovsky
Platon Makovsky

Reputation: 285

Can't traverse through a C linked list

Task is to create a linked list consisting of objects. User inputs data for each individual Node in the main and then the object is being passed to push, which creates the list.

The problem comes in the printList function, where the condition for a break is never met. For some reason the line head = head->next doesn't do anything, as the address of next with every iteration remains the same.

typedef struct Node {
    int a;
    char asd[30];
    struct Node *next;
}Node;

Node *head = NULL;

void push(Node**head, struct Node* object);
void printList(Node *head);

int main() {

    struct Node {
        int oA;
        char oAsd[30];
        struct Node *next;
    };

    struct Node *object = malloc(sizeof(struct Node));

    int c = 0;
    while (1) {
        printf("This int will be stored in Node %d.\n", ++c);
        scanf("%d", &object->oA);
        getchar();
        if (!object->oA) {
            break;
        }
        printf("This string will be stored in Node %d.\n", c);
        gets_s(object->oAsd, 30);
        if (!(strcmp(object->oAsd, "\0"))) {
            break;
        }
        push(&head, object);
    }
    printList(head);

    return 0;
}

void push(Node ** head,  struct Node* object)
{
    Node *tmp = malloc(sizeof(Node));
    tmp = object;
    tmp->next = (*head);
    (*head) = tmp;
}


void printList(Node *head) {
    if (head == NULL) {
        puts("No list exists.");
        exit(9);
    }

        while (1) {
            printf("-------------------------------\n");
            printf("|Int: <%d> |||| String: <%s>.|\n", head->a, head->asd);
            printf("-------------------------------\n");
            if (head->next) {
                printf("\n\n%p\n\n", head->next);

                head = head->next;
            }
            else {
                break;
            }
        }
}`

Upvotes: 0

Views: 84

Answers (1)

4386427
4386427

Reputation: 44274

There are two major problems in your code:

  • You define struct Node both outside main and inside main

  • Here tmp = object; you copy the value of a pointer to another pointer but you really want to copy the value of a struct to another struct, i.e. *tmp = *object;.

Besides that - don't put head as a global variable.

So the code should be more like:

typedef struct Node {
    int a;
    char asd[30];
    struct Node *next;
}Node;

void push(Node**head, struct Node* object);
void printList(Node *head);

int main() {
    Node *head = NULL;

    struct Node *object = malloc(sizeof(struct Node));

    int c = 0;
    while (1) {
        printf("This int will be stored in Node %d.\n", ++c);
        scanf("%d", &object->a);
        getchar();
        if (!object->a) {
            break;
        }
        printf("This string will be stored in Node %d.\n", c);
        gets_s(object->asd, 30);
        if (!(strcmp(object->asd, "\0"))) {
            break;
        }
        push(&head, object);
    }
    printList(head);

    return 0;
}

void push(Node ** head,  struct Node* object)
{
    Node *tmp = malloc(sizeof(Node));
    *tmp = *object;                    // Copy the struct
    tmp->next = (*head);
    (*head) = tmp;
}


void printList(Node *head) {
    if (head == NULL) {
        puts("No list exists.");
        exit(9);
    }

        while (1) {
            printf("-------------------------------\n");
            printf("|Int: <%d> |||| String: <%s>.|\n", head->a, head->asd);
            printf("-------------------------------\n");
            if (head->next) {
                printf("\n\n%p\n\n", head->next);

                head = head->next;
            }
            else {
                break;
            }
        }
}

Upvotes: 3

Related Questions