一、關(guān)鍵點(diǎn)
1.指針變量取值或賦值
指針變量取值或賦值時(shí)如果有其他一元運(yùn)算符
注意加上括號(hào)
(*p)--//表示 p指針指向的地址的內(nèi)容--
*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▲函數(shù) InitStack 測(cè)試...\n"); //1.函數(shù)InitStack測(cè)試
{
printf("初始化順序棧 S ...\n");
InitStack_Sq(&S);
printf("\n");
}
printf("▼4\n▲函數(shù) StackEmpty 測(cè)試...\n"); //4.函數(shù)StackEmpty測(cè)試
{
StackEmpty_Sq(S) ? printf(" S 為空??!\n") : printf(" S 不為空!\n");
printf("\n");
}
printf("▼7\n▲函數(shù) Push 測(cè)試...\n"); //7.函數(shù)Push測(cè)試
{
for(i=1; i<=6; i++)
{
printf("將 \"%2d\" 壓入棧 S ", 2*i);
Push_Sq(&S, 2*i);
printf("(累計(jì)第 %d 個(gè)元素)...\n", S.top-S.base);
}
printf("\n");
}
printf("▼9\n▲函數(shù) StackTraverse 測(cè)試...\n"); //9.函數(shù)StackTraverse測(cè)試
{
printf(" S 中的元素為:S = ");
StackTraverse_Sq(S, PrintElem);
printf("\n\n");
}
printf("▼8\n▲函數(shù) Pop 測(cè)試...\n"); //8.函數(shù)Pop測(cè)試
{
Pop_Sq(&S, &e);
printf("棧頂元素 \"%d\" 出棧...\n", e);
printf(" S 中的元素為:S = ");
StackTraverse_Sq(S, PrintElem);
printf("\n\n");
}
printf("▼5\n▲函數(shù) StackLength 測(cè)試...\n"); //5.函數(shù)StackLength測(cè)試
{
i = StackLength_Sq(S);
printf(" S 的長(zhǎng)度為 %d \n", i);
printf("\n");
}
printf("▼6\n▲函數(shù) GetTop 測(cè)試...\n"); //6.函數(shù)GetTop測(cè)試
{
GetTop_Sq(S, &e);
printf("棧頂元素的值為 \"%d\" \n", e);
printf("\n");
}
printf("▼3\n▲函數(shù) ClearStack 測(cè)試...\n"); //3.函數(shù)ClearStack測(cè)試
{
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▲函數(shù) DestroyStack 測(cè)試...\n"); //2.函數(shù)DestroyStack測(cè)試
{
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;
}
三、結(jié)果

image.png