Single linked list, self sorting based on *char input
<p>I have spent significant amount of time on this and I have finally made it work at all (as in it saves the values and prints them). I want to sort it counter alphabetically. If I add the values into my list they do not print out in the same order I have added them in but it seems kind of random... It seems that the strcpy doesn't work in the way I would imagine it to work...</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
using namespace std;


typedef struct list list_t;
struct list{
    list *next;
    char *nam;
    int age;

};

static list_t *top = NULL;


void add_value(char * name1, int age){
    list * neww = (list_t*)malloc(sizeof( list_t));
    //tmp = top;
    neww-&gt;age = age;
    neww-&gt;nam = (char*)malloc(strlen(name1) + 1);
    neww-&gt;next = NULL;
    strcpy(neww-&gt;nam, name1);
    list * tmp = (list_t*)malloc(sizeof(list_t));
    tmp = top;
    if (tmp==NULL){
        top = neww;
        //printf("%s\n", top-&gt;nam);
    }else
    while (1){


        if (tmp-&gt;next == NULL){
            tmp-&gt;next = neww;
            break;
        }
        //printf("top - %s %d\n neww - %s %d\n tmp - %s %d", top-&gt;nam, top-&gt;age, neww-&gt;nam, neww-&gt;age, tmp-&gt;nam, tmp-&gt;age);
        if (strcmp(neww-&gt;nam, tmp-&gt;nam)&gt;=0){
            neww-&gt;next = tmp-&gt;next;
            tmp-&gt;next = neww;
            break;
        }

        tmp = tmp-&gt;next;


    }

}


void print(){
    list * tmp = (struct list*)malloc(sizeof(struct list));
    tmp = top;
    while (tmp){
        printf("%s %d\n", tmp-&gt;nam, tmp-&gt;age);
        tmp = tmp-&gt;next;

    }


}


int main(){

    char namee[100];
    int age;
    for (int i = 0; i &lt; 5; i++){
        scanf("%s %d", &amp;namee, &amp;age);
        add_value(namee, age);
    }

    print();

    return 0;
}
</code></pre>