您的位置:首页 > 其它

单链表的创建,删除,插入,显示,排序

2008-04-25 21:22 369 查看

#include <stdio.h>


#include <malloc.h>




struct info




...{


int num;


int score;


};




struct student




...{


struct info infor;


struct student *next;


};


int n;




struct student *creat(void)




...{


struct student *head, *p1, *p2;


n = 0;


p1 = p2 = (struct student *)malloc(sizeof(struct student));


scanf("%d, %d", &p1->infor.num, &p1->infor.score);


head = NULL;


while (p1->infor.num != 0 )




...{


n++;


if (n == 1)


head = p1;


else


p2->next = p1;


p2 = p1;


p1 = (struct student *)malloc(sizeof(struct student));


scanf("%d, %d", &p1->infor.num, &p1->infor.score);


}


p2->next = NULL;


return head;


}




void show(struct student *head)




...{


struct student *p;


printf(" Now, These %d records are: ", n);


p = head;


if (head != NULL)




...{


do




...{


printf("%d %d ", p->infor.num, p->infor.score);


p = p->next;


}while (p != NULL);


}


}




struct student *insert(struct student *head, struct student *stud)




...{


struct student *p1, *p2, *p3;


p2 = head;


p1 = stud;


if (head == NULL)




...{


head = p1;


p1->next = NULL;


}


else




...{


while (p1->infor.num > p2->infor.num && p2->next != NULL)




...{


p3 = p2;


p2 = p2->next;


}


if (p1->infor.num <= p2->infor.num)




...{


if (head == p2)




...{


head = p1;


}


else




...{


p3->next = p1;


}


p1->next = p2;


}


else




...{


p2->next = p1;


p1->next = NULL;


}


}


n++;


return head;


}




struct student *del(struct student *head, int num)




...{


struct student *p1, *p2;


if (head == NULL)




...{


printf(" , list null ");


return head;


}


p1 = head;




while (p1->infor.num != num && p1->next != NULL)




...{


p2 = p1;


p1 = p1->next;


}




if (p1->infor.num == num)




...{


if (p1 == head)




...{


head = p1->next;


}


else




...{


p2->next = p1->next;


}


printf(" delete: %d ", num);


n--;


}


if (p1 == NULL)




...{


printf(" %d not been found ", num);


}


return head;


}




struct student *sort(struct student *head)




...{


struct student *p1, *p2;


struct info temp;


if (head == NULL)




...{


printf(" list null ");


return head;


}


p1 = head;


p2 = p1->next;


while (p1 != NULL)




...{


while (p2 != NULL)




...{


if (p1->infor.score > p2->infor.score)//可以设置为根据成绩排序或学好排序




...{


temp = p1->infor;


p1->infor = p2->infor;


p2->infor = temp;


}


p2 = p2->next;


}


p1 = p1->next;


if (p1->next == NULL)




...{


break;


}


else




...{


p2 = p1->next;


}


}


return head;


}




int main()




...{




struct student *head, *stu;


int num;




printf("input records: ");


head = creat();


show(head);




printf(" input the deleted number:");


scanf("%d", &num);


while (num != 0)




...{


head = del(head, num);


show(head);


printf(" input the deleted number:");


scanf("%d", &num);


}




printf(" input the inserted record:");


stu = (struct student *)malloc(sizeof(struct student));


scanf("%d, %d", &stu->infor.num, &stu->infor.score);


while (stu->infor.num != 0)




...{


head = insert(head, stu);


show(head);


printf(" input the inserted record:");


stu = (struct student *)malloc(sizeof(struct student));//重新分配一次内存,以免覆盖掉上一次的节点


scanf("%d, %d", &stu->infor.num, &stu->infor.score);


}




head = sort(head);


show(head);




return 0;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐