链表的删除,插入,查找,排序

输入学号,再输入名字,最后输入成绩,实现输入和输出。

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct Student)


struct Student
{
int num;
char name[20];
float score;
struct Student *next;
};

int n,h;

struct Student *creat(void)/*动态链表的创建。。*/
{
n=0;
    struct Student *head,*p1,*p2;
    p1=p2=(struct Student *)malloc(LEN);
    printf("                 ");
    scanf("%d%s%f",&p1->num,p1->name,&p1->score);
    printf("                 ");
    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("%d%s%f",&p1->num,p1->name,&p1->score);
        printf("                 ");
    }
    p2->next=NULL;
    return(head);/*返回的是一个指针类的地址,方便main函数的输出。。*/
}


void print(struct Student *head)/*相当于int max(int x,int y),struct Student相当于int,head相当于head。。*//*这个函数即输出(终结者)*/
{
    struct Student *p;
    p=head;
    while(p!=NULL)
{
        printf("                 %d %s %5.1f\n",p->num,p->name,p->score);
p=p->next;
}
}


struct Student *del(struct Student *head)/*动态链表的节点删除。。*/
{
    printf("\n=============================================");
    printf("\n                 |开始删除学号|                ");
    printf("\n=============================================\n");
    printf("请输入:");
scanf("%d",&n);
    printf("- - - - - - - - - - - - - - - - - - - - - - -");
    struct Student *p1,*p2;
    p1=p2=head;
    while(p1->num!=n&&p1->next!=NULL)/*这个函数是在找所要的节点位置。。*/
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num==n)
    {
        if(p1==head)/*当删除的是第一个节点的时候的讨论。。*/
            head=p1->next;/*地址的传送(开始改变),没有赋值,即把它隔了过去。。*/
        else
            p2->next=p1->next;/*地址的传送(开始改变),没有赋值,即把它隔了过去。。*/
        printf("\n已经删除学号为%d同学的信息.\n=============================================\n",n);
    }
    else
        printf("\n the number has not been found!");
    return head;/*返回改变后的头即 head。。*/
}


struct Student *insert(struct Student *head)/*动态链表的节点的增加。。*/
{
    int a,i;
    printf("\n=============================================\n");
    printf("       |请输入你想要插入多少名学生的信息|       ");
    printf("\n=============================================\n");
    printf("开始输入:");
    scanf("%d",&a);
    printf("- - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("开始插入学生信息:\n");
    printf("- - - - - - - - - - - - - - - - - - - - - - -\n");
    struct Student *p1,*p2,*p0;
    for (i=1;i<=a;i++)
    {
        p0=(struct Student *)malloc(LEN);
        printf("                 ");
        scanf("%d%s%f",&p0->num,p0->name,&p0->score);
        p1=p2=head;
        if(head==NULL)/*原表是空表*/
        {
            head=p0;
            p0->next=NULL;
        }
        else
        {
            while((p0->num>p1->num)&&(p1->next!=NULL))/*这个函数是在找所要的节点位置。。*/
            {
                p2=p1;
                p1=p1->next;
            }
            if(p0->num<=p1->num)
            {
                if(head==p1)
                    head=p0;/*插到第一个之前,找到头就可以全部输出,另一种方法如下。。*/
                /*
                 第二种方法:
                 p0->next=head;
                 head=p0;
                 */
                else
                    p2->next=p0;/*插到原队中间,p2为暂时保存的节点防止p1的改变而找不到原来的节点值。。*/
                p0->next=p1;
            }
            else
            {
                p1->next=p0;
                p0->next=NULL;/*插到原队最后。。*/
            }
        }
    }
    printf("- - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("插入之后的学生信息:\n");
    printf("=============================================\n");
    return head;
}


struct Student *Sort(struct Student *head)/*按照动态链表中输入的成绩进行排序。。*/
{
    printf("=============================================\n");
    printf("                  |开始排序|                  \n");
    printf("=============================================\n");
    printf("从大到小:\n");
    printf("- - - - - - - - - - - - - - - - - - - - - - -\n");
    struct Student *p,*q;
    p=head;
    char str[20];
    float number1;
    int number;
    while(p!=NULL)
    {
        q=p->next;
        while(q!=NULL)
        {
            if(p->score<q->score)
            {
                number1=p->score;
                p->score=q->score;
                q->score=number1;
                number=p->num;
                p->num=q->num;
                q->num=number;
                strcpy(str,p->name);
                strcpy(p->name,q->name);
                strcpy(q->name,str);
            }
            q=q->next;
        }
        p=p->next;
    }
    return head;
}


struct Student *find(struct Student *head)/*动态链表的节点的查找。。*/
{
    int a;
    printf("=============================================\n");
    printf("          |请输入你想要查找的学生的学号|          \n");
    printf("=============================================\n");
    printf("开始输入:");
    scanf("%d",&a);
    printf("=============================================\n");
    printf("                  |查找的结果|                 \n");
    printf("=============================================\n");
    struct Student *p1,*p2;
    p1=p2=head;
    while (p1->num!=a)
    {
        p2=p1;
        p1=p1->next;
    }
    p1->next=NULL;
    head=p1;
    return head;
}


int main()/*此函数有种收所有头的作用,让它们所有的都联系起来。。*/
{
    printf("=============================================\n");
    printf("    |学号|          |姓名|           |成绩|\n");
    printf("=============================================\n            开始输入,以0 0 0结束。\n");
struct Student *head;
head=creat();/*切记它是void类型的所以用()仔细观察它的联系和聚拢作用。。*/
//print(head);
    head=del(head);/*它是有返回值的,所以用(head)。。*/
    print(head);
    head=insert(head);/*增加节点。。*/
    print(head);
    head=Sort(head);/*按照动态链表中输入的成绩进行排序。。*/
    print(head);
    head=find(head);/*动态链表的节点的查找。。*/
    print(head);
    printf("- - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("                  *【运行结束】*               \n");
    return 0;
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值