在今天將PTA上的作業迴文數的判斷完成了,正好和我昨天進行的課本書寫是一樣的,具體程式碼如下:
include
include
define MAXSIZE 100
using namespace std;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
void init_stack(SqStack &s)
{
s.base = new int[MAXSIZE];
if(!s.base) cout<<"ERROR!"<<endl;
s.top = s.base;
s.stacksize = MAXSIZE;
}
bool IsEmpty(SqStack &s)
{
if(s.top==s.base){
return 1;
}else {
return 0;
}
}
bool overflow(SqStack &s)
{
if(s.top - s.base == s.stacksize) return 1;
else return 0;
}
void Push(SqStack &s,int number)
{
if(overflow(s)) cout<<"棧已滿無法入棧!";
else {
*s.top++ = number;
}
}
void Pop(SqStack &s)
{
if(IsEmpty(s)) cout<<"棧空,無法出棧!";
else {
s.top--;
}
}
int Get_top(SqStack &s)
{
if(!IsEmpty(s)) return *(s.top-1);
}
typedef struct
{
int *base;//佇列的基地址
int front;
int rear;
}SqQueue;
void init_Queue(SqQueue &Q)
{
Q.base = new int[MAXSIZE];
if(!Q.base) cout<<"儲存分配失敗";
Q.rear = Q.front = 0;
}
int QueueLength(SqQueue &Q)
{
return (Q.rear-Q.front + MAXSIZE) % MAXSIZE;
}
void EnQueue(SqQueue &Q,int number)
{
if((Q.rear + 1) % MAXSIZE == Q.front) cout<<"隊滿,無法入隊";
else{
Q.base[Q.rear] = number;
Q.rear = (Q.rear + 1) % MAXSIZE;
}
}
void DeQueue(SqQueue &Q)
{
if(Q.front==Q.rear) cout<<"隊空,無法出隊!";
else {
Q.front = (Q.front + 1)%MAXSIZE;
}
}
int GetHead(SqQueue &Q)
{
if(Q.front!=Q.rear){
return Q.base[Q.front];
}
}
bool QueueisEmpty(SqQueue &Q)
{
if(Q.rear==Q.front) return 1;
else {
return 0;
}
}
void check(int number)
{
SqStack my_stack;
init_stack(my_stack);//初始化
int temp = number;
SqQueue my_Queue;
init_Queue(my_Queue);
while(temp > 0){
int x = temp%10;
Push(my_stack,x);
EnQueue(my_Queue,x);
temp/=10;
}
while(!IsEmpty(my_stack)&&!QueueisEmpty(my_Queue)){
if(Get_top(my_stack)!=GetHead(my_Queue)){
cout<<"該字串不是迴文字串"<<endl;
return;
}
Pop(my_stack);
DeQueue(my_Queue);
}
cout<<"該字串是迴文字串"<<endl;
}
int main()
{
int number = 0;
string str;
getline(cin,str);
number = stoi(str);
check(number);
return 0;
}
然後我瞭解了樹的相關知識,有完全樹,還有滿樹,各種二叉樹,還有三叉樹,這種要記得東西感覺還有很多...