阜阳市城乡建设局网站,百度seo怎么做网站内容优化,吉林省建设银行网站,奉贤专业网站建设链表-节点最大值
本题要求实现一个函数#xff0c;遍历一个不带头节点的链表#xff0c;求链表节点数据的最大值
节点类型定义#xff1a;
struct node { int ch ; struct node *next ;}
函数接口定义#xff1a; 在这里描述函数接口。例如#xff1a; int max_node( …链表-节点最大值
本题要求实现一个函数遍历一个不带头节点的链表求链表节点数据的最大值
节点类型定义
struct node { int ch ; struct node *next ;}
函数接口定义 在这里描述函数接口。例如 int max_node( struct node *p)
p是链表头指针,返回链表上最大的ch属性值。
裁判测试程序样例
#include stdio.h
#includestdlib.h
struct node
{int ch;
struct node * next;}; struct node *setlink(int N);//建立链表函数已经定义int max_node(struct node * head);//需要定义这个函数int main()
{
int N;
struct node *head;
scanf(%d,N);
headsetlink(N);
printf(%d, max_node(head));
return 0;
}输入样例
在这里给出一组输入。例如
6
7 8 9 1 2 3输出样例
在这里给出相应的输出。例如
9
其实这个题目相对简单我们只需要注意链表如何后移即可
int max_node( struct node *p)
{int max0;maxp-ch;struct node * s;//定义一个临时变量sp;while(1){if(s-nextNULL)break;if(s-chmax)maxs-ch;ss-next; //可以实现进入下一个节点}return max;
} 链表—累加和
本题要求实现一个函数遍历一个不带头结点的链表求链表节点数据的累加和
节点类型定义
struct node { int ch ; struct node *next ;}
函数接口定义
int sum_node( struct node *p)
p是链表头指针,返回链表上所有节点ch属性值的累加和。
裁判测试程序样例
#include stdio.h
#includestdlib.h
struct node
{int ch;
struct node * next;};struct node *setlink(int N);//建链表函数已经定义
int sum_node(struct node * head);//需要定义的函数int main()
{
int N;
struct node *head;
scanf(%d,N);
headsetlink(N);
printf(%d, sum_node(head));
return 0;
}
/* 请在这里填写答案 */
输入样例
在这里给出一组输入。例如
6
3 1 2 7 4 5输出样例
在这里给出相应的输出。例如
22
这个题目也较为简单只需要注意一下最后一步
#includestdio.h
#includestdlib.h
int sum_node( struct node *p)
{int sum0;struct node * s;sp;while(1){if(s-nextNULL)break;sums-ch;ss-next;}sums-ch; //这里要注意尾结点的数据域在上面循环中未能进行处理return sum;
} 链表——统计节点个数
定义函数遍历一个不带头结点的链表统计链表上的节点个数
函数接口定义
int countnode(struct node * head)
head是链表头指针返回值是节点个数
裁判测试程序样例
#include stdio.h
#includestdlib.h
struct node
{int ch;
struct node * next;}; struct node *setlink(int N);//建立链表函数已建立int countnode(struct node * head);//需要顶一次函数int main()
{
int i,N;
struct node *head;
scanf(%d,N);
headsetlink(N);
printf(%d, countnode(head));
return 0;
}/* 请在这里填写答案 */
输入样例
在这里给出一组输入。例如
6
1 2 3 4 5 6输出样例
在这里给出相应的输出。例如
6
看完前两个题目这个题也很容易了
#includestdio.h
#includestdlib.h
int countnode(struct node * head)
{int a0;struct node * s;shead;while(1){if(s-nextNULL)break;else{a;ss-next;}}return a1; //跟第二个题一样需要注意尾结点
} 逆序数据建立链表
本题要求实现一个函数按输入数据的逆序建立一个链表。
函数接口定义
struct ListNode *createlist();
函数createlist利用scanf从输入中获取一系列正整数当读到−1时表示输入结束。按输入数据的逆序建立一个链表并返回链表头指针。链表节点结构定义如下
struct ListNode { int data; struct ListNode *next; };
裁判测试程序样例
#include stdio.h
#include stdlib.hstruct ListNode {int data;struct ListNode *next;
};struct ListNode *createlist();int main()
{struct ListNode *p, *head NULL;head createlist();for ( p head; p ! NULL; p p-next )printf(%d , p-data);printf(\n);return 0;
}/* 你的代码将被嵌在这里 */
输入样例
1 2 3 4 5 6 7 -1输出样例
7 6 5 4 3 2 1
看了前三个是不是感觉还可以呢现在来上点难度
分析一下题目逆序输出链表那不就是要用头插法建立链表么头插法就是一直往前插入数据而尾插法是一直往后插入数据。
#includestdio.h
#includestdlib.h
#includemalloc.h
struct ListNode *createlist()
{struct ListNode * s,* head;int data;head(struct ListNode *)malloc(sizeof(struct ListNode));//这里我们要建立一个头结点head-nextNULL; //头结点的指针域为空while(1){scanf(%d,data);if(data-1)//链表结束标志break;s(struct ListNode *)malloc(sizeof(struct ListNode));s-datadata; //s中存储新的数据s-nexthead-next; //s的指针域指向头结点head-nexts; //头结点的指针域指向存放新数据的s} //循环结束后head相当于头指针其指针域指向我们所要的第一个节点return head-next;
} 判断回文字符串
本题要求编写函数判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。
函数接口定义
int Judge_char( char *s );
函数Judge_char判断输入字符串char *s是否为回文若是则返回1否则返回0。
裁判测试程序样例
#include stdio.h
#include string.h
#define MAXN 20
int Judge_char( char *s );int main()
{char s[MAXN];scanf(%s, s);if ( Judge_char(s)1 )printf(Yes\n);elseprintf(No\n);printf(%s\n, s);return 0;
}/* 你的代码将被嵌在这里 */
输入样例
thisistrueurtsisiht输出样例
Yes
thisistrueurtsisiht输入样例
thisisnottrue输出样例
No
thisisnottrue
最后再附上最近碰见的回文吧
#includestdio.h
#includestring.h
int Judge_char( char *s )
{int i0,j;for(i0,jstrlen(s)-1-i;ij;i,j--){if(s[i]!s[j])break;}if(ji) //是回文的话一定存在ij,否则不是回文return 1;if(ij)return 0;
} 欢迎大家积极留言呀