using namespace std;
//定義棧
typedef struct stack {
char bata;
struct stack *next;
}Stack;
//定義隊列
typedef struct QNode {
char data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int main()
{
Stack *top = new Stack;
if(!top) exit(1);
top = NULL;//初始化棧
LinkQueue *x = new LinkQueue;
if(!x) exit(1);
x->front = x->rear=NULL;//初始化隊列
cout<<"請輸入要檢測的字符串,可以包含空格,以@結束"<<endl;
char sr[100];
sr[0] = ' ';
int i=-1;
do{
i++;
cin>>sr[i];
stack *s1 = new stack;
if(!s1) exit(1);
if(i==0)
{
s1->next = NULL; //輸入棧第壹個數據
s1->bata=sr[i];
top = s1;
}
s1->bata = sr[i];
s1->next = top;
top = s1;
QNode *x1 = new QNode;
if(!x1) exit(1);
if(i==0) //輸入隊列第壹個數據
{
x1->data = sr[i];
x->front = x1;
x->rear = x1;
x->front->next = NULL;
}
x1->data = sr[i];
x->rear->next = x1;
x->rear = x1;
} while(sr[i]!='@');
cout<<"檢測結果"<<endl;
if(top==NULL&&x->front==NULL)
cout<<"未輸入!!!"<<endl;
top = top->next; //將'@'字符刪除
while(top!=NULL&&x->front!=NULL)
{
if(top->bata!=x->front->data)
{
cout<<"此字符串不是回文!"<<endl;
return 0;
}
else
{
cout<<"此字符串是回文。"<<endl;
return 0;
}
}
}裏面有具體操作沒有用函數分開寫,但都是按棧和隊列寫的