进出栈
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
//
#include "stdio.h"
#include "malloc.h"
#include "process.h"
#define STACK_INIT_SIZE 100; //初始分配内存
#define STACKINCREAMENT 10; //储存空间分配增量
int snumber = 17112201;
int class22 = 29;
typedef struct sqStack {
int *base; //栈底
int *top; //栈顶
int stacksize;
}sqStack;
sqStack* InitStack() {
sqStack* s;
int stacksize = STACK_INIT_SIZE
s = (sqStack*)malloc(sizeof(sqStack));
s->base = (int*)malloc(stacksize * sizeof(int));
printf("s.base\t%d\n", s->base);
if (!s->base) exit(OVERFLOW);
s->top = s->base;
printf("s.top\t%d\n", s->top);
//s->stacksize = stacksize;
return s;
} //InitStack
int GetTop(sqStack* s) {
return *(s->top-1);
}
void Push(sqStack* s, int data) {
*s->top = data; //将整形data压入栈s,内存向后移4个字节,s.top指向data
s->top++; //将s.top 向后移1个字节
}
int Pop(sqStack* s) { //删除栈顶元素并返回其值
s->top--; //s.top移回到插入最后1个元素之前
return *(s->top);
}
void CheckStack(sqStack* s) {
printf("\n查看栈:\n");
printf("Pop:\n");
printf("s.top\t%d\n", s->top);
printf("……………………\n");
for (int i = 1; i <= class22; i++)
{
printf("s.d\t%d\n", *(s->top - i));
}
//printf("s.d\t%d\n", *(S->top - 1)); //栈顶压着的元素
//printf("s.d\t%d\n", *(S->top - 1 -1)); //之下
printf("……………………\n");
printf("s.base\t%d\n", s->base);
}
int main() {
printf("IninStack:\n");
sqStack* S = InitStack();
//Push(S, 9527); //整形(int)占用内存的长度为4,而不是因为输入的数字是4位数
int snumber = 17112201;
int class22 = 25;
for (int i = snumber; i < snumber+ class22; i++)
{
Push(S, i);
}
printf("Push:\n");
printf("s.base\t%d\n", S->base); //栈底
printf("s.top-1\t%d\n", *(S->top-1)); //栈顶压着的元素
printf("s.top\t%d\n", S->top); //栈顶
printf("GetTop:\n");
int s_top = GetTop(S);
printf("s.top\t%d\n", s_top);
//printf("Pop:\n");
//int pop = Pop(S);
//printf("s.pop \t%d\n", pop);
CheckStack(S);
}