第八章十一题powerby spritsq
来源:互联网 发布:中国历年进出口数据 编辑:程序博客网 时间:2024/06/05 08:55
//
//8_11 判断字符串是否是对称的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSIZE 1024
typedef char datatype;
struct stack
{
datatype elements[MAXSIZE];
int top;
};
struct node
{
char data;
struct node *next;
};
typedef node linklist;
//创建字符串
linklist *Creat()
{
linklist *head,*node,*r;
head = NULL;
r = NULL;
char ch;
printf("请输入一个字符串(以$结束):/n");
ch = getche();
printf("/n");
while(ch != '$')
{
node = (linklist *)malloc(sizeof(linklist));
node->data = ch;
if(head == NULL)head = node;
else r->next = node;
r = node;
printf("请输入一个字符:/n");
ch = getche();
//ch = getchar();
printf("/n");
}
if(r != NULL)
r->next = NULL;
return head;
}
//测量字符串长度
int ListLen(linklist *p)
{
linklist *temp = p;
int length = 0;
if(temp == NULL)
{
printf("字符串为空!");
}
else
{
while(temp)
{
length++;
temp = temp->next;
}
}
return (length);
}
void SetNull(struct stack *s)
{
s->top = -1;
}
int Empty(struct stack *s)
{
if(s->top >= 0) return (0);
else return (1);
}
//进栈:将字符串的一半压入栈内
struct stack *Push(struct stack *s,linklist *p)
{
int len = ListLen(p);
if(s->top >= MAXSIZE-1)
{
printf("stack overflow!");
return NULL;
}
else
{
for(int i=0; i<(len/2); i++)
{
s->top++;
s->elements[s->top] = p->data;
p = p->next;
}
}
return (s);
}
//出栈
datatype Pop(struct stack *s)
{
datatype temp;
if(Empty(s))
{
printf("Stack underflow!");
return (-1);
}
else
{
s->top--;
temp = s->elements[s->top+1];
return (temp);
}
}
//比较字符串
int Compare(struct stack *s,linklist *p)
{
datatype temp;
int len, mid,count;
len = ListLen(p);
if(len%2 == 0)
{
mid = len/2;
}
else mid = len/2 +1;
for(int i=0; i<mid; i++)
{
p = p->next;
}
count = 0;
while((!Empty(s))&&p)
{
temp = Pop(s);
if(temp != p->data)
{
printf("该字符串不是对称字符串!");
return (0);
}
else
p = p->next;
count++;
}
if(((len%2 == 0)&&(count == mid))||((len%2 != 0)&&(count == mid-1)))
return (1);
}
void main()
{
linklist *strlist;
stack *str;
str = (stack *)malloc(sizeof(stack));
strlist = Creat();
SetNull(str);
Push(str,strlist);
if(Compare(str,strlist))
{
while(strlist)
{
printf("%c",strlist->data);
strlist = strlist->next;
}
printf("/n该字符串是一对称字符串!");
}
//释放资源……
while(strlist)
{
free(strlist);
strlist = strlist->next;
}
}
- 第八章十一题powerby spritsq
- 第八章十一题修改后(用栈实现)POWERBY KTL
- 第八章十六题修改后POWERBY KTL
- 第八章十一题,修改了 ,用单链表实现str.(不好意思又看错条件了,这次没有错误了。)powerby KTL
- 第十章第十六题 POWERBY KTL
- 《老子》第八十一章
- 第九章第十七题(赶出来的作业) powerby ktl
- java语言程序设计第八版第十章十一题
- 第九章第14题(赶出来的作业) POWERBY KTL
- 【100题】第八十一 ~ 第九十题
- 第就章第21题(赶出来的作业) POWERBY KTL
- 第十五题用拉链法实现散列表 POWERBY KTL
- 第13题用线性探查法实现的散列表 POWERBY KTL
- 第二章第八题
- 第二章第八题
- 第三章第八题
- 第三章第八题
- 【八十一题题目合集 微软面试100题 第八十一题】
- SQL Server 2005数据库开发详解抢先试读
- 在使用中文名导出数据到excel文件时文件名出现乱码的解决办法
- Dreamweaver 8.0.2 Updater 05/09/06
- Hibernate中的检索1-----主控对象
- 多层架构 增加扩展性
- 第八章十一题powerby spritsq
- db2 编目远程数据库到本地
- Asp技术群-飘柔家族专用AJAX免费短信发送
- 数据集的使用方法和技巧
- 第八章十一题,修改了 ,用单链表实现str.(不好意思又看错条件了,这次没有错误了。)powerby KTL
- Flex组件篇:HRule和VRule
- 修改了一下StringLayout,增加了翻页功能,并且还有了滚动条,希望多多提意见.
- Flex组件篇:Label
- ibatis - 自动代码生成工具abator 使用介绍(s00n原作)