#include "stdio.h"
#include "malloc.h"
#include
<string.h>
typedef struct LNode
{
char str;
struct LNode *next;
}LNode;
typedef struct
{
LNode *front;
LNode *rear;
}LinkLNode;
typedef struct
{
char data[100];
int top;
}SqStack;
void InitList(LinkLNode *p)
{
p->front = p->rear = NULL;
}
void EnList(LinkLNode *p,char s)
{
LNode *q = (LNode *)malloc(sizeof(LNode));
q->str = s;
q->next = NULL;
if(p->front == NULL)
p->front = p->rear = q;
else
p->rear->next = q;
p->rear = q;
}
void ShowList(LinkLNode *p)
{
LNode *q = p->front;
if(p->front == NULL)
{
printf("没有输入测试字符!");
}
else
while(q != NULL)
{
printf("%c",q->str);
q = q->next;
}
}
char DeList(LinkLNode *p)
{
char c;
LNode *q = p->front;
q = p->front;
c = q->str;
p->front = q->next;
if(p->front == NULL)
p->rear = NULL;
free(q);
return c;
}
void InitStack(SqStack *q)
{
memset(q->data, 0, 100);
q->top = -1;
}
void Ruzhan(SqStack*q,char s)
{
++q->top;
q->data[q->top]=s;
}
char Chuzhan(SqStack *q)
{
return (q->data[q->top--]);
}
void ShowStack(SqStack *q)
{
int i;
for(i=q->top;i>=0;--i)
printf("%c ",q->data[i]);
printf("\n");
}
void main()
{
int x=0,y;
char s;
char A[20],B[20];
LinkLNode p;
SqStack q ;
InitList(&p);
InitStack(&q);
printf("请输入要测试的字符:(按 0 结束输入!)\n");
while((scanf("%c",&s)) == 1)
{
if(s == '0')
break;
EnList(&p,s);
Ruzhan(&q,s);
fflush(stdin);
x++;
}
for(y=0;y<x;y++)
{
A[y]
= DeList(&p);
B[y]
= Chuzhan(&q);
if(A[y] != B[y])
{
printf("不是回文!");
break;
}
else
{
printf("是回文!");
break;
}
}
}
给个你参考下
呵呵
我用的是栈