链表排序和删除:
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;
}