📄 algo3-1.cpp
字号:
#include <stdio.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
char InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if (S.base==NULL) return 0;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 1;
}
char Empty_SqStack(SqStack &S)
{ if (S.top==S.base) return 1;
else return 0;
}
char GetTop(SqStack S,SElemType &e)
{
if(S.base==S.top) return 0;
e=*(S.top-1);
return 1;
}
void push(SqStack &S,SElemType e)
{
*(S.top)=e;
S.top++;
}
int Length(SqStack &S)
{
int p;
if (S.base!=S.top)
{
p=S.top-S.base;
}
return(p);
}
char Pop(SqStack &S,SElemType &e)
{
if(S.base==S.top) return 0;
S.top--;
e=*(S.top);
return 1;
}
void DisplayStack(SqStack S)
{
printf("\n-- BASE%c---\n",S.base);
while (S.base!=S.top)
printf("%d -- %c\n",S.base,*(S.base++));
printf("\n-- TOP%c---\n",S.top);
}
void main()
{
SqStack S;
int i;
SElemType e;
SElemType a[6]={'a','b','c','d','e'};
if (InitStack(S))
printf("SUCCESS!\n");
else
printf("ERROR!\n");
printf("检验是否为空\n");
if(Empty_SqStack(S))
printf("为空\n");
else
printf("不为空\n");
for(i=0;i<5;i++)
push(S,a[i]);
DisplayStack(S);
printf("检验是否为空\n");
if(Empty_SqStack(S))
printf("为空\n");
else
printf("不为空\n");
printf("长度%d\n",Length(S));
printf("从栈顶到栈底的元素\n");
for(i=1;i<=5;i++)
printf("%d -- %c\n",S.top-i,*(S.top-i));
printf("出栈序列\n");
for(i=4;i>=0;i--)
{Pop(S,e);
printf("%c\n",e);
}
printf("检验是否为空\n");
if(Empty_SqStack(S))
printf("为空\n");
else
printf("不为空\n");
//free(&S);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -