题目:
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");
}
}