转载请注明出处: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个字符,试设计一个算法,使用栈推断该字符串是否中心对称的更多相关文章

  1. 一个线性表中的元素为整数,设计一个算法,将正整数和负整数分开,使线性表的前一半为负整数,后一半为正整数。(C语言)

    以下为完整可运行示例代码: #include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; str ...

  2. 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除。(C语言)

    /* 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除 */ #include <stdio.h> #include <stdlib.h> typedef st ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. [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 ...

  5. 数据结构作业——P53算法设计题(6):设计一个算法,通过一趟遍历确定长度为n的单链表中值最大的结点

    思路: 设单链表首个元素为最大值max 通过遍历元素,与最大值max作比较,将较大值附给max 输出最大值max 算法: /* *title:P53页程序设计第6题 *writer:weiyuexin ...

  6. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  7. leetCode题解之删除单链表中指定的元素

    1.问题描述 Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  8. SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除

    数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  9. 链表习题(8)-寻找单链表中数据域大小为k的结点,并与前一结点交换,如果前一结点存在的情况下

    /*寻找单链表中数据域大小为k的结点,并与前一结点交换,如果前一结点存在的情况下*/ /* 算法思想:定义两个指针,pre指向前驱结点,p指向当前结点,当p->data == k的时候,交换 p ...

随机推荐

  1. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

    1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...

  2. CentOS6-釋放ip重新分配,centos7 ifconifg没有ip

    http://bbs.csdn.net/topics/390725823 系统win7 ,dhcp自动获取ip虚拟机是10.0 安装之后我装了ubuntu  选用 NAT网络, 刚装完我能上网 ,但是 ...

  3. [Android学习笔记]Bitmap,BitmapDrawable,BitmapFactory学习笔记

    Bitmap:图片文件的封装,可以看做是一张位图此类中的静态方法可以通过源Bitmap创建新的Bitmap对象此类封装了位图的一些信息Bitmap文档 BitmapFactory:一个工具类,用于创建 ...

  4. java学习笔记12--国际化

    java学习笔记12--国际化 国际化的操作就是指一个程序可以同时适应多门语言,即:如果现在程序者是中国人,则会以中文为显示文字,如果现在程序的使用者是英国人,则会以英语为显示的文字,也就是说可以通过 ...

  5. linux c socket 案源

    service结束 #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #inclu ...

  6. 黑马程序员:Java基础总结----类加载器

    黑马程序员:Java基础总结 类加载器   ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! 类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个 ...

  7. NET实现的DDD、CQRS与微服务架构

    WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间 ...

  8. C编译: makefile基础

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在编译一个大型项目的时候,往往有很多目标文件.库文件.头文件以及最终的可执行文件. ...

  9. mysql登录报错 ERROR 1045 (28000)

    1.现象: [root@localhost ~]# mysql -u root -p Enter password:  ERROR 1045 (28000): Access denied for us ...

  10. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)

    配置项目,从tomcat低版本,放到tomcat8时,正常的项目居然报错了: The method getDispatcherType() is undefined for the type Http ...