利用链栈和队列判断回文
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef char Elemtype;
typedef int status;
typedef struct node
{
Elemtype data;
struct node *next;
}SqStack;
typedef struct Stack
{
SqStack *top;//栈顶
int count;
}stack;
typedef struct q
{
Elemtype data;
struct q *next;
}queue;
typedef struct d
{
queue *front,*rear;
}Queue;
int SqStack_Empty(stack *l)
{
if(l->count==0)//为空则返回1
{
// printf("栈为空!\n");
return 1;
}
else
{
// printf("栈不为空!\n");
return 0;
}
}
status push(stack *l,Elemtype e)
{
SqStack *s;
s=(SqStack *)malloc(sizeof(SqStack));
if(!s)
exit(OVERFLOW);
s->data=e;
s->next=l->top;
l->top=s;
l->count++;
return OK;
}
void EnQueue(Queue *Q,Elemtype e)
{
queue *p;
p=(queue *)malloc(sizeof(queue));
p->data=e;
p->next=NULL;
if(Q->front==Q->rear)
{
Q->front=p;
Q->rear=NULL;
queue *head;
head=(queue *)malloc(sizeof(queue));
Q->front=head;
head->next=p;
Q->rear=p;
}
else
{
Q->rear->next=p;
Q->rear=p;
}
}
status pop(stack *l,Elemtype *e)
{
if(SqStack_Empty(l))
return ERROR;
*e=l->top->data;
SqStack *p;
p=l->top;
l->top=p->next;
free(p);
l->count--;
return OK;
}
void DelQueue(Queue *l,Elemtype *e)
{
if(l->front==l->rear)
return ;
*e=l->front->next->data;
queue *p;
p=l->front->next;
l->front->next=p->next;
free(p);
}
void main()
{
stack *l;
Queue *Q;
Elemtype ch,a,b;
// Init_SqStack(l);
l=(stack *)malloc(sizeof(stack));
if(!l)
exit(OVERFLOW);
l->count=0;
l->top=NULL;
// Init_Queue(Q);
Q=(Queue *)malloc(sizeof(Queue));
if(!Q)
exit(OVERFLOW);
Q->front=NULL;
Q->rear=Q->front;
printf("'@'为结束符\n");
ch=getchar();
while(ch!='@')
{
//putchar(ch);
push(l,ch);
EnQueue(Q,ch);
ch=getchar();
}
while(!SqStack_Empty(l))
{
pop(l,&a);
DelQueue(Q,&b);
if(a!=b)
{
printf("这不是回文串!\n");
return ;
}
}
printf("这是一个回文串!\n");
}