#define ERROR 0
#define OVERFLOW -2
#define Stack_init_size 100
#define Stackincrement 10
#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
void InitStack(SqStack *S){
//传入的是指针,我需要的是指针的类型故加星号
int i,n;
SElemType e;
S->base = (SElemType *)malloc(Stack_init_size*sizeof(SElemType));
if(!S->base)
return ;
S->top = S->base;//?????
S->stacksize = Stack_init_size;
}
void Push(SqStack *S,SElemType e){
if(S->top - S->base>=S->stacksize){
S->base = (SElemType *)realloc(S->base
,(S->stacksize+Stackincrement)*sizeof(SElemType));
//为什么不是S->top??????
S->top = S->base+S->stacksize;//1
S->stacksize = S->stacksize+Stackincrement;
//2:为什么1和2的位置不对换呢????
}
*S->top = e;
S->top ++;
}
void Pop(SqStack *S){
SElemType e;
if(S->top==S->base)
return ;
S->top--;
e=*S->top;
printf("要输出的元素:%d\n",e);
}
void main(){
SElemType e;
SqStack S;
InitStack(&S);
Push(&S,3);
Push(&S,4);
Push(&S,5);
Push(&S,6);
Pop(&S);
Pop(&S);
Pop(&S);
Pop(&S);
}
栈的基本操作-指针
最新推荐文章于 2022-08-16 14:44:15 发布