1

I have this program that I'm trying to modify it, but I don't understand why the statement: struct Link * temp = cap; doesn't print me the number that I assigned to the linked list. Thanks in advance!

struct Link
{
    int data;
    struct Link *urmatorul;
};

void Insert(Link * cap, int n)
{
    struct Link * temp = (Link*)malloc(sizeof(struct Link));
    temp->data = n;
    temp->urmatorul = NULL;
    if(cap != NULL)
        temp->urmatorul = cap;
    cap = temp;
}

void Print(Link * cap)
{
    struct Link *temp = cap;
    printf(" %d", cap->data);
    printf("The number is: ");
    while(temp != NULL)
    {
        printf(" %d", temp->data);
        temp = temp->urmatorul;
    }
    printf("\n");
}

int main()
{
    struct Link * cap;
    cap = NULL;
    printf("How many numbers? \n");
    int x, n, i;
    scanf(" %d", &x);
    for(i = 0; i < x; ++i)
    {
        printf("Enter the number: \n");
        scanf("%d", &n);
        Insert(cap, n);
        Print(cap);
    }
    return 0;
}
3
  • 1
    C passes arguments by value. Your cap is NULL, never changed after Insert(). Commented Feb 6, 2018 at 21:11
  • 1
    Think back to your text-books or class-notes. What do they say about passing arguments to functions? Do you remember anything about it be by value? Which means they are copied, and no matter how much you modify the copy inside the function, the original will never change? Do some research about emulating pass by reference in C. Commented Feb 6, 2018 at 21:12
  • Insert needs to return the new root node of your linked list. Your insert function is leaking memory. Commented Feb 6, 2018 at 21:15

2 Answers 2

2

You need to pass the Link * by reference to change it, that's a Link **

void Insert(Link **cap, int n)
{
    struct Link * temp = (Link*)malloc(sizeof(struct Link));
    temp->data = n;
    temp->urmatorul = NULL;
    if(*cap != NULL)
        temp->urmatorul = *cap;
    *cap = temp;
}

and in your main(...) use

Insert(&cap, n);

or you could return the new Link * from your Insert(...) like this;

Link * Insert(Link * cap, int n)
{
    struct Link * temp = (Link*)malloc(sizeof(struct Link));
    temp->data = n;
    temp->urmatorul = NULL;
    if(cap != NULL)
        temp->urmatorul = cap;
    return temp;
}

and in your main(...) use

cap = Insert(cap, n);
Sign up to request clarification or add additional context in comments.

1 Comment

return cap = temp; is a meaningless assignment. Of course, even a weak optimizer will remove it, but it's semantically incorrect. Just return temp.
1

This line does not do anything, because cap inside Insert is a copy of cap from the main:

cap = temp;

The change gets discarded as soon as Insert exits, so main's cap remains NULL.

Change Insert's signature to return Link*, and assign it to cap in the call from main:

Link* Insert(Link * cap, int n)
{
    struct Link * temp = malloc(sizeof(struct Link)); // No need to cast
    temp->data = n;
    temp->urmatorul = cap; // No need for the conditional
    return temp;
}

The call looks like this:

cap = Insert(cap, n);

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.