回文判断
问题描述
\quad 回文是指一个字符序列以中间字符为基准两边字符完全相同。要求程序从键盘输入一个字符串,用于判断回文的不包括字符串的结束标志。
算法思想
\quad 把字符串中的字符逐个分别存入队列和堆栈,然后逐个出队列和退栈并比较出队列的元素和退栈的元素是否相等,若全部相等则该字符是回文,否则就不是回文。
函数模块
( 1 ) v o i d P a l i n d r o m e ( c h a r s t r [ ] ) (1) void Palindrome(char str[]) (1)voidPalindrome(charstr[]),判断字符序列是否为回文。
( 2 ) v o i d E n t e r S t r ( c h a r s t r [ ] ) (2) void EnterStr(char str[]) (2)voidEnterStr(charstr[]),从键盘输入回文序列。
( 3 ) v o i d m a i n ( ) (3) void main() (3)voidmain(),主函数,循环调用 P a l i n d r o m e ( c h a r s t r [ ] ) , E n t e r S t r ( c h a r s t r [ ] ) Palindrome(char str[]),EnterStr(char str[]) Palindrome(charstr[]),EnterStr(charstr[]),当用户要求退出时,停止运行。
( 4 ) (4) (4) 堆栈和队列分别利用 S t a c k . h Stack.h Stack.h和 L Q u e u e . h LQueue.h LQueue.h文件。
测试数据
( 1 ) A B C D E F E D C B A (1) ABCDEFEDCBA (1)ABCDEFEDCBA
( 2 ) A B C D E F F E D C B A (2)ABCDEFFEDCBA (2)ABCDEFFEDCBA
( 3 ) A B C D B C A (3)ABCDBCA (3)ABCDBCA
C语言程序
头文件
S t a c k . h Stack.h Stack.h
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#endif // SEQLIST_H_INCLUDED
//typedef int DataType;
typedef struct snode
{
DataType data;
struct snode *next;
}LSNode;
void StackInitiate(LSNode **head)
{
*head=(LSNode *)malloc(sizeof(LSNode));
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next==NULL)return 0;
else return 1;
}
int StackPush(LSNode *head,DataType x)
{
LSNode *p;
p=(LSNode *)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;//新的结点插入在头结点后面
head->next=p;//新结点成为栈顶
return 1;
}
int StackPop(LSNode *head,DataType *x)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("此栈已空!\n");
return 0;
}
head->next=p->next;//删除原结点
*x=p->data;
free(p);
return 1;
}
int StackTop(LSNode *head,DataType *x)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("堆栈已空!\n");
return 0;
}
*x=p->data;
return 1;
}
void Destory(LSNode *head)
{
LSNode *p,*s;
p=head;
while(p!=NULL)
{
s=p;
p=p->next;
free(s);
}
}
L q u e u e . h Lqueue.h Lqueue.h
#ifndef LQUEUE_H_INCLUDED
#define LQUEUE_H_INCLUDED
#endif // LQUEUE_H_INCLUDED
//typedef int DataType;
typedef struct qnode
{
DataType data;//为什么不在此处定义两个指针
struct qnode *next;//此处需要使用qnode,因此typedef struct qnode
}LQNode;//定义结点
typedef struct
{
//不带头结点
LQNode *front;
LQNode *rear;
}LQueue;//只是两个指针
void QueueInitiate(LQueue *Q)
{
Q->front=NULL;
Q->rear=NULL;
}
int QueueNotEmpty(LQueue Q)
{
if(Q.front==NULL)return 0;
else return 1;
}
void QueueAppend(LQueue *Q,DataType x)
{
LQNode *p;
p=(LQNode *)malloc(sizeof(LQNode));//申请结点空间
p->data=x;
p->next=NULL;
//rear->a(n-1),为最后一个元素
if(Q->rear!=NULL)Q->rear->next=p;//队列非空时,将结点p插入在队尾
Q->rear=p;//修改队尾指针
if(Q->front==NULL)Q->front=p;//队列为空时,修改队头指针
}
int QueueDelete(LQueue *Q,DataType *x)
{
LQNode *p;
if(Q->front==NULL)
{
printf("队列已空,无可删元素!\n");
return 0;
}
else
{
*x=Q->front->data;//取队头元素数据
p=Q->front;//记录队头指针
Q->front=Q->front->next;//出队列,结点脱链
if(Q->front==NULL)Q->rear=NULL;//队列中只有一个元素的情况
free(p);
return 1;
}
}
int QueeuGet(LQueue *Q,DataType *x)
{
if(Q->front==NULL)
{
printf("队列已空,无元素可取!\n");
return 0;
}
else
{
*x=Q->front->data;
return 1;
}
}
void DestoryList(LQueue *Q)
{
LQNode *p,*p1;//释放空间时必须要两个指针
p=Q->front;
while(p->next!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
主文件
#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
#include"Stack.h"
#include"LQueue.h"
#include<string.h>
void Enterstr(char str[])
{
printf("请输入字符串:\n");
scanf("%s",str);
}
void Palindorme(char str[])
{
int len,i,j;
char p,q;
len=strlen(str);
LQueue Q;
LSNode *S;
QueueInitiate(&Q);
StackInitiate(&S);
for(i=0;i<len;i++)
{
StackPush(S,str[i]);
QueueAppend(&Q,str[i]);
}
j=0;
while(StackNotEmpty(S)==1&&QueueNotEmpty(Q)==1)
{
StackPop(S,&p);
QueueDelete(&Q,&q);
if(p==q)j++;
}
if(j==len)
printf("此字符串是回文!\n");
else
printf("此字符串不是回文!!\n");
}
int main()
{
void Enterstr(char str[]);
void Palindorme(char str[]);
char s1[100],s;
while(1)
{
Enterstr(s1);
Palindorme(s1);
printf("输入N退出!\n");
scanf("%s",&s);
if(s=='N')break;
}
return 0;
}