如何把它按学号顺序从小到大建立有序链表

如何把它按学号顺序从小到大建立有序链表,最后遍历链表,并按顺序输出学生信息
输入
1
3
20080108 Zhangsan
20070328 Lisi
20070333 Wangwu
输出
20070328 Lisi
20070333 Wangwu
20080108 Zhangsan
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stud_node {

 char   name[20];
 int    xh;
 struct stud_node *next;

};
struct stud_node *head, *tail;

void input();

int main()
{
struct stud_node *p;

head = tail = NULL;
input();
for ( p = head; p != NULL; p = p->next )
    printf("%d %s\n",  p->xh ,p->name);

return 0;

}
void input()
{
struct stud_node *pt;
pt = (struct stud_node *)malloc(sizeof(struct stud_node));
int n,num;
scanf("%d",&n);
while(n--){
scanf("%d",&num);
for(int i=1;i<=num;i++){
scanf("%d %s", &pt->xh,pt->name);
if (head == NULL)
{
head = pt;
head->next = NULL;
}

    if (tail != NULL)
    {
        tail->next = pt;
    }

    
    tail = pt;
    tail->next = NULL;
    pt = (struct stud_node *)malloc(sizeof(struct stud_node));
    
}

}}

修改如下,供参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
    char   name[20];
    int    xh;
    struct stud_node *next;
};
struct stud_node *head, *tail;
void input();
int main()
{
    struct stud_node *p;
    head = tail = NULL;
    input();
    for (p = head; p != NULL; p = p->next )
         printf("%d %s\n",  p->xh ,p->name);
    return 0;
}
void input()
{
    struct stud_node *pt, *ptr;
    int n,num;
    scanf("%d",&n);
    while(n--){
         scanf("%d",&num);
         for(int i=1;i<=num;i++){
             pt = (struct stud_node *)malloc(sizeof(struct stud_node));
             pt->next = NULL;
             scanf("%d %s", &pt->xh,pt->name);
             if (head == NULL)
             {
                 head = pt;
                 head->next = NULL;
             }
             else   //if (tail != NULL)
             {
                 tail = head; ptr = NULL;
                 while (tail && tail->xh < pt->xh){
                       ptr = tail;
                       tail = tail->next;
                 }
                 if (ptr == NULL){
                     pt->next = head;
                     head = pt;
                 }
                 else{
                     pt->next = ptr->next;
                     ptr->next = pt;
                 }
             }
             //tail = pt;
             //tail->next = NULL;
             //pt = (struct stud_node *)malloc(sizeof(struct stud_node));
         }
    }
}

数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633