设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称
转载请注明出处:http://blog.csdn.net/u012860063
问题:设单链表中存放n个字符。试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define LEN sizeof(struct node)
#define MAX 147
struct node
{
char cc;
struct node *next;
};
int judge(struct node *head,int len)
{
struct node *top,*p1,*p2;
top = NULL;
p1 = head->next;
for(int i = 0 ; i < len/2 ; i++)
{
p2 = (struct node *)malloc(LEN);
p2->cc = p1->cc;
p2->next = top;
top = p2;
p1 = p1->next;
}
if(len%2 == 1)
p1 = p1->next;
p2 = top;
for(i = 0 ; i < len/2 ; i++)
{
if(p2->cc != p1->cc)
break;
top = p2->next;
p1 = p1->next;
p2 = top;
}
if(!top)
return 1;
else
return 0;
} int main()
{
int n=0;
char str[MAX];
struct node *head,*p;
head = p = (struct node *)malloc(LEN);
head->next = p->next = NULL;
printf("亲、请输入一个字符串:\n");
gets(str);
int len = strlen(str);
while(n < len && str[0] != '\n')
{
p = (struct node *)malloc(LEN);
p->cc = str[n];
p->next = head->next;
head->next = p;
n++;
}
int flag = judge(head,len);
if(flag)
printf("%s是一个回文!\n",str);
else
printf("%s不是一个回文!\n",str);
return 0;
}
设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称的更多相关文章
- 一个线性表中的元素为整数,设计一个算法,将正整数和负整数分开,使线性表的前一半为负整数,后一半为正整数。(C语言)
以下为完整可运行示例代码: #include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; str ...
- 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除。(C语言)
/* 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除 */ #include <stdio.h> #include <stdlib.h> typedef st ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 数据结构作业——P53算法设计题(6):设计一个算法,通过一趟遍历确定长度为n的单链表中值最大的结点
思路: 设单链表首个元素为最大值max 通过遍历元素,与最大值max作比较,将较大值附给max 输出最大值max 算法: /* *title:P53页程序设计第6题 *writer:weiyuexin ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- leetCode题解之删除单链表中指定的元素
1.问题描述 Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- 链表习题(8)-寻找单链表中数据域大小为k的结点,并与前一结点交换,如果前一结点存在的情况下
/*寻找单链表中数据域大小为k的结点,并与前一结点交换,如果前一结点存在的情况下*/ /* 算法思想:定义两个指针,pre指向前驱结点,p指向当前结点,当p->data == k的时候,交换 p ...
随机推荐
- C#使用Redis集群缓存
C#使用Redis集群缓存 本文介绍系统缓存组件,采用NOSQL之Redis作为系统缓存层. 一.背景 系统考虑到高并发的使用场景.对于并发提交场景,通过上一章节介绍的RabbitMQ组件解决.对于系 ...
- A Game of Thrones(16) - Edard
“They’ve found her, my lord.” Ned rose quickly. “Our men or Lannister’s?” “It was Jory,” his steward ...
- 函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网
函数式编程很难,这正是你要学习它的原因 | 外刊IT评论网 函数式编程很难,这正是你要学习它的原因 156 次分享 新浪微博 腾讯微博 Tweet 人人网 QQ空间 很奇怪不是,很少有人每天都使用函数 ...
- BZOJ 刷题记录 PART 6
[BZOJ2709]水的二分加验证.可是好像被读入萎到了... [BZOJ3229]强大的算法见此.被机房的一堆大神"推荐".于是被坑了...写了一个下午... [BZOJ3631 ...
- C++编程命名规范
原地址:http://www.cnblogs.com/joinclear/archive/2013/02/21/2921422.html C++编程命名规范 0前言 根据多年工作经验和其它命名规范整理 ...
- A Game of Thrones(15) - Sansa
Eddard Stark had left before dawn, Septa Mordane informed Sansa as they broke their fast. “The king ...
- Jeecg社区wiki在开放,最终能够在线看文档啦!!!
Jeecg社区wiki在开放,最终能够在线看文档啦! .! 2014-12-18 scott JEECG jeecg开源社区wiki正式启动了.方便大家看文档 訪问地址是: http://osbaba ...
- 新浪SAE数据库信息
此账号仅能在SAE平台上使用,不能从外部连接我们建议开发者使用SaeMysql操作数据库 如果您想自己实现数据库相关操作,可以使用以下常量: 用户名 : SAE_MYSQL_USER 密 码 : S ...
- 使用Spring的JAVA Mail支持简化邮件发送(转)
闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是Mail ...
- sql语句查询数据库中的表名/列名/主键/自动增长值
原文地址:http://blog.csdn.net/pukuimin1226/article/details/7687538 ----查询数据库中用户创建的表 ----jsj01 为数据库名 sele ...