怎样对单链表排序
跪求:怎样对单链表排序。(求例题)。拜托各位大虾!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tag_student
{
char name[8];
struct tag_student *next;
}student;
void list_sort(student *head)
{
student *p = head;
while(NULL != p->next)
{
student *q = p->next;
while(q)
{
if(strcmp(p->name, q->name) > 0)
{
char tmp[8];
strcpy(tmp, p->name);
strcpy(p->name, q->name);
strcpy(q->name, tmp);
}
q = q ->next;
}
p = p->next;
}
}
int main()
{
student headNode; //头结点
student *p= &headNode; // 头指针,头指针指向头结点,头节点的next指向的是第一个节点
for(int i = 0; i != 5; ++i)
{
p->next = (student *)malloc(sizeof(student));
p = p->next;
p->next = NULL;
scanf("%s", p->name);
}
list_sort(headNode.next);
p= headNode.next;
while(p)
{
printf("%s ", p->name);
p =p->next;
}
}