p = head; //有序链表不为空
while(p->num < node->num && p != NULL) //p指向的节点的学号比插入节点的学号小,并且它不等于NULL
{
t = p; //保存当前节点的前驱,以便后面判断后处理
p = p->next; //后移一个节点
}
if (p == head) //刚好插入第一个节点之前
{
node->next = p;
head = node;
}
else //插入其它节点之后
{
t->next = node; //把node节点加进去
node->next = p;
}
n += 1; //插入完毕,节点总数加1
return head;
}
/*
以上函数的测试程序:
提示:根据测试函数的不同注释相应的程序段,这也是一种测试方法。
*/
int main(void)
{
struct student *head;
struct student *stu;
int thenumber;
// 测试Create()、Print()
head = Create();
Print(head);
//测试Del()
printf("nWhich one delete: ");
scanf("%d",&thenumber);
head = Del(head,thenumber);
Print(head);
//测试Insert()
stu = (struct student *)malloc(LEN);
printf("nPlease input insert node -- num,score: ");
scanf("%d %f",&stu->num,&stu->score);
printf("nInsert behind num: ");
scanf("%d",&thenumber);
head = Insert(head,thenumber,stu);
Print(head);
//测试Reverse()
printf("nReverse the LinkList: n");
head = Reverse(head);
Print(head);
//测试SelectSort()
printf("nSelectSort the LinkList: n");
head = SelectSort(head);
Print(head);
//测试InsertSort()
printf("nInsertSort the LinkList: n");
head = InsertSort(head);
Print(head);
//测试BubbleSort()
printf("nBubbleSort the LinkList: n");
head = BubbleSort(head);
Print(head);
printf("nSortInsert the LinkList: n");
//测试SortInsert():上面创建链表,输入节点时请注意学号num从小到大的顺序
stu = (struct student *)malloc(LEN);
printf("nPlease input insert node -- num,&stu->score);
head = SortInsert(head,stu);
Print(head);
//销毁链表
DestroyList(head);
printf ("n");
system ("pause");
} (编辑:广西网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|