Adding an element to the end in linked list C program
<p>This program is meant to insert an element in the middle of a linked list, but I left out functions that aren't in question.</p>

<p>At first I wrote it with assigning HEAD and CURRENT elements to NULL globally and it worked fine. With locally assigned variables in <code>main()</code> it doesn't work. Specifically, the <code>while</code> loop in <code>main</code> is infinite because of the faulty <code>insertDataToEnd</code> function. How could I fix it? Also, before I wrote <code>insertDataToEnd</code> differently and it printed only first and last elements of the list, could the problem be with <code>printList</code> ? </p>

<p>EDIT (again): still having some issues processing all the new information on structures. Now I have this sortList function to swap elements so they would be in inclining order. I get an error only when the function is used.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct node {
    int data;
    struct node *next;
}node_t;

void insertDataToEnd(int value, struct node **head){
    struct node *link = (struct node*) malloc(sizeof(node_t));
    link -&gt; data = value;
    link -&gt; next = NULL;
    node_t *current = *head;
    if(*head == NULL){
        *head = link;
    }
    else{
        while(current -&gt; next != NULL){
        current = current -&gt; next;
        }
    current -&gt; next = link;

    }
}

void printList(struct node* head){
    node_t *current = head;
    while(current != NULL){
        printf("%d -&gt; ", current -&gt; data);
        current = current -&gt; next;
    }
    printf("NULL\n");
}

void sortList(int count, struct node* head){
    int i, j, temp;
    count += 1;
    struct node *current;
    struct node *next;
    for(i = 0; i &lt; count; i++){
        current = head;
        next = current -&gt; next;
        for(j = 1; j &lt; count + 1; j++){
            if(current -&gt; data &gt; next -&gt; data){
                temp = current -&gt; data;
                current -&gt; data = next -&gt; data;
                next -&gt; data = temp;
            }
            current = current-&gt;next;
            next = next-&gt;next;
        }
    }
}

void insertElement(int value, int k, struct node** head){
    node_t *elemk = (struct node*) malloc (sizeof(node_t));
    node_t *elem = (struct node*) malloc (sizeof(node_t));
    elemk = *head;
    int i = 2;
    while (i &lt; k &amp;&amp; elemk != NULL){
        elemk = elemk -&gt; next;
        i++;
    }
    if(i == k){
        printf("element inserted.\n", k, value);
         elem -&gt; data = value;
         elem -&gt; next = elemk -&gt; next;
         elemk -&gt; next = elem;
    }
    else printf("error.\n");
}

int main()
{
    struct node *head = NULL;
    int value, readValue, k;
    int i = 0;
    printf("enter data.\n");
    while(1){
        scanf("%d", &amp;value);
        insertDataToEnd(value, &amp;head);
        i++;
        if (i == 4) break;
    }
    sortList(i, head);
    printf("insert element\n");
    scanf("%d %d", &amp;readValue, &amp;k);
    insertElement(readValue, k, &amp;head);
    printList(head);
    return 0;
}
</code></pre>