简单链表

 #include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student*next;
};
int n;

struct student*creat(void)
{
    struct student*head;
    struct student*p1,*p2;
    n=0;
    p1=p2=(struct student* )malloc(LEN);
    scanf("%ld,%f",&p1->num,&p1->score);
    head=NULL;
    while(p1->num!=0)
    {n=n+1;
    if(n==1)
    head=p1;
    else
    p2->next=p1;
    p2=p1;
    p1=(struct student* )malloc(LEN);
    scanf("%ld,%f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return(head);
}
void print(struct student *head)
{
    struct student *p;
    printf("这%d个记录分别是:/n",n);
    p=head;
    if(head!=NULL)
    do
    {
         printf("%d,%5.1f/n",p->num,p->score);
         p=p->next;
    }while(p!=NULL);
}


struct student*find(struct student *head,long num)
{
   struct student *p1,*p2;
   if(head==NULL){printf("链表为空/n");goto end;}
   p1=head;

   while(num!=p1->num&&p1->num!=NULL)
   {p2=p1;p1=p1->next;}

   if(num==p1->num)
    printf("查找元素为%d,它的值为%f",p1->num,p1->score);
   else
    printf("没找到");
   end:
   return(head);
}


struct student *del(struct student * head,long num)
{
   struct student *p1,*p2;
   if(head==NULL){printf("链表为空/n");goto end;}
   p1=head;

   while(num!=p1->num&&p1->num!=NULL)
   {p2=p1;p1=p1->next;}

   if(num==p1->num)
   {
    if(p1==head)
    head=p1->next;
    else
    p2->next=p1->next;
    printf("delete:%d/n",num);
    n=n-1;
   }
   else
    printf("没找到");
   end:
   return(head);
}


struct student * paixu(struct student *head)
{
    struct student*p1,*p2;
    if(head==NULL){printf("链表为空/n");goto end;}
    int i,j;
    float temp;int m;
    for(i=0;i<n;i++)
    {p1=p2=head;
    p1=p1->next;
    for(j=0;j<=n-i;j++)
    {if(p1->score>p2->score)
    {
        temp=p1->score;m=p1->num;
        p1->score=p2->score;p1->num=p2->num;
        p2->score=temp;p2->num=m;
        if(p1->next!=NULL)
        {p1=p1->next;p2=p2->next;}
    }
    }
    }
    return(head);
    end:
    return(head);
}


struct student * insert(struct student *head,struct student*stud)
{
    struct student *p1,*p2,*p3;
    p1=head;
    p2=stud;
    if(head==NULL)
    {head=p2;p2->next=NULL;}
     else
     while((p2->num>p1->num)&&(p1->next!=NULL))
      {p3=p1;
      p1=p1->next;
       }
      if(p2->num<=p1->num)
     {if(p1==head)
       {head=p2;p2->next=p1;}
       else
        p3->next=p2;
        p2->next=p1;
      }
        else
        {p1->next=p2;
        p2->next=NULL;
        }
        n=n+1;
        return(head);
}


main()
{
struct student *head,*stu;
head=NULL;
long del_num;
long find_num;
int a=0;
l:printf("/n请输入要执行的操作序号:/n");
printf("0 退出/n1 创建/n2 删除/n3 插入/n4 查找/n5 排序/n");
scanf("%d",&a);
     if(a==0)goto l0;
else if(a==1)goto l1;
else if(a==2)goto l2;
else if(a==3)goto l3;
else if(a==4)goto l4;
else if(a==5)goto l5;
else {printf("输入错误,请重新输入");goto l;}

l1:printf("input records:/n");
head=creat();
print(head);
goto l;
l2:printf("/n输入要删除的序号:/n");
scanf("%ld",&del_num);
while(del_num!=0)
{
    head=del(head,del_num);
    print(head);
    printf("输入要删除的序号:");
    scanf("%d",&del_num);
}
goto l;
l3:printf("/n输入要插入的序号:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
    head=insert(head,stu);
    print(head);
    printf("输入要插入的序号:");
    stu=(struct student *)malloc(LEN);
    scanf("%ld,%f",&stu->num,&stu->score);
}
goto l;
l4:printf("/n输入要查找的序号:");
scanf("%ld",&find_num);
while(find_num!=0)
{
    head=find(head,find_num);
    print(head);
    printf("输入要查找的序号:");
    scanf("%d",&find_num);
}
goto l;

l5:paixu(head);
   print(head);
   goto l;

l0: printf("%d",n);
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值