SCAU:链表创建与插入结点(填空)

本文介绍了如何在C语言中实现链表的创建、插入节点以及打印链表的操作,包括结构体定义、创建函数、插入函数和主函数的详细步骤。

链表创建与插入结点(填空)

Time Limit:1000MS  Memory Limit:65535K

题型: 填空题   语言: G++;GCC;VC

描述

代码实现先创新一个链表,然后显示该链表,之后插入一个结点,再显示插入结点的链表。
请填空,完成该代码


#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("%ld\t%d",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) 
      {_______________________;} 
    else 
    {
       while( (p0->num > p1->num) && (p1->next!=NULL) ) 
       {
         p2=p1;
         _______________________;
       } 
       if( p0->num <= p1->num ) 
       {
           if( head==p1 ) head=p0; 
           else p2->next=p0; 
           p0->next=p1; 
        } 
        else {  p1->next=p0;} 
    } 
    return(_______________________); 


int main() 

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

#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 = head;
    while (p != NULL)
    {
        printf("%ld\t%d", p->num, p->score);
        p = p->next;
        printf("\n");
    }
}

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


int 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);
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zero_019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值