0

I'm basically trying to implement LinkedList in C but I still can't get it I will leave my code below so you can see what I'm missing:

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int val;
    struct Node *Next;
};

struct Node *head;

void insert(int x);
void print();

int main() {
    head = NULL;
    int d, i = 0, f;
    printf("How many number you want to insert:\n");
    scanf("%d", &d);
    for (i; i < d; i++) {
        printf("Enter your number:\n");
        scanf("%d", &f);
        insert(f);
        print();
    }
    return 0;
}

void insert(int x) {
    struct Node *A = (struct Node *)malloc(sizeof(struct Node));
    if (head == NULL) {
        A->val = x;
        A->Next = head;
        head = A;
        return;
    }
    A->val = x;
    A->Next = head->Next;
}

void print() {
    printf("[");
    struct Node *B = head;
    while (B->Next != NULL) {
        B = B->Next;
        printf("%d", B->val);
    }
    printf("]\n");
}

I need some pointers to where I missed up.

5
  • 1
    Three C++ tags on a C question. I do not recommend using more than zero. Commented Feb 7, 2021 at 23:57
  • The best way to figure out linked lists is by drawing pictures. Draw the list before an operation. Draw what it should look like after the operation. Draw the steps between as necessary. When you can visualize how the pieces are put together, you can usually figure out what code you need. Commented Feb 7, 2021 at 23:59
  • When asking a question it's in your best interests to clearly state what the program is doing and what you want it to do. This allows potential answerers to focus in on one problem. If you leave the question wide open, you may get many differing answers or, just as likely, no answers because the question is closed for lacking clarity. Commented Feb 8, 2021 at 0:01
  • Your insert function is broken for the case when head is non-null--it does nothing. But, this brings up the question: Do you want insert to prepend to the front of the list or append to the tail of the list? Commented Feb 8, 2021 at 0:02
  • You may want to compare with Singly Linked List (node only, no wrapper) and see where the pointer handling differences are. See Linus on Understanding Pointers for explanation of iterating using both the address-of the pointer and pointer itself to avoid having to keep track of the previous node for deletions and insertions within the list. Commented Feb 8, 2021 at 0:02

2 Answers 2

1

Here are the two ways to insert, either at the front/head of the list or the back/tail of the list:

void
insert_front(int x)
{
    struct Node *A = malloc(sizeof(*A));

    A->val = x;

    A->Next = head;
    head = A;
}

void
insert_back(int x)
{
    struct Node *A = malloc(sizeof(*A));

    A->val = x;
    A->Next = NULL;

    struct Node *prev = NULL;
    for (struct Node *cur = head;  cur != NULL;  cur = cur->Next)
        prev = cur;

    if (prev != NULL)
        prev->Next = A;
    else
        head = A;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The print() function is incorrect: it misses the first node and would crash on an empty list:

void print() {
    printf("[");
    struct Node *B = head;
    while (B != NULL) {
        printf(" %d", B->val);
        B = B->Next;
    }
    printf(" ]\n");
}

The insert() function also has problems: only the first node is correctly inserted. Here is a modified version:

// insert a node in front of the list.
void insert(int x) {
    struct Node *A = (struct Node *)malloc(sizeof(struct Node));
    if (A != NULL) {
        A->val = x;
        A->Next = head;
        head = A;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.