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

链表-节点最大值

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

节点类型定义:

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;
}

 欢迎大家积极留言呀!

  • 19
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值