//8583 顺序栈的基本操作
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
// 请补全代码
S.base=new SElemType[STACK_INIT_SIZE];
if(!S.base)
return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
if(S.top-S.base==S.stacksize)
return ERROR;
else
{
*S.top=e;
S.top++;
return OK;
}
}
Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
if(S.base==S.top)
return ERROR;
else
{
S.top--;
e=*S.top;
return OK;
}
}
Status GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码
if(S.base==S.top)
return ERROR;
else
{
e=*(S.top-1);
return OK;
}
}
int StackLength(SqStack S)
{
// 返回栈S的元素个数
// 请补全代码
return S.top-S.base;
}
Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
SElemType *p = (SElemType *)malloc(sizeof(SElemType));
p = S.top; //请填空
if(StackLength(S)==0)
printf("The Stack is Empty!");
else
{
printf("The Stack is: ");
while(p>S.base) //请填空
{
p--;
printf("%d ", *p);
//请填空
}
}
printf("\n");
return OK;
}
//8584 循环队列的基本操作
#include <malloc.h>
#include <stdio.h>
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
using namespace std;
typedef int Status;
typedef int QElemType;
//初始化
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
//创建空队列
Status InitQueue(SqQueue &Q)
{
Q.base=new QElemType[MAXQSIZE];
Q.front=Q.rear=0;
return OK;
}
//入队
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
//出队
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.rear==Q.front) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
//取对头元素
Status GetHead(SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
return OK;
}
//求队列元素个数
Status QueueLength(SqQueue &Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
//遍历输出队列元素
Status QueueTraverse(SqQueue &Q)
{
int i;
i=Q.front;
if(QueueLength(Q)==0) return ERROR;
else{
printf("The Queue is: ");
while(i!=Q.rear)
{
printf("%d ",Q.base[i]);
i=(i+1)%MAXQSIZE;
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqQueue S;
QElemType x, e;
if(InitQueue(S)) // 判断顺序表是否创建成功,请填空
{
printf("A Queue Has Created.\n");
}
while(1)
{
printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(!EnQueue(S,x)) printf("Enter Error!\n"); // 判断入队是否合法,请填空
else printf("The Element %d is Successfully Entered!\n", x);
break;
case 2: if(!DeQueue(S,e)) printf("Delete Error!\n"); // 判断出队是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: if(!GetHead(S,e))printf("Get Head Error!\n"); // 判断Get Head是否合法,请填空
else printf("The Head of the Queue is %d!\n", e);
break;
case 4: printf("The Length of the Queue is %d!\n",QueueLength(S)); //请填空
break;
case 5: QueueTraverse(S); //请填空
break;
case 0: return 1;
}
}
}
//8585 栈的应用——进制转换
int main()
{
SqStack s;
int n;
scanf("%d",&n);
InitStack(s);
while(n>0)
{
Push(s,n%8);
n/=8;
}
StackTraverse(s);
}
//8586 括号匹配检验
#include<stdio.h>
#include<math.h>
#include<stdlib.h> // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
typedef char SElemType;
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
S.base =(SElemType*)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!S.base ) return FALSE;
S.top =S.base ;
S.stacksize =STACK_INIT_SIZE;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.base ==S.top ) return 1;
else return 0;
}
Status Push(SqStack &S,SElemType e)
{
if(S.top -S.base >=S.stacksize )
{
S.base =(SElemType*)realloc(S.base ,(S.stacksize +STACKINCREMENT)*sizeof(SElemType));
if(!S.base ) return FALSE;
S.top =S.base +S.stacksize ;
S.stacksize +=STACKINCREMENT;
}
*(S.top )=e;
S.top ++;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.base ==S.top )
return FALSE;
--S.top ;
e=*(S.top );
return OK;
}
void check()
{
// 对于输入的任意一个字符串,检验括号是否配对
SqStack s;
SElemType ch[80],*p,e;
if(InitStack(s)) // 初始化栈成功
{
//printf("请输入表达式\n");
gets(ch);
p=ch;
while(*p) // 没到串尾
switch(*p)
{
case '(':
case '[':Push(s,*p++);
break; // 左括号入栈,且p++
case ')':
case ']':if(!StackEmpty(s)) // 栈不空
{
Pop(s,e); // 弹出栈顶元素
if(*p==')'&&e!='('||*p==']'&&e!='[')
// 弹出的栈顶元素与*p不配对
{
printf("isn't matched pairs\n");
exit(ERROR);
}
else
{
p++;
break; // 跳出switch语句
}
}
else // 栈空
{
printf("lack of left parenthesis\n");
exit(ERROR);
}
default: p++;; // 其它字符不处理,指针向后移
}
if(StackEmpty(s)) //字符串结束时栈空
printf("matching\n");
else
printf("lack of right parenthesis\n");
}
}
int main()
{
check();
}
//行编辑程序
typedef char SElemType;
#include"cstdlib"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 30 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{ // 构造一个空栈S
// 请补全代码
S.base=(SElemType*)malloc(STACK_INIT_SIZE* sizeof(SElemType));
if(!S.base)
return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status ClearStack(SqStack &S)
{ // 把S置为空栈
S.top=S.base;
return OK;
}
Status DestroyStack(SqStack &S)
{ // 销毁栈S,S不再存在
free(S.base);
S.base=NULL;
S.top=NULL;
S.stacksize=0;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)malloc((STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
return ERROR;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top=e;
S.top++;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
if(S.top==S.base)return ERROR;
e=*(S.top-1);
S.top--;
return OK;
}
Status StackTraverse(SqStack S,Status(*visit)(SElemType))
{ // 从栈底到栈顶依次对栈中每个元素调用函数visit()。
// 一旦visit()失败,则操作失败
while(S.top>S.base)
visit(*S.base++);
printf("\n");
return OK;
}
Status visit(SElemType c)
{
printf("%c",c);
return OK;
}
void LineEdit()
{ // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2
//2
//
//defne##ine OK 1
//
//typp cila@type int element
SqStack s;
char ch,c;
int n,i;
InitStack(s);
scanf("%d",&n);
ch=getchar();//读换行
for(i=1;i<=n;i++)
{ ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '#':Pop(s,c);
break; // 仅当栈非空时退栈
case '@':ClearStack(s);
break; // 重置s为空栈
default :Push(s,ch); // 有效字符进栈
}
ch=getchar(); // 从终端接收下一个字符
}
StackTraverse(s,visit); // 将从栈底到栈顶的栈内字符输出
ClearStack(s); // 重置s为空栈
}
DestroyStack(s);
}
int main()
{
LineEdit();
}
//18938 汉诺塔问题
#include <stdio.h>
int m;
// 将 n 个盘子从 a借助 c 移动到b
void mmove(int n, char a, char b, char c)
{
if( 1 == n )
{
printf("%c->%d->%c\n", a,n,b);
}
else
{
mmove(n-1, a, c, b); // 将 n-1 个盘子从 a 借助 b 移到 c 上
printf("%c->%d->%c\n", a, n,b); // 将 第 n 个盘子从 a 移到 b 上
mmove(n-1, c, b,a); // 将 n-1 个盘子从 c借助 a 移到 b 上
}
}
int main()
{
int n;
char a,b,c;
scanf("%d %c %c %c", &n,&a,&b,&c);
mmove(n, a, b, c);
return 0;
}
//18937 阿克曼(Ackmann)函数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int akm(int m, int n)
{
if (m > 0 && n > 0)
{
return akm(m - 1, akm(m, n - 1));
}
else if (m > 0 && n == 0)
{
return akm(m - 1, 1);
}
else if (m == 0)
{
return n + 1;
}
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("%d", akm(m, n));
return 0;
}