赞
踩
本题要求实现一个函数,遍历一个不带头节点的链表,求链表节点数据的最大值
节点类型定义:
struct node { int ch ; struct node *next ;}
在这里描述函数接口。例如: int max_node( struct node *p)
p是链表头指针,返回链表上最大的ch属性值。
- #include <stdio.h>
- #include<stdlib.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);
- head=setlink(N);
- printf("%d", max_node(head));
- return 0;
- }

在这里给出一组输入。例如:
- 6
- 7 8 9 1 2 3
在这里给出相应的输出。例如:
9
其实这个题目相对简单,我们只需要注意链表如何后移即可
- int max_node( struct node *p)
- {
- int max=0;
- max=p->ch;
- struct node * s;//定义一个临时变量
- s=p;
- while(1)
- {
- if(s->next==NULL)
- break;
- if(s->ch>max)
- max=s->ch;
- s=s->next; //可以实现进入下一个节点
- }
- return max;
- }

本题要求实现一个函数,遍历一个不带头结点的链表,求链表节点数据的累加和
节点类型定义:
struct node { int ch ; struct node *next ;}
int sum_node( struct node *p)
p是链表头指针,返回链表上所有节点ch属性值的累加和。
- #include <stdio.h>
- #include<stdlib.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);
- head=setlink(N);
- printf("%d", sum_node(head));
- return 0;
- }
- /* 请在这里填写答案 */

在这里给出一组输入。例如:
- 6
- 3 1 2 7 4 5
在这里给出相应的输出。例如:
22
这个题目也较为简单只需要注意一下最后一步
- #include<stdio.h>
- #include<stdlib.h>
- int sum_node( struct node *p)
- {
- int sum=0;
- struct node * s;
- s=p;
- while(1)
- {
- if(s->next==NULL)
- break;
- sum+=s->ch;
- s=s->next;
- }
- sum+=s->ch; //这里要注意尾结点的数据域在上面循环中未能进行处理
- return sum;
- }

定义函数,遍历一个不带头结点的链表,统计链表上的节点个数
int countnode(struct node * head)
head是链表头指针,返回值是节点个数
- #include <stdio.h>
- #include<stdlib.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);
- head=setlink(N);
- printf("%d", countnode(head));
- return 0;
- }
-
- /* 请在这里填写答案 */

在这里给出一组输入。例如:
- 6
- 1 2 3 4 5 6
在这里给出相应的输出。例如:
6
看完前两个题目,这个题也很容易了!
- #include<stdio.h>
- #include<stdlib.h>
- int countnode(struct node * head)
- {
- int a=0;
- struct node * s;
- s=head;
- while(1)
- {
- if(s->next==NULL)
- break;
- else
- {
- a++;
- s=s->next;
- }
- }
- return a+1; //跟第二个题一样需要注意尾结点
- }

本题要求实现一个函数,按输入数据的逆序建立一个链表。
struct ListNode *createlist();
函数createlist
利用scanf
从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:
struct ListNode { int data; struct ListNode *next; };
- #include <stdio.h>
- #include <stdlib.h>
-
- struct 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
看了前三个是不是感觉还可以呢,现在来上点难度
分析一下题目,逆序输出链表,那不就是要用头插法建立链表么,头插法就是一直往前插入数据,而尾插法是一直往后插入数据。
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- struct ListNode *createlist()
- {
- struct ListNode * s,* head;
- int data;
- head=(struct ListNode *)malloc(sizeof(struct ListNode));//这里我们要建立一个头结点
- head->next=NULL; //头结点的指针域为空
- while(1)
- {
- scanf("%d",&data);
- if(data==-1)//链表结束标志
- break;
- s=(struct ListNode *)malloc(sizeof(struct ListNode));
- s->data=data; //s中存储新的数据
- s->next=head->next; //s的指针域指向头结点
- head->next=s; //头结点的指针域指向存放新数据的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");
- else
- printf("No\n");
- printf("%s\n", s);
-
- return 0;
- }
-
- /* 你的代码将被嵌在这里 */

thisistrueurtsisiht
- Yes
- thisistrueurtsisiht
thisisnottrue
- No
- thisisnottrue
最后再附上最近碰见的回文吧!
- #include<stdio.h>
- #include<string.h>
- int Judge_char( char *s )
- {
- int i=0,j;
- for(i=0,j=strlen(s)-1-i;i<j;i++,j--)
- {
- if(s[i]!=s[j])
- break;
- }
- if(j<=i) //是回文的话一定存在i>=j,否则不是回文
- return 1;
- if(i<j)
- return 0;
- }
欢迎大家积极留言呀!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。