69,378
社区成员
发帖
与我相关
我的任务
分享
#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define QUEUEMAXSIZE 50
#define INCREMENTSIZE 10
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
typedef struct
{
char *qbase;
int front;
int rear;
}Queue;
void InitStack(Stack &J)
{
J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base;
J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
free(J.base);
J.base=NULL;
J.top=NULL;
J.stacksize=0;
}
int StackEmpty(Stack J)
{
if(J.base==J.top)
return 1;
else
return 0;
}
void Push(Stack &J,char &e)
{
if(J.top>=J.base)
J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base+J.stacksize;
J.stacksize+=INCREMENTSIZE;
*(J.top)++=e;
}
void Pop(Stack &J, char e)
{
if(StackEmpty(J))
exit(-1);
e=*--J.top;
}
void InitQueue(Queue &K)
{
K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
if(K.front==K.rear)
exit(-1);
free(K.qbase);
K.qbase=NULL;
K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
if(K.front==K.rear)
return 1;
else
return 0;
}
void EnQueue(Queue &K,char f)
{
if((K.rear+1)%QUEUEMAXSIZE==0)
exit(-1);
K.qbase[K.rear]=f;
K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K,char f)
{
if(K.front==K.rear)
exit(-1);
f=K.qbase[K.front];
K.front=(K.front+1)%QUEUEMAXSIZE;
}
void Puen(Stack &J,Queue &K,char *r)
{
while((*r)!='@')
{
Push(J,*r);
EnQueue(K,*r);
r++;
}
}
void Pal(Stack &J,Queue &K)
{
while(*(J.top-1)==(K.qbase[K.front]))
{
Pop(J,*(J.top-1));
DeQueue(K,(K.qbase[K.front]));
}
if(StackEmpty(J)&& QueueEmpty(K))
printf("您输入的字符数列是回文数列!\n");
else
printf("您输入的字符数列不是回文数列!\n");
}
int main()
{
char a[50];
Stack s;
Queue q;
printf("请输入字符序列以‘@’加上回车结束:\n");
gets(a);
printf("您输入的字符数列为:\n");
puts(a);
InitStack(s);
InitQueue(q);
Puen(s,q,a);
Pal(s,q);
DestoryStack(s);
DestoryQueue(q);
return 0;
}
#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define QUEUEMAXSIZE 50
#define INCREMENTSIZE 10
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
typedef struct
{
char *qbase;
int front;
int rear;
}Queue;
void InitStack(Stack &J)
{
J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base;
J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
free(J.base);
J.base=NULL;
J.top=NULL;
J.stacksize=0;
}
int StackEmpty(Stack J)
{
if(J.base==J.top)
return 1;
else
return 0;
}
void Push(Stack &J,char e)
{
if((J.top-J.base)>=J.stacksize){
J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base+J.stacksize;
J.stacksize+=INCREMENTSIZE;
}
*(J.top)++=e;
}
void Pop(Stack &J, char &e)
{
if(StackEmpty(J))
exit(-1);
e=*--J.top;
}
void InitQueue(Queue &K)
{
K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
if(K.front==K.rear)
exit(-1);
free(K.qbase);
K.qbase=NULL;
K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
if(K.front==K.rear)
return 1;
else
return 0;
}
void EnQueue(Queue &K,char f)
{
if((K.rear+1)%QUEUEMAXSIZE==0)
exit(-1);
K.qbase[K.rear]=f;
K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K,char &f)
{
if(K.front==K.rear)
exit(-1);
f=K.qbase[K.front];
K.front=(K.front+1)%QUEUEMAXSIZE;
}
void Puen(Stack &J,Queue &K,char *r)
{
while((*r)!='@')
{
Push(J,*r);
EnQueue(K,*r);
r++;
}
}
void Pal(Stack &J,Queue &K)
{char a,b;
// while(*(J.top-1)==(K.qbase[K.front]))
/*printf("\nStack: ");
char *p=J.top-1;
while (p!=J.base)
{ printf("%d ",*p);
p--;
}
printf("%d ",*p);
*/
while(!StackEmpty(J))
{
Pop(J,a);
DeQueue(K,b);
printf("a=%d,b=%d\n",a,b);
if (a!=b)
break;
}
if(StackEmpty(J)==1)
printf("您输入的字符数列是回文数列!\n");
else
printf("您输入的字符数列不是回文数列!\n");
}
int main()
{
char a[50];
Stack s;
Queue q;
printf("请输入字符序列以‘@’加上回车结束:\n");
gets(a);
printf("您输入的字符数列为:\n");
puts(a);
InitStack(s);
InitQueue(q);
Puen(s,q,a);
Pal(s,q);
DestoryStack(s);
DestoryQueue(q);
return 0;
}
void Push(Stack &J,char &e) //这里出现错误,自己调试跟踪吧
#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define QUEUEMAXSIZE 50
#define INCREMENTSIZE 10
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
typedef struct
{
char *qbase;
int front;
int rear;
}Queue;
void InitStack(Stack &J)
{
J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base;
J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
free(J.base);
J.base=NULL;
J.top=NULL;
J.stacksize=0;
}
int StackEmpty(Stack J)
{
if(J.base==J.top)
return 1;
else
return 0;
}
void Push(Stack &J,char e)
{
if((J.top-J.base)>=J.stacksize){
J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base+J.stacksize;
J.stacksize+=INCREMENTSIZE;
}
*(J.top)++=e;
}
void Pop(Stack &J)
{
if(StackEmpty(J))
exit(-1);
--J.top;
}
void InitQueue(Queue &K)
{
K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
if(K.front==K.rear)
exit(-1);
free(K.qbase);
K.qbase=NULL;
K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
if(K.front==K.rear)
return 1;
else
return 0;
}
void EnQueue(Queue &K,char f)
{
if((K.rear+1)%QUEUEMAXSIZE==0)
exit(-1);
K.qbase[K.rear]=f;
K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K)
{
if(K.front==K.rear)
exit(-1);
K.front=(K.front+1)%QUEUEMAXSIZE;
}
void Puen(Stack &J,Queue &K,char *r)
{
while((*r)!='@')
{
Push(J,*r);
EnQueue(K,*r);
r++;
}
}
void Pal(Stack &J,Queue &K)
{
//char a,b;
/*while(*(J.top-1)==(K.qbase[K.front]))
printf("\nStack: ");
char *p=J.top-1;
while (p!=J.base)
{ printf("%d ",*p);
p--;
}
printf("%d ",*p);
*/
//while(!StackEmpty(J))
while(*(J.top-1)==(K.qbase[K.front]))
{
Pop(J);//Pop(J,a)
DeQueue(K);//DeQueue(K),
//printf("a=%d,b=%d\n",a,b);
//if (a!=b)
//break;
}
if(StackEmpty(J))
printf("您输入的字符数列是回文数列!\n");
else
printf("您输入的字符数列不是回文数列!\n");
}
int main()
{
char a[50];
Stack s;
Queue q;
printf("请输入字符序列以‘@’加上回车结束:\n");
gets(a);
printf("您输入的字符数列为:\n");
puts(a);
InitStack(s);
InitQueue(q);
Puen(s,q,a);
Pal(s,q);
DestoryStack(s);
DestoryQueue(q);
return 0;
}
for(int i = 0; i < n / 2; ++i)
{
if(s[i] != s[n - 1 - i])
{
return false;
}
}
return true
直接比较就行了,用栈是个什么情况