P305_1111链表的综合操作,建表、…

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#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);
    head = NULL;
    while(n<3)
    {
        n++;
        scanf("%ld,%f",&p1->num,&p1->score);
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        p1 =(struct student *)malloc(LEN);

    }
    p2->next = NULL;
    return head;
}

void print(struct student *head)
{
    struct student *p;
    printf("\nNow,these %d recoeds are:\n",n);
    p = head;
    while(p!=NULL)
    {
        printf("%ld,%f\n",p->num,p->score);
        p = p->next;
    }
}

struct student *del(struct student *head,long num)
{
    if(head==NULL) {printf("\nlist null! \n");return head;}
    struct student *p1,*p2;
    p2 = p1 = head;
    while(p1!=NULL&&num!=p1->num)
    {
        p2 = p1;
        p1 = p1->next;
    }//while
    if(p1==head)//删除首元素
    {
        head = p1->next;
        free(p1);
    }
    else if(p1==NULL)//找不到该元素
    {
        printf("Can't find the student whose number is %ld\n",num);
    }
    else//普通删除
    {
        p2->next = p1->next; //如果删除的是尾结点,则p1->next==NULL,所以跟普通删除处理过程是一样的
        free(p1);
    }
    return head;
}//del

struct student *insert(struct student *head,struct student *stud)
{
    if(head==NULL) {printf("\nlist null! \n");return head;}
    struct student *p0,*p1,*p2;
    p2 = p1 = head;
    p0 = stud;
    while(p1!=NULL&&p1->numnum)
    {
        p2 = p1;
        p1 = p1->next;
    }

    if(p1==NULL)//插入到链表尾端
    {
        p2->next = p0;
        p0->next = NULL;
    }
    else if(p1==head)//p1->num>=p0->num,将p0插入首元素之前
    {
        p0->next = head;
        head = p0;
    }
    else
    {
        p2->next = p0;
        p0->next = p1;

    }//if_else_if
    n++;
    return head;
}

int main()
{
    struct student *head,stu;
    long del_num;
    printf("input records:\n");
    head = creat();  
    print(head);
    printf("\ninput the deleted number:");
    scanf("%ld",&del_num);
    head = del(head,del_num);
    print(head);
    printf("\ninput the inserted:");
    scanf("%ld,%f",&stu.num,&stu.score);
    head = insert(head,&stu);
    print(head);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值