?? algo3-1.cpp
字號(hào):
#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("檢驗(yàn)是否為空\(chéng)n");
if(Empty_SqStack(S))
printf("為空\(chéng)n");
else
printf("不為空\(chéng)n");
for(i=0;i<5;i++)
push(S,a[i]);
DisplayStack(S);
printf("檢驗(yàn)是否為空\(chéng)n");
if(Empty_SqStack(S))
printf("為空\(chéng)n");
else
printf("不為空\(chéng)n");
printf("長(zhǎng)度%d\n",Length(S));
printf("從棧頂?shù)綏5椎脑豛n");
for(i=1;i<=5;i++)
printf("%d -- %c\n",S.top-i,*(S.top-i));
printf("出棧序列\(zhòng)n");
for(i=4;i>=0;i--)
{Pop(S,e);
printf("%c\n",e);
}
printf("檢驗(yàn)是否為空\(chéng)n");
if(Empty_SqStack(S))
printf("為空\(chéng)n");
else
printf("不為空\(chéng)n");
//free(&S);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -