• 链表 C语言


    题目 1052: [编程入门]链表合并]

    1. //学习链表记录
    2. #include<stdio.h>
    3. #include<stdlib.h>
    4. //结构体,然后区分结构体是内部的最后一行
    5. //struct Student *next;
    6. typedef struct Student{
    7. int num;
    8. int grade;
    9. struct Student *next;
    10. }Node,*node;
    11. //https://blog.csdn.net/endeavor_g/article/details/80552680
    12. //链表理论学习
    13. node creat(int n);//创建链表
    14. void unite(node head1,node head2);// 结合链表
    15. //这个有点像union联合函数
    16. void order(node head,int sum);//排序链表
    17. void output(node head);//打印链表
    18. //主函数
    19. int main(){
    20. int a,b;
    21. scanf("%d%d",&a,&b);//输入的两个链表元素数量
    22. node head1=creat(a);//创造两个链表 ,head12为头节点
    23. node head2=creat(b);
    24. unite(head1,head2);//结合两个链表
    25. order(head1,a+b);//排序两个链表
    26. return 0;
    27. }
    28. //创建链表
    29. /*
    30. LinkList *creat(int n){
    31. LinkList *head, *node, *end;//定义头节点,普通节点,尾部节点;
    32. head = (LinkList*)malloc(sizeof(LinkList));//分配地址
    33. end = head; //若是空链表则头尾节点一样
    34. for (int i = 0; i < n; i++) {
    35. node = (LinkList*)malloc(sizeof(LinkList));
    36. scanf("%d", &node->score);
    37. end->next = node;
    38. end = node;
    39. }
    40. end->next = NULL;//结束创建
    41. return head;
    42. }
    43. */
    44. node creat(int n){
    45. node head=(node)malloc(sizeof(Node));//head为头结点 ,尾插创造
    46. head->next=NULL;
    47. node p=head;//p为头指针
    48. for(int i=0;i<n;i++){
    49. node q=(node)malloc(sizeof(Node));
    50. q->next=NULL;//q是结尾节点
    51. p->next=q;
    52. p=q;
    53. scanf("%d%d",&(*q).num,&(*q).grade);
    54. }
    55. return head;
    56. }
    57. //结合两个链表
    58. void unite(node head1,node head2){
    59. node p;
    60. p=head1;
    61. while(p->next!=NULL)
    62. p=p->next;
    63. p->next=head2->next;
    64. head2->next=NULL;
    65. }
    66. //排序两个链表
    67. void order(node head,int sum){
    68. node p=head->next;//p指向首结点
    69. int t1,t2;//t1学号中间变量,t2成绩中间变量
    70. int cnt=0;//cnt为链表长度,这里选用冒泡排序
    71. while(cnt<sum){
    72. node q=p;
    73. while(q->next!=NULL){
    74. if((q->num)>(q->next->num)){
    75. t1=q->num;
    76. q->num=q->next->num;
    77. q->next->num=t1;
    78. t2=q->grade;
    79. q->grade=q->next->grade;
    80. q->next->grade=t2;
    81. }
    82. q=q->next;
    83. }
    84. cnt++;
    85. }
    86. output(head);
    87. }
    88. //输出两个链表
    89. void output(node head){
    90. node p=head->next;
    91. node q;
    92. while(p!=NULL){
    93. printf("%d %d\n",p->num,p->grade);
    94. q=p;
    95. p=p->next;
    96. free(q);
    97. }
    98. }

    c语言链表详解(超详细)_Mr.Gzj的博客-CSDN博客_链表c语言

    题目 1052: [编程入门]链表合并_海琴烟Sunshine的博客-CSDN博客

    这个题目在我做之前,我是不会使用链表去运行的,因为我不太好使用链表,同时在我看了链表的相关代码后,太过于复杂,所以我直接使用结构去进行运行,代码如下:

    1. #include<stdio.h>
    2. typedef struct student{
    3. int hao;
    4. int mark;
    5. }stu;
    6. stu a[100001];
    7. int main()
    8. {
    9. int i,j,k,n,m;
    10. int te,temp;
    11. scanf("%d %d\n",&n,&m);
    12. int s=n+m;
    13. for(i=0;i<s;i++)
    14. {
    15. scanf("%d %d\n",&a[i].hao,&a[i].mark);
    16. }
    17. //采用冒泡排序法
    18. for(j=1;j<i;j++)
    19. {
    20. for(k=0;k<i-j;k++)//从第一个数开始,一直到倒数第二个数,比较大小
    21. //然后是第一个数开始,一直到倒数第三个数,比较大小,依次
    22. {
    23. if(a[k+1].hao<a[k].hao)
    24. {
    25. temp=a[k].hao;
    26. a[k].hao=a[k+1].hao;
    27. a[k+1].hao=temp;
    28. te=a[k].mark;
    29. a[k].mark=a[k+1].mark;
    30. a[k+1].mark=te;
    31. }
    32. }
    33. }
    34. for(i=0;i<s;i++)
    35. {
    36. printf("%d %d\n",a[i].hao,a[i].mark);
    37. }
    38. return 0;
    39. }

    题目 1585: 

    蓝桥杯算法训练VIP-链表数据求和操作

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. int snum=0,sgra=0;
    4. typedef struct Student{
    5. int num;//假设num=实数
    6. int grade;//假设grade=虚数的前面数字
    7. struct Student *next;
    8. }Node,*node;
    9. node creat(int n);//创建链表
    10. //void unite(node head1,node head2);// 结合链表
    11. void order(node head,int sum);//排序链表
    12. void output(node head);//打印链表
    13. int main(){
    14. int a,b;
    15. node head=creat(10);//创造两个链表 ,head12为头节点
    16. //node head2=creat(b);
    17. //unite(head1,head2);//结合两个链表
    18. order(head,a+b);//排序两个链表
    19. //output(head1);
    20. return 0;
    21. }
    22. node creat(int n){
    23. node head=(node)malloc(sizeof(Node));//head为头结点 ,尾插创造
    24. head->next=NULL;
    25. node p=head;//p为头指针
    26. for(int i=0;i<n;i++){
    27. node q=(node)malloc(sizeof(Node));
    28. q->next=NULL;
    29. p->next=q;
    30. p=q;
    31. scanf("%d %d",&(*q).num,&(*q).grade);
    32. }
    33. return head;
    34. }
    35. /*
    36. void unite(node head1,node head2){
    37. node p;
    38. p=head1;
    39. while(p->next!=NULL)
    40. p=p->next;
    41. p->next=head2->next;
    42. head2->next=NULL;
    43. }*/
    44. void order(node head,int sum){
    45. node p=head->next;//p指向首结点
    46. int t1,t2;//t1学号中间变量,t2成绩中间变量
    47. int cnt=0;//cnt为链表长度,这里选用冒泡排序
    48. /* while(cnt<sum){
    49. node q=p;
    50. while(q->next!=NULL){
    51. if((q->num)>(q->next->num)){
    52. t1=q->num;
    53. q->num=q->next->num;
    54. q->next->num=t1;
    55. t2=q->grade;
    56. q->grade=q->next->grade;
    57. q->next->grade=t2;
    58. }
    59. q=q->next;
    60. }
    61. cnt++;
    62. } */
    63. output(head);
    64. }
    65. void output(node head){
    66. node p=head->next;
    67. node q;
    68. while(p!=NULL){
    69. snum=snum+p->num;
    70. sgra=sgra+p->grade;
    71. //printf("%d %d\n",p->num,p->grade);
    72. q=p;
    73. p=p->next;
    74. free(q);
    75. }
    76. printf("%d+%di",snum,sgra);
    77. }

    下面这道求和的题目,直接在上面的程序上进行修改就可以,区别在于不需要排序和整合两个链表,只需要进行链表求和就可以,此处要注意修改的是输入和输出,然后把不需要的注释掉就可以了

    题目 1676: 

    数据结构-链表的基本操作

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #include<string.h>
    4. typedef struct LN{
    5. int x;
    6. struct LN *next;
    7. }lnode,*Lnode;
    8. Lnode creat(int);//创建链表
    9. void show(Lnode);//展示链表中的元素
    10. void get(Lnode,int);//得到链表中的元素
    11. void delete_(Lnode,int);//删除链表中的元素
    12. void insert(Lnode,int,int);//插入
    13. int main(){
    14. int n;
    15. scanf("%d",&n);//列表内数量
    16. getchar();//输入字符
    17. Lnode head=creat(n);//创建链表
    18. int m;
    19. scanf("%d",&m);//m是第二行下面输入的行数
    20. getchar();
    21. char str[20];
    22. for(int i=0;i<m;i++){
    23. scanf("%s",str);
    24. getchar();
    25. if(strcmp(str,"show")==0){//strcmp对比str和show,如果相等则为0
    26. show(head);
    27. }
    28. if(strcmp(str,"get")==0){
    29. int a;
    30. scanf("%d",&a);
    31. getchar();
    32. get(head,a);
    33. }
    34. if(strcmp(str,"delete")==0){
    35. int b;
    36. scanf("%d",&b);
    37. getchar();
    38. delete_(head,b);
    39. }
    40. if(strcmp(str,"insert")==0){
    41. int c,d;
    42. scanf("%d%d",&c,&d);
    43. getchar();
    44. insert(head,c,d);
    45. }
    46. }
    47. return 0;
    48. }
    49. //创建链表
    50. Lnode creat(int n){
    51. Lnode head=(Lnode)malloc(sizeof(lnode));
    52. head->next=NULL;
    53. for(int i=0;i<n;i++){//采用头插法
    54. Lnode p=(Lnode)malloc(sizeof(lnode));
    55. scanf("%d",&(*p).x);
    56. getchar();
    57. p->next=head->next;
    58. head->next=p;
    59. }
    60. return head;
    61. }
    62. //show
    63. void show(Lnode head){
    64. Lnode p=head->next;
    65. if(p==NULL){
    66. printf("Link list is empty\n");
    67. }
    68. else{
    69. while(p!=NULL){
    70. printf("%d ",p->x);
    71. p=p->next;
    72. }
    73. printf("\n");
    74. }
    75. }
    76. //get
    77. void get(Lnode head,int a){
    78. Lnode p=head->next;
    79. int j=1;
    80. while(p&&j<a){
    81. p=p->next;
    82. j++;
    83. }
    84. if(!p||j>a){
    85. printf("get fail\n");
    86. }
    87. else{
    88. printf("%d\n",p->x);
    89. }
    90. }
    91. //delete_
    92. void delete_(Lnode head,int b){
    93. Lnode p=head;
    94. int j=0;
    95. while(p->next&&j<b-1){
    96. p=p->next;
    97. j++;
    98. }
    99. if(!(p->next)||(j>b-1)){
    100. printf("delete fail\n");
    101. }
    102. else{
    103. Lnode q=p->next;
    104. p->next=q->next;
    105. printf("delete OK\n");
    106. free(q);
    107. }
    108. }
    109. //insert
    110. void insert(Lnode head,int c,int d){
    111. Lnode p=head;
    112. int j=0;
    113. while(p&&j<c-1){
    114. p=p->next;
    115. j++;
    116. }
    117. if(!p||(j>c-1)){
    118. printf("insert fail\n");
    119. }
    120. else{
    121. Lnode q=(Lnode)malloc(sizeof(lnode));
    122. q->next=p->next;
    123. p->next=q;
    124. q->x=d;
    125. printf("insert OK\n");
    126. }
    127. }

    题目 1676: 数据结构-链表的基本操作_海琴烟Sunshine的博客-CSDN博客

    最后一个基本操作,里面get delete  还有插入三个算法,主要看题目给的算法,里面的一些细节,一定要注意,比如说+1或者硕士p开头是什么

  • 相关阅读:
    Linux目录操作
    【全志R128填坑分享】适配LVGL界面图片和文字显示很虚,色阶明显的解决方法
    生产企业如何选择适合自己的工业网关?
    “我有一个大胆的想法”?Meta AI 新技术让你的思维图像一览无余!
    Java8中的Stream流
    光照静态烘焙
    ShardingSphere学习(超详细)
    在Vue3 + Vite项目中使用less
    vue 父子孙页面传值的多种方法
    无线充电器出口欧盟CE认证RED指令测试分析
  • 原文地址:https://blog.csdn.net/Lukegood/article/details/127622605