#include<malloc.h>#include<stdio.h>#defineOK1#defineERROR0#defineSTACK_INIT_SIZE100// 存储空间初始分配量#defineSTACKINCREMENT10// 存储空间分配增量typedefint SElemType;// 定义栈元素类型typedefint Status;// Status是函数的类型,其值是函数结果状态代码,如OK等structSqStack{
SElemType* base;// 在栈构造之前和销毁之后,base的值为NULL
SElemType* top;// 栈顶指针int stacksize;// 当前已分配的存储空间,以元素为单位};// 顺序栈
Status InitStack(SqStack& S){// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE// 请补全代码
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 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.base == S.top)return ERROR;
e =*(S.top-1);
S.top--;return OK;}
Status GetTop(SqStack S, SElemType& e){// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR// 请补全代码if(S.base == S.top)return ERROR;
e =*(S.top-1);return OK;}intStackLength(SqStack S){// 返回栈S的元素个数// 请补全代码if(S.base == S.top)return ERROR;return(int)(S.top - S.base);}
Status StackTraverse(SqStack S){// 从栈顶到栈底依次输出栈中的每个元素
SElemType* p =(SElemType*)malloc(sizeof(SElemType));
p = S.top;//请填空if(S.base==S.top)printf("The Stack is Empty!");//请填空else{printf("The Stack is: ");
p--;while(p>=S.base)//请填空{//请填空printf("%d ",*p);
p--;}}printf("\n");return OK;}intmain(){int a;
SqStack S;
SElemType x, e;if(InitStack(S))// 判断顺序表是否创建成功,请填空{printf("A Stack Has Created.\n");}while(1){printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");scanf("%d",&a);switch(a){case1:scanf("%d",&x);if(!Push(S,x))printf("Push Error!\n");// 判断Push是否合法,请填空elseprintf("The Element %d is Successfully Pushed!\n", x);break;case2:if(!Pop(S,e))printf("Pop Error!\n");// 判断Pop是否合法,请填空elseprintf("The Element %d is Successfully Poped!\n", e);break;case3:if(!GetTop(S,e))printf("Get Top Error!\n");// 判断Get Top是否合法,请填空elseprintf("The Top Element is %d!\n", e);break;case4:printf("The Length of the Stack is %d!\n",StackLength(S));//请填空break;case5:StackTraverse(S);//请填空break;case0:return1;}}}
循环队列的基本操作
#include<malloc.h>#include<stdio.h>#defineOK1#defineERROR0typedefint Status;// Status是函数的类型,其值是函数结果状态代码,如OK等typedefint QElemType;#defineMAXQSIZE100// 最大队列长度(对于循环队列,最大队列长度要减1)typedefstruct{
QElemType* base;// 初始化的动态分配存储空间int front;// 头指针,若队列不空,指向队列头元素int rear;// 尾指针,若队列不空,指向队列尾元素的下一个位置}SqQueue;
Status InitQueue(SqQueue& Q){// 构造一个空队列Q,该队列预定义大小为MAXQSIZE// 请补全代码
Q.base =new QElemType[MAXQSIZE];if(!Q.base)return ERROR;
Q.front = Q.rear =0;return OK;}
Status EnQueue(SqQueue& Q, QElemType e){// 插入元素e为Q的新的队尾元素// 请补全代码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){// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR// 请补全代码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){// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR// 请补全代码if(Q.front == Q.rear)return ERROR;
e = Q.base[Q.front];return OK;}intQueueLength(SqQueue Q){// 返回Q的元素个数// 请补全代码return(Q.rear - Q.front + MAXQSIZE)% MAXQSIZE;}
Status QueueTraverse(SqQueue Q){// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.int i;
i = Q.front;if(QueueLength(Q)==0)printf("The Queue is Empty!");//请填空else{printf("The Queue is: ");while(i!=Q.rear)//请填空{printf("%d ", Q.base[i]);//请填空
i =(i+1)%MAXQSIZE;//请填空}}printf("\n");return OK;}intmain(){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){case1:scanf("%d",&x);if(!EnQueue(S,x))printf("Enter Error!\n");// 判断入队是否合法,请填空elseprintf("The Element %d is Successfully Entered!\n", x);break;case2:if(!DeQueue(S,e))printf("Delete Error!\n");// 判断出队是否合法,请填空elseprintf("The Element %d is Successfully Deleted!\n", e);break;case3:if(!GetHead(S,e))printf("Get Head Error!\n");// 判断Get Head是否合法,请填空elseprintf("The Head of the Queue is %d!\n", e);break;case4:printf("The Length of the Queue is %d!\n",QueueLength(S));//请填空break;case5:QueueTraverse(S);//请填空break;case0:return1;}}}
#include<iostream>usingnamespace std;voidprintStep(int num,char x,char y){
cout << x <<"->"<< num <<"->"<< y <<"\n";}voidStep(int n,char a,char c,char b){if(n ==1)printStep(n, a, c);else{Step(n -1, a, b, c);printStep(n, a, c);Step(n -1, b, c, a);}}intmain(){int num;char a, c, b;scanf("%d %c %c %c",&num,&a,&c,&b);Step(num, a, c, b);return0;}