當前位置:編程學習大全網 - 源碼下載 - 回文判斷,寫壹個C++程序同時用棧和隊列判斷回文(急!!)

回文判斷,寫壹個C++程序同時用棧和隊列判斷回文(急!!)

#include <iostream>

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;

}

}

}裏面有具體操作沒有用函數分開寫,但都是按棧和隊列寫的

  • 上一篇:uu跑腿怎麽加入兼職
  • 下一篇:那位老師幫我編寫壹個通達信選股公式:要求***振信號(就是黃色線)剛剛出現1天或者2天時的選股公式
  • copyright 2024編程學習大全網