2430: C语言习题 链表建立,插入,删除,输出

时间限制: 1 Sec  内存限制: 128 MB

提交: 576  解决: 280

题目描述

编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)

编写一个函数printlink,用来输出一个链表。

编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。

编写一个函数insertlink,用来向动态链表插入一个结点。

编写一个函数freelink,用来释放一个动态链表。

输入

输入多个学生的学号和成绩,建立动态链表,以0 0 结束

输入学号,删除链表中的对应结点

插入两个链表结点

输出

输出的链表

样例输入

  1. 1001 100
  2. 1002 95
  3. 1005 90
  4. 1008 76
  5. 0 0
  6. 1005
  7. 1006 98
  8. 1009 99

样例输出

  1. 1001 100.00
  2. 1002 95.00
  3. 1006 98.00
  4. 1008 76.00
  5. 1009 99.00

提示

主函数已给定如下,提交时不需要包含下述主函数







/* C代码 */



int main()



{



    struct student *creatlink(void);



    struct student *dellink(struct student *,long);



    struct student *insertlink(struct student *,struct student *);



    void printlink(struct student *);



    void freelink(struct student *);



    struct student *head,stu;



    long del_num;



    head=creatlink();



    scanf("%ld",&del_num);



    head=dellink(head,del_num);



    scanf("%ld%f",&stu.num,&stu.score);



    head=insertlink(head,&stu);



    scanf("%ld%f",&stu.num,&stu.score);



    head=insertlink(head,&stu);



    printlink(head);



    freelink(head);



    return 0;



}







/* C++代码 */







int main()



{



    student *creatlink(void);



    student *dellink(student *,long);



    student *insertlink(student *,student *);



    void printlink(student *);



    void freelink(student *);



    student *head,stu;



    long del_num;



    head=creatlink();



    cin>>del_num;



    head=dellink(head,del_num);



    cin>>stu.num>>stu.score;



    head=insertlink(head,&stu);



    cin>>stu.num>>stu.score;



    head=insertlink(head,&stu);



    cout<<setiosflags(ios::fixed);



    cout<<setprecision(2);



    printlink(head);



    freelink(head);



    return 0;



}

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<iomanip>
  6. using namespace std;
  7. struct student
  8. {
  9. long int num;
  10. float score;
  11. struct student *next;
  12. };
  13. struct student *creatlink()
  14. {
  15. struct student *p1,*p2,*head=NULL;
  16. p1=p2=(struct student*)malloc(sizeof(struct student));
  17. while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num))
  18. {
  19. if(head==NULL)head=p1;
  20. else p2->next=p1;
  21. p2=p1;
  22. p1=p1->next=(struct student*)malloc(sizeof(struct student));
  23. }
  24. p2->next=NULL;
  25. return head;
  26. };
  27. struct student *dellink(struct student *a,long b)
  28. {
  29. struct student *head=a,*p=a,*p2=a;
  30. while(p!=NULL)
  31. {
  32. if(p->num==b)
  33. p2->next=p->next;
  34. p2=p;
  35. p=p->next;
  36. }
  37. return head;
  38. };
  39. struct student *insertlink(struct student *a,struct student *b)
  40. {
  41. struct student *head=a,*p=a,*p2=a,*k;
  42. k=(struct student*)malloc(sizeof(struct student));
  43. k->num=b->num,k->score=b->score;
  44. int n=0;
  45. while(p!=NULL)
  46. {
  47. if(p->num>b->num)
  48. {
  49. p2->next=k;
  50. k->next=p;
  51. n=1;
  52. }
  53. p2=p;
  54. p=p->next;
  55. }
  56. if(n==0)p2->next=k,k->next=NULL;
  57. return head;
  58. };
  59. void printlink(struct student *a)
  60. {
  61. struct student *p=a;
  62. while(p!=NULL)
  63. {
  64. printf("%ld %.2f\n",p->num,p->score);
  65. p=p->next;
  66. }
  67. }
  68. void freelink(struct student *a)
  69. {
  70. while(a!=NULL)
  71. {
  72. delete(a);
  73. a=a->next;
  74. }
  75. }
  76. int main()
  77. {
  78. student *creatlink(void);
  79. student *dellink(student *,long);
  80. student *insertlink(student *,student *);
  81. void printlink(student *);
  82. void freelink(student *);
  83. student *head,stu;
  84. long del_num;
  85. head=creatlink();
  86. cin>>del_num;
  87. head=dellink(head,del_num);
  88. cin>>stu.num>>stu.score;
  89. head=insertlink(head,&stu);
  90. cin>>stu.num>>stu.score;
  91. head=insertlink(head,&stu);
  92. cout<<setiosflags(ios::fixed);
  93. cout<<setprecision(2);
  94. printlink(head);
  95. freelink(head);
  96. return 0;
  97. }

YTU 2430: C语言习题 链表建立,插入,删除,输出的更多相关文章

  1. C语言习题 链表建立,插入,删除,输出

    Problem B: C语言习题 链表建立,插入,删除,输出 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 222  Solved: 92 [Subm ...

  2. YTU 2429: C语言习题 学生成绩输入和输出

    2429: C语言习题 学生成绩输入和输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 1897  解决: 812 题目描述 编写一个函数print,打印一个学生的成绩数组,该数组中有 ...

  3. Problem A: C语言习题 链表建立,插入,删除,输出

    #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct student { l ...

  4. Problem X: C语言习题 学生成绩输入和输出

    Problem X: C语言习题 学生成绩输入和输出 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 4722  Solved: 2284[Submit] ...

  5. 20140502 static_cast和dynamic_cast的类型检查 双链表建立,删除,打印

    1.static_cast和dynamic_cast的类型检查 static_cast的类型检查:只检查无关类之间的转换 CBaseY* pY1 = static_cast<CBaseY*> ...

  6. YTU 2414: C语言习题 字符串排序

    2414: C语言习题 字符串排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 656  解决: 305 题目描述 输入n个字符串,将它们按字母由小到大的顺序排列并输出.编写三个函数实 ...

  7. YTU 2974: C语言习题5.26--文件操作3

    2974: C语言习题5.26--文件操作3 时间限制: 1 Sec  内存限制: 128 MB 提交: 213  解决: 92 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...

  8. YTU 2973: C语言习题5.25--文件操作2

    2973: C语言习题5.25--文件操作2 时间限制: 1 Sec  内存限制: 128 MB 提交: 242  解决: 105 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编 ...

  9. YTU 2972: C语言习题5.24--文件操作1

    2972: C语言习题5.24--文件操作1 时间限制: 1 Sec  内存限制: 128 MB 提交: 248  解决: 94 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...

随机推荐

  1. 03003_Http响应

    1.Http协议 (1)状态码: (2)常用的状态码如下: 200 :请求成功: 302 :请求重定向: 304 :请求资源没有改变,访问本地缓存: 404 :请求资源不存在.通常是用户路径编写错误, ...

  2. C#与Ranorex自动化公用方法

    原创 - C#与Ranorex自动化公用方法 利用c#在Ranorex上写自动化已经有很长的一段时间了,总结发现常用的方法不外乎如下几种: 1.打开浏览器:或者app public static vo ...

  3. mongodb客户端连接mongodb server

    import pymongo import sys import os sys.path.append(os.path.split(os.path.realpath(__file__))[0]+&qu ...

  4. Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~

    A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. 生物遗传学 整理人PYJ (恋_紫花地丁)

    生物遗传学整理人PYJ (恋_紫花地丁) 高中生物唯一需要数学知识的就是遗传学的概率计算了.这里对简单的遗传学规律做一些总结. 目录: 1.      孟德尔第一定律(分离定律): 2.      孟 ...

  6. 【译】Nodejs最好的ORM

    TypeORM github: https://github.com/typeorm/typeorm这篇译文是从TypeORM github上的使用说明上翻译过来的,已经提交PR并merge到库中了. ...

  7. BZOJ 1798:

    6:       LAZY 线段树有乘法的更新    #include <cstdio> #include <cstring> #include <algorithm&g ...

  8. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  9. 初始VueJS视频

    本视频简单的介绍的使用. 初始VueJS视频

  10. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...