求解!大一题:链表创建与插入结点(填空)




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

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;
}

1空填 head = stud;
2空填 p1 = p1->next;
3空填 head
4空填 create(n)

你题目的解答代码如下:

#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)
      {head = stud;}
    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;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img