链表的创建

#include<stdio.h>
#include<stdlib.h>
typedef struct student{
long num;
char name[11];
float score;
struct student *next;
}Linklist;
//带头结点的头插法
Linklist *creat(){
Linklist *head,*node,*end;
int count=0;
head = (Linklist *)malloc(sizeof(Linklist));
end = head;
node = (Linklist *)malloc(sizeof(Linklist));
printf(“请输入学号姓名成绩:\n”);
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
while(node->score!=-1){
if(count1){
end->next = node;
node->next = NULL;
}
else{
node->next = end->next;
end->next = node;
}
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
}
free(node);
return head;
}
//头插法创建不带头节点的链表
Linklist *creat1()
{
Linklist *head,*node;
int count=0;
node = (Linklist *)malloc(sizeof(Linklist));
printf(“请输入学号姓名成绩:\n”);
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
while(node->score!=-1){
if(count
1){
head = node;
node->next = NULL;
}
else{
node->next = head;
head = node;
}
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
}
free(node);
return head;
}

//带头结点的尾插法创建链表
Linklist * create()
{
Linklist *head,*node,*end;
int count=0;
head = (Linklist *)malloc(sizeof(Linklist));
end = head;
printf(“请输入学号姓名成绩:\n”);
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
while(node->score!=-1){
end->next = node;
end = node;
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
}
free(node);
end->next = NULL;
return(head);
}

//不带头结点尾插法创建链表
Linklist * create1()
{
Linklist *head,*node,*end;
int count=0;
head = NULL;
printf(“请输入学号姓名成绩:\n”);
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
while(node->score!=-1){
if(count==1){
head = node;
end = node;
node->next = NULL;
}
else{
end->next = node;
end = node;
node->next =NULL;
}
node = (Linklist *)malloc(sizeof(Linklist));
scanf("%ld %s %f",&node->num,node->name,&node->score);
count++;
}
free(node);
return(head);
}

//用于打印带头结点的
void print(Linklist *p)
{
p = p->next;
while(p!=NULL){
printf("%ld %s %.1f\n",p->num,p->name,p->score);
p = p->next;
}
}
//用于打印不带头结点的
void print1(Linklist *p)
{
while(p!=NULL){
printf("%ld %s %.1f\n",p->num,p->name,p->score);
p = p->next;
}
}
//冒泡排序
void mpsort(Linklist *head,int n)
{
int i,j;
float temp;
Linklist *p,*p1,*t;

for(i=0;i<n-1;i++){
	t = head->next;
	for(j=0;j<n-i-1;j++){
		p = t;
		p1 = t->next;
		if(p->score>p1->score){
			temp = p->score;
			p->score = p1->score;
			p1->score = temp;
		}
		t = t->next;
	}
}

}

int length_list(Linklist *p)
{
int count=0;
Linklist *t;
t = p->next;
while(t!=NULL){
count++;
t = t->next;
}
return count;
}
int main()
{
Linklist *head;
int n=0;
// head = creat();
// print(head);
// head = creat1();
// print1(head);
head = create();
n = length_list(head);
printf("%d\n",n);
mpsort(head,n);
print(head);
// head = create1();
// print1(head);
free(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值