OJ 链表结点的插入

1098 [填空]链表结点的插入
时间限制:1000MS 代码长度限制:10KB
提交次数:2783 通过次数:1484

题型: 填空题 语言: GCC
Description
完成插入链表结点的函数(按学号顺序),并调试通过、提交。

#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)

struct student
{
    long num;
    int score;
    struct student *next;
};

struct student *create(int n)
{
    struct student *head=NULL,*p1=NULL,*p2=NULL;
    int i;
    for(i=1; i<=n; i++)
    {
        p1=(struct student *)malloc(LEN);
        scanf("%ld",&p1->num);
        scanf("%d",&p1->score);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
    }
    return(head);
}

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

struct student *insert(struct student *head, struct student *stud)
{
    struct student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)//如果head是空表 则令head指向p0 ,p0的next指向null
    {
        head=p0;
        p0->next=NULL;
    }
    else     //否则当需要插入的号码大于原有表中的号码且原有表p1的next不为空时,让p2指向p1,p1指向下一个
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num)//说明p0在p1前面,如果前面没有其他元素了,head->p0->p1;如果前面还有其他元素head->...->p2->p0->p1
        {
            if(head==p1)head=p0;
            else p2->next=p0;
            p0->next=p1;
        }
        else//如果p0在p1后面 p1->p0->null
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
    return(head);
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);
    head=create(n);
    print(head);
    stu=(struct student *)malloc(LEN);
    scanf("%ld",&stu->num);
    scanf("%d",&stu->score);
    stu->next = NULL;
    head=insert(head,stu);
    print(head);
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值