当前位置:   article > 正文

链表-----节点最大值,累加和,统计节点个数,逆序数据建立链表以及回文字符串_6-6 链表-节点最大值

6-6 链表-节点最大值

链表-节点最大值

本题要求实现一个函数,遍历一个不带头节点的链表,求链表节点数据的最大值

节点类型定义

struct node { int ch ; struct node *next ;}

函数接口定义:

 

在这里描述函数接口。例如: int max_node( struct node *p)

p是链表头指针,返回链表上最大的ch属性值。

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {int ch;
  5. struct node * next;};
  6. struct node *setlink(int N);//建立链表函数,已经定义
  7. int max_node(struct node * head);//需要定义这个函数
  8. int main()
  9. {
  10. int N;
  11. struct node *head;
  12. scanf("%d",&N);
  13. head=setlink(N);
  14. printf("%d", max_node(head));
  15. return 0;
  16. }

输入样例:

在这里给出一组输入。例如:

  1. 6
  2. 7 8 9 1 2 3

输出样例:

在这里给出相应的输出。例如:

9

其实这个题目相对简单,我们只需要注意链表如何后移即可 

  1. int max_node( struct node *p)
  2. {
  3. int max=0;
  4. max=p->ch;
  5. struct node * s;//定义一个临时变量
  6. s=p;
  7. while(1)
  8. {
  9. if(s->next==NULL)
  10. break;
  11. if(s->ch>max)
  12. max=s->ch;
  13. s=s->next; //可以实现进入下一个节点
  14. }
  15. return max;
  16. }

 

链表—累加和

本题要求实现一个函数,遍历一个不带头结点的链表,求链表节点数据的累加和

节点类型定义:

struct node { int ch ; struct node *next ;}

函数接口定义:

int sum_node( struct node *p)

p是链表头指针,返回链表上所有节点ch属性值的累加和。

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {int ch;
  5. struct node * next;};
  6. struct node *setlink(int N);//建链表函数已经定义
  7. int sum_node(struct node * head);//需要定义的函数
  8. int main()
  9. {
  10. int N;
  11. struct node *head;
  12. scanf("%d",&N);
  13. head=setlink(N);
  14. printf("%d", sum_node(head));
  15. return 0;
  16. }
  17. /* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

  1. 6
  2. 3 1 2 7 4 5

输出样例:

在这里给出相应的输出。例如:

22

这个题目也较为简单只需要注意一下最后一步 

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int sum_node( struct node *p)
  4. {
  5. int sum=0;
  6. struct node * s;
  7. s=p;
  8. while(1)
  9. {
  10. if(s->next==NULL)
  11. break;
  12. sum+=s->ch;
  13. s=s->next;
  14. }
  15. sum+=s->ch; //这里要注意尾结点的数据域在上面循环中未能进行处理
  16. return sum;
  17. }

 

链表——统计节点个数

定义函数,遍历一个不带头结点的链表,统计链表上的节点个数

函数接口定义:

int countnode(struct node * head)

head是链表头指针,返回值是节点个数

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {int ch;
  5. struct node * next;};
  6. struct node *setlink(int N);//建立链表函数已建立
  7. int countnode(struct node * head);//需要顶一次函数
  8. int main()
  9. {
  10. int i,N;
  11. struct node *head;
  12. scanf("%d",&N);
  13. head=setlink(N);
  14. printf("%d", countnode(head));
  15. return 0;
  16. }
  17. /* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

  1. 6
  2. 1 2 3 4 5 6

输出样例:

在这里给出相应的输出。例如:

6

看完前两个题目,这个题也很容易了! 

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int countnode(struct node * head)
  4. {
  5. int a=0;
  6. struct node * s;
  7. s=head;
  8. while(1)
  9. {
  10. if(s->next==NULL)
  11. break;
  12. else
  13. {
  14. a++;
  15. s=s->next;
  16. }
  17. }
  18. return a+1; //跟第二个题一样需要注意尾结点
  19. }

 

逆序数据建立链表

本题要求实现一个函数,按输入数据的逆序建立一个链表。

函数接口定义

struct ListNode *createlist();

函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:

struct ListNode { int data; struct ListNode *next; };

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct ListNode {
  4. int data;
  5. struct ListNode *next;
  6. };
  7. struct ListNode *createlist();
  8. int main()
  9. {
  10. struct ListNode *p, *head = NULL;
  11. head = createlist();
  12. for ( p = head; p != NULL; p = p->next )
  13. printf("%d ", p->data);
  14. printf("\n");
  15. return 0;
  16. }
  17. /* 你的代码将被嵌在这里 */

输入样例:

1 2 3 4 5 6 7 -1

输出样例:

7 6 5 4 3 2 1 

看了前三个是不是感觉还可以呢,现在来上点难度 

分析一下题目,逆序输出链表,那不就是要用头插法建立链表么头插法就是一直往前插入数据,而尾插法是一直往后插入数据

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. struct ListNode *createlist()
  5. {
  6. struct ListNode * s,* head;
  7. int data;
  8. head=(struct ListNode *)malloc(sizeof(struct ListNode));//这里我们要建立一个头结点
  9. head->next=NULL; //头结点的指针域为空
  10. while(1)
  11. {
  12. scanf("%d",&data);
  13. if(data==-1)//链表结束标志
  14. break;
  15. s=(struct ListNode *)malloc(sizeof(struct ListNode));
  16. s->data=data; //s中存储新的数据
  17. s->next=head->next; //s的指针域指向头结点
  18. head->next=s; //头结点的指针域指向存放新数据的s
  19. } //循环结束后head相当于头指针,其指针域指向我们所要的第一个节点
  20. return head->next;
  21. }

 

判断回文字符串

本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。

函数接口定义:

int Judge_char( char *s );

函数Judge_char判断输入字符串char *s是否为回文,若是则返回1,否则返回0。

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXN 20
  4. int Judge_char( char *s );
  5. int main()
  6. {
  7. char s[MAXN];
  8. scanf("%s", s);
  9. if ( Judge_char(s)==1 )
  10. printf("Yes\n");
  11. else
  12. printf("No\n");
  13. printf("%s\n", s);
  14. return 0;
  15. }
  16. /* 你的代码将被嵌在这里 */

输入样例:

thisistrueurtsisiht

输出样例:

  1. Yes
  2. thisistrueurtsisiht

输入样例:

thisisnottrue

输出样例:

  1. No
  2. thisisnottrue

最后再附上最近碰见的回文吧! 

  1. #include<stdio.h>
  2. #include<string.h>
  3. int Judge_char( char *s )
  4. {
  5. int i=0,j;
  6. for(i=0,j=strlen(s)-1-i;i<j;i++,j--)
  7. {
  8. if(s[i]!=s[j])
  9. break;
  10. }
  11. if(j<=i) //是回文的话一定存在i>=j,否则不是回文
  12. return 1;
  13. if(i<j)
  14. return 0;
  15. }

 欢迎大家积极留言呀!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/800305
推荐阅读
相关标签
  

闽ICP备14008679号