当前位置: 首页 > article >正文

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

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

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

 


http://www.kler.cn/news/155967.html

相关文章:

  • word表格图片批处理参考程序
  • Linux-usb触摸板去除鼠标箭头
  • Ubuntu20.24 安装ecCodes,包括 tar.gz 和 python(笔记)
  • [网络安全]dos命令
  • Sakila数据库和World数据库
  • Vue+ElementUI+C#前后端分离:监控长耗时任务的实践
  • [足式机器人]Part4 南科大高等机器人控制课 Ch00 课程简介
  • C语言,求取数组的序亏:已知一个整数数组,求出个数组中每个元素在整个 数组的排序。
  • 鸿蒙(HarmonyOS)应用开发——构建页面(题目答案)
  • 93. 复原 IP 地址
  • 华为手环配置技巧
  • Java 中 IO 流分为几种?
  • 【算法思考记录】力扣1423. 可获得的最大点数[Python3, 滑动窗口]
  • golang 实现单向链表(lru)、双向链表、双向循环链表
  • Error:cannot launch node of type [map_server/map_server]
  • A++ 敏捷开发-1 如何改善
  • 常微分方程组的数值解法(C++)
  • WPS开发文档
  • Android:BackStackRecord
  • KALI LINUX安全审核
  • 实时设计#N3期训练营DONE,ComfyUI中文社区@上海
  • 再谈项目管理中的效率问题
  • “此应用专为旧版android打造,因此可能无法运行”,问题解决方案
  • 并发编程1:线程的基本概念
  • C# 使用HtmlAgilityPack解析提取HTML内容
  • 爬虫伦理与法律:确保数据采集合法性与伦理性
  • Unity工具脚本-检测资源文件夹是否有预制件是指定层级
  • 深入了解Java8新特性-日期时间API之ZonedDateTime类
  • 【Arduino库之:FastLED库】
  • SCAU:数字字符序列2