链表创建、排序和删除

链表排序和删除:

struct 
{
	int no;
	char * name;
	int depno;
	float score ;
	struct node *next;
}stu[5]={
{0,"张三",10005,88.0},
{1,"李四",10005,65.0},
{2,"王六",10005,92.0},
{3,"赵四",10005,55.0},
{4,"孙二",10005,78.0}
};

创建

typedef struct node
{
	int no;
	char * name;
	int depno;
	float score;
	struct node *next;
}ElemType;

ElemType* creatlist(int no[],char * name[],int depno[],float score[])
{
	ElemType *tail,*s,*h;
	int n=5,i=0;
	h=tail=(ElemType*)malloc(sizeof(ElemType));
	tail->no=no[i];
	tail->score=score[i];
	tail->depno=depno[i];
	tail->name=name[i];
	tail->next=NULL;
	for(i=n-1;i>0;i--)
	{
		s=(ElemType*)malloc(sizeof(ElemType));		
		s->no=no[i];
		s->score=score[i];
		s->depno=depno[i];
		s->name=name[i];
		s->next=tail->next;
		tail->next=s;
	}
	return h;
}

删除的方法:
p为要删位置的前一个位置,
dele=p->next,也就是dele为该删除的位置

ElemType * del(ElemType *h)
{
	int a;
	ElemType * dele;
	printf("请输入要删除学生的学号号:");
	scanf("%d",&a);
	for(ElemType *p=h;p;p=p->next)
	{
		if(p->next->no==a)
		{
			dele=p->next;
			p->next=dele->next;
			free(dele);//释放
			printf("已删除工号为%d的职工记录\n",a);	
			break;
		}
	}
	return h;
}

排序:
排序的方法是排期中数据域的序,所以并不需要将整个链表进行交换,只需要将数据域中的内容进行交换即可。

ElemType *judgescore(ElemType *h)
{
	if(h->next==NULL) //如果链表为空,则输出该句话并直接返回
	{
		printf("链表为空\n");
		return h;
	}
	ElemType *q,*p;
	q=h;
	p=NULL; 
	while(q!=p){
		while(q->next!=p){
			if(q->score>cur->next->score){
				float temp=q->score;//本质就是两个数的换位
				q->score=q->next->score;
				q->next->score=temp;
				int temp1=q->no;
				q->no=q->next->no;
				q->next->no=temp1;
				int temp2=q->depno;
				q->depno=q->next->depno;
				q->next->depno=temp2;
				char * temp3=q->name;
				q->name=q->next->name;
				q->next->name=temp3;
			}
			q=q->next;
		}
		p=q;
		q=h;
	}
	printf("已排序完毕\n");
	return h;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值