第1关 顺序栈的基本操作
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
typedef int SElemType; // 定义栈元素类型为整型
/* 顺序栈类型定义 */
typedef struct
{
SElemType *base; //栈的基址即栈底指针
SElemType *top; //栈顶指针
int stacksize; //当前分配的空间
}SqStack;
void input(SElemType &s);
void output(SElemType s);
void InitStack(SqStack &S);// 构造一个空栈S
void DestroyStack(SqStack &S); // 销毁栈S,S不再存在
void ClearStack(SqStack &S); // 把S置为空栈
int StackEmpty(SqStack S); // 若栈S为空栈,则返回TRUE,否则返回FALSE
int StackLength(SqStack S); // 返回S的元素个数,即栈的长度
int GetTop(SqStack S,SElemType &e); // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
void Push(SqStack &S,SElemType e); // 插入元素e为新的栈顶元素
int Pop(SqStack &S,SElemType &e); // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
void StackTraverse(SqStack S,void(*visit)(SElemType)); // 从栈底到栈顶依次对栈中每个元素调用函数visit()
int main()
{
int j;
SqStack s;
SElemType e;
InitStack(s);
int i;
cin>>i;
for(j=0;j<i;j++)
{
input(e);
Push(s,e);
}
printf("栈中元素依次为:");
StackTraverse(s,output);
Pop(s,e);
printf("弹出的栈顶元素 e=%d\n",e);
printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));
GetTop(s,e);
printf("栈顶元素 e=%d 栈的长度为%d\n",e,StackLength(s));
ClearStack(s);
printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
DestroyStack(s);
printf("销毁栈后,s.top=%u s.base=%u s.stacksize=%d\n",s.top,s.base, s.stacksize);
}
/*****SElemType类型元素的基本操作*****/
void input(SElemType &s)
{
cin>>s;
}
void output(SElemType s)
{
cout<<s<<" ";
}
/*****顺序栈的基本操作*****/
void InitStack(SqStack &S)
{
// 构造一个空栈S
/********** Begin **********/
S.base=(SElemType*)malloc(STACK_INIT_SIZE);
S.stacksize=STACK_INIT_SIZE;
S.top=S.base;
/********** End **********/
}
void DestroyStack(SqStack &S)
{
// 销毁栈S,S不再存在
/********** Begin **********/
// free(S.base);
// free(S.top);
S.base=S.top=NULL;
S.stacksize=0;
/********** End **********/
}
void ClearStack(SqStack &S)
{
// 把S置为空栈
/********** Begin **********/
S.base=S.top=NULL;
S.stacksize=0;
/********** End **********/
}
int StackEmpty(SqStack S)
{
// 若栈S为空栈,则返回TRUE,否则返回FALSE
/********** Begin **********/
if(S.top==S.base)
return 1;
else
return 0;
/********** End **********/
}
int StackLength(SqStack S)
{
// 返回S的元素个数,即栈的长度
/********** Begin **********/
int m;
m=S.top-S.base;
return m;
/********** End **********/
}
int GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
/********** Begin **********/
if(S.top!=S.base)
{
e=*(S.top-1);
return 1;
}
else
return 0;
/********** End **********/
}
void Push(SqStack &S,SElemType e)
{
// 插入元素e为新的栈顶元素
/********** Begin **********/
*(S.top)++=e;
/********** End **********/
}
int Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
/********** Begin **********/
if(S.base!=S.top)
{
e=*--S.top;
return 1;
}
else
return 0;
/********** End **********/
}
void StackTraverse(SqStack S,void(*visit)(SElemType))
{
// 从栈底到栈顶依次对栈中每个元素调用函数visit()
/********** Begin **********/
while(S.top!=S.base)
{
visit(*S.base++);
}
printf("\n");
/********** End **********/
}
第2关 栈的应用-进制转换
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
typedef int SElemType; // 定义栈元素类型为整型
#include "sqstack.h" // 顺序栈的类型定义
void conversion(unsigned n)
{
// 对于输入的任意一个非负10进制整数,打印输出与其等值的16进制数
/********** Begin **********/
if(n>0)
{
conversion(n/16);
if(n%16<10)
printf("%c",'0'+n%16);
else
printf("%c",'A'+n%16-10);
}
/********** End **********/
}
int main()
{
unsigned n; // 非负整数
scanf("%u",&n); // 输入非负十进制整数n
conversion(n);
}
第3关 栈的应用-回文的判断
#include<stdio.h>
#include<string.h>
#include"sqstack.h" //头文件sqstack.h为顺序栈的定义和基本操作的实现
typedef char SElemType;// 定义栈元素类型为字符型
# define MAXSIZE 80
int main()
{
/********** Begin **********/
char a[80];
scanf("%s",a);
int i=0,length;
length=strlen(a);
printf("%s\n",a);
int m=0,n=0,j=0,t;
m=length/2;
if(length%2)
n=m+1;
else
n=m;
for(i=0,j=length-1;i<m,j>=n;i++,j--)
{
if(a[i]==a[j])
t=0;
else
t=1;
}
if(t==0)
printf("YES");
else
printf("NO");
/********** End **********/
return 0;
}