I'm a rookie in data structures .今天上机实习了一下回文串着一章向大家汇报。(drmao
drmao 2003-10-29 10:39:58 #include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define Status int
#define StackElemType char
#define QueueElemType char
#define STACKINITSIZE 100
#define STACKINCREMENT 10
#define QUEUEINITSIZE 100
#define QUEUEINCREMENT 10
typedef struct Stack{
StackElemType *base;
StackElemType *top;
int stacksize;
}SqStack;
////////////////////////////////////////////////////
typedef struct QNode{
QueueElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct LinkQueue{
QueuePtr front;
QueuePtr rear;
}LinkQueue;///////////////////////////////////////////////////////////////////////////////////////
Status InitQueue(LinkQueue &Q){
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
Q.rear->next=0;
return 1;
}/////////////////////////////////////////////
Status DestroyQueue(LinkQueue &Q){
while(Q.front){ Q.rear=Q.front->next; free(Q.front);Q.front=Q.rear; }
return 1;
}/////////////////////////////////////////////////////
Status EnQueue(LinkQueue &Q,QueueElemType e){QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
s->data=e; s->next=0; Q.rear->next=s; Q.rear=s;
return 1;
}//////////////////////////////////////////////////////////
Status DeQueue(LinkQueue &Q,QueueElemType &e){QueuePtr p=0;
if(Q.front==Q.rear){printf("\nQUEUE EMPTY!");return 0;}
p=Q.front->next; e=p->data; Q.front->next=p->next;
if(p==Q.rear)Q.rear=Q.front;
free(p);
return 1;
}////////////////////////////////////////////////////
Status QueueEmpty(LinkQueue &Q){return Q.front==Q.rear;
}///////////////////////////////////////////////////
Status InitSqStack(SqStack &S){
S.top=S.base=(StackElemType*)malloc(STACKINITSIZE*sizeof(StackElemType));
S.stacksize=STACKINITSIZE;
return 1;
}//////////////////////////////////////////////
Status Push(SqStack &S,StackElemType e){
if((S.top-S.base)>=S.stacksize-1){
S.base=(StackElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(StackElemType));
S.stacksize+=STACKINCREMENT;
}
*(S.top++)=e;
return 1;
}////////////////////////////////////
Status GetTop(SqStack &S,StackElemType &e){
if(S.top==S.base)return 0;
e=*(S.top-1);
return 1;
}/////////////////////////////////////////////////////
Status Pop(SqStack &S,StackElemType &e){
if(S.top==S.base){return 0;}
e=*(--S.top);
return 1;
}///////////////////////////////////////
Status StackEmpty(SqStack &S){ return S.base==S.top;
}/////////////////////////////////////////////////////
Status Test(char T){int gap='a'-'A'; char t;
SqStack S; LinkQueue Q; InitSqStack(S); InitQueue(Q);
if(T>'Z'||T<'A'){
loop: printf("\nT要大写的英文字母请再次输入,按#退出");
scanf("%c",&T);
if(T=='#')return 0;
if(T>'Z'||T<'A') goto loop;
}
while(T>='A'){Push(S,T),EnQueue(Q,T);T--;}
Pop(S,t);
while(!QueueEmpty(Q)){ DeQueue(Q,t);
if((t-'A')%2==1)printf("%3c",t+gap);
else printf("%3c",t);
}
while(!StackEmpty(S)){ Pop(S,t);
if((t-'A')%2==1)printf("%3c",t+gap);
else printf("%3c",t);
}
return 1;
}
/////////////////////////////////
int main(){ char t;scanf("%c",&t);
Test(t);
return 1;
}////////////////////////////////////////