1-线性表-3:central list(C语言实现)

题目:

If a list is central symmetry, it is called a central list. for example, 1->3->5->7->9->7->5->3->1 is a central list.
Give you a sequence of numbers, form a link list and then judge whether it is a central list.

输入格式:

A sequence of numbers seperated by comma

输出格式:

If it is a central list output Yes, otherwise No

输入样例:

1,3,5,7,9,7,5,3,1,

输出样例:

Yes

代码如下:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

struct Node
{
    int data;
    struct Node *pNext; // 指针 指向下一个链表元素
};
struct Node *CreatList(void) // 尾插法创建链表
{
    int cnt = 0; // 用来存放有效节点的个数
    int val;     // 用来临时存放用户输入的节点的值

    struct Node *pHead = (struct Node *)malloc(sizeof(struct Node));
    struct Node *pTail = pHead;
    pTail->pNext = NULL;
    while ((val = getchar()) != '\n')
    {
        if (val != ',')
        {
            struct Node *pNew = (struct Node *)malloc(sizeof(struct Node));
            pTail->pNext = pNew;
            pNew->data = val - 48;
            pNew->pNext = NULL;
            pTail = pNew;
            cnt++;
        }
    }
    pHead->data = cnt;
    return pHead;
}
int main()
{
    struct Node *pHead = CreatList();
    struct Node *p = pHead->pNext;
    int cnt = pHead->data;
    int arr1[cnt];
    int arr2[cnt];
    for (int i = 0; i < cnt; i++)
    {
        arr1[i] = p->data;
        arr2[cnt - i - 1] = p->data;
        p = p->pNext;
    }
    int num = 1;
    for (int i = 0; i < cnt; i++)
    {
        if (arr1[i] == arr2[i])
        {
            continue;
        }
        else
        {
            num = 0;
            printf("No");
            break;
        }
    }
    if (num == 1)
    {
        printf("Yes");
    }
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值