栈和队列实际应用对回文数字 各种树的学习
在今天将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;
}
然后我了解了树的相关知识,有完全树,还有满树,各种二叉树,还有三叉树,这种要记得东西感觉还有很多...