如何把它按学号顺序从小到大建立有序链表,最后遍历链表,并按顺序输出学生信息
输入
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));
}
}}