一、关键点
1.指针变量取值或赋值
指针变量取值或赋值时如果有其他一元运算符
注意加上括号
(*p)--//表示 p指针指向的地址的内容--
*p-- //表示p指针指向的地址--
二. 代码
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
typedef int SElemType_Sq;
typedef int Status;
typedef struct{
SElemType_Sq *base;
SElemType_Sq *top;
int stacksize;
}SqStack;
void PrintElem(SElemType_Sq e);
Status InitStack_Sq(SqStack *S);
Status DestroyStack_Sq(SqStack *S);
Status ClearStack_Sq(SqStack *S);
Status StackEmpty_Sq(SqStack S);
int StackLength_Sq(SqStack S);
Status GetTop_Sq(SqStack S, SElemType_Sq *e);
Status Push_Sq(SqStack *S, SElemType_Sq e);
Status Pop_Sq(SqStack *S, SElemType_Sq *e);
Status StackTraverse_Sq(SqStack S, void(Visit)(SElemType_Sq));
void PrintElem(SElemType_Sq e){
printf("%d ", e);
}
Status InitStack_Sq(SqStack *S){
S->base = (SElemType_Sq *)malloc(STACK_INIT_SIZE*sizeof(SElemType_Sq));
if(!S->base){
exit(OVERFLOW);
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status DestroyStack_Sq(SqStack *S){
free(S->base);
S->base = NULL;
S->top = NULL;
S->stacksize = 0;
return OK;
}
Status ClearStack_Sq(SqStack *S){
S->top = S->base;
return OK;
}
Status StackEmpty_Sq(SqStack S){
return S.base == S.top?TRUE:FALSE;
}
int StackLength_Sq(SqStack S){
return S.top-S.base;
}
Status GetTop_Sq(SqStack S, SElemType_Sq *e){
if(S.top==S.base){
return ERROR;
}
*e = *(S.top-1);
return OK;
}
Status Push_Sq(SqStack *S, SElemType_Sq e){
if(S->top-S->base>=S->stacksize){
SElemType_Sq *p;
p = (SElemType_Sq*)realloc(S->base,(S->stacksize+STACK_INCREMENT)*sizeof(SElemType_Sq));
if(!p){
exit(OVERFLOW);
}
S->base = p;
S->top = S->base+S->stacksize;
S->stacksize+=STACK_INCREMENT;
}
*(S->top) = e;
S->top++;
return OK;
}
Status Pop_Sq(SqStack *S, SElemType_Sq *e){
if(S->top==S->base){
return ERROR;
}
S->top--;
*e = *(S->top);
return OK;
}
Status StackTraverse_Sq(SqStack S, void(Visit)(SElemType_Sq)){
SElemType_Sq *p = S.top-1;
while(p>=S.base){
Visit(*p--);
}
return OK;
}
int main(){
SqStack S;
int i;
SElemType_Sq e;
printf("▼1\n▲函数 InitStack 测试...\n"); //1.函数InitStack测试
{
printf("初始化顺序栈 S ...\n");
InitStack_Sq(&S);
printf("\n");
}
printf("▼4\n▲函数 StackEmpty 测试...\n"); //4.函数StackEmpty测试
{
StackEmpty_Sq(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
printf("\n");
}
printf("▼7\n▲函数 Push 测试...\n"); //7.函数Push测试
{
for(i=1; i<=6; i++)
{
printf("将 \"%2d\" 压入栈 S ", 2*i);
Push_Sq(&S, 2*i);
printf("(累计第 %d 个元素)...\n", S.top-S.base);
}
printf("\n");
}
printf("▼9\n▲函数 StackTraverse 测试...\n"); //9.函数StackTraverse测试
{
printf(" S 中的元素为:S = ");
StackTraverse_Sq(S, PrintElem);
printf("\n\n");
}
printf("▼8\n▲函数 Pop 测试...\n"); //8.函数Pop测试
{
Pop_Sq(&S, &e);
printf("栈顶元素 \"%d\" 出栈...\n", e);
printf(" S 中的元素为:S = ");
StackTraverse_Sq(S, PrintElem);
printf("\n\n");
}
printf("▼5\n▲函数 StackLength 测试...\n"); //5.函数StackLength测试
{
i = StackLength_Sq(S);
printf(" S 的长度为 %d \n", i);
printf("\n");
}
printf("▼6\n▲函数 GetTop 测试...\n"); //6.函数GetTop测试
{
GetTop_Sq(S, &e);
printf("栈顶元素的值为 \"%d\" \n", e);
printf("\n");
}
printf("▼3\n▲函数 ClearStack 测试...\n"); //3.函数ClearStack测试
{
printf("清空 S 前:");
StackEmpty_Sq(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
ClearStack_Sq(&S);
printf("清空 S 后:");
StackEmpty_Sq(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
printf("\n");
}
printf("▼2\n▲函数 DestroyStack 测试...\n"); //2.函数DestroyStack测试
{
printf("销毁 S 前:");
S.base!=NULL && S.top!=NULL ? printf(" S 存在!\n") : printf(" S 不存在!!\n");
DestroyStack_Sq(&S);
printf("销毁 S 后:");
S.base!=NULL && S.top!=NULL ? printf(" S 存在!\n") : printf(" S 不存在!!\n");
printf("\n");
}
return 0;
}