栈的实现

include <stdio.h>

define MAXSIZE 10

typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

void InitSqStack(SqStack *sq){
int i;
sq->base=(SqStack ) malloc(MAXSIZEsizeof(SqStack));
if(!sq->base){
printf("init fail \n");
exit(0);
}
sq->top=sq->base;
printf("top%p\n",sq->top);
printf("base%p\n",sq->base);
sq->stacksize=MAXSIZE;
printf("init success \n");
}

void push(SqStack *sq,SElemType e ){
int i;
if(sq->top-sq->base>= MAXSIZE){

}
printf("e%p\n",&e);
//sq->top[0]=e;
*(sq->top)=e;
printf("top%p\n",sq->top);
sq->top++;
i=sq->top-sq->base;
printf("top%p\n",sq->top);
printf("base%p\n",sq->base);
printf("top-base=%d\n",i);

}

void pop(SqStack *sq,SElemType * e ){
int i=4;
sq->top--;
printf("%d ",sq->top[0]);
printf("top%p\n",sq->top);
*e=sq->top[0];
printf("e%p\n",e);

}

void vist(SqStack sq){
int i=sq.top-sq.base;
int j=i-1;
printf("%d\n",i);
printf("(");
while(i>0){
printf("%d ",sq.base[j]);
i--;
j--;
}
printf(")\n");
}

int main(){
SqStack sq;
int p;
InitSqStack(&sq);
vist(sq);
push(&sq,1);
push(&sq,2);
push(&sq,3);
push(&sq,4);
printf("p%p\n",p);
pop(&sq,&p);
printf("p%p\n",p);
vist(sq);
printf("p=%d\n",p);

return 0;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • include <stdio.h> include <stdlib.h> define STACK_INIT_SI...
    Drs莫思归阅读 442评论 0 0
  • * 构造空栈 * 取栈顶元素 * 取栈底元素 * 求栈的长度 * 判断是否栈满 * 栈的扩容 * 判断...
    虎太郎丨C阅读 963评论 0 0
  • /** * @author huihut * @E-mail:huihut@outlook.com * @vers...
    刘帆_d384阅读 500评论 0 0
  • 写给兄弟 编辑 只看楼主 删除 发言 楼主 炳然 2010-03-16 18:18:06 那晚深夜接到电话,说你已...
    小然007阅读 199评论 0 0