There is nothing to talk about. In the structure of the sequential stack, the header pointer, the tail pointer, And the stack capacity. When building a stack, you need to find a suitable memory space and point it to the space with the tail pointer in the struct. The header pointer also points to this space. Stack pressure is: first determine whether the space is sufficient? If it is not enough, continue to allocate space. If it is enough, assign a value to the node pointed to by the header pointer and move the first node to the back (that is, the data in the space that the header pointer always points to does not have any value ). Stack play is: first move the header pointer back to a unit, and then return the value at the top of the stack with E.
#include <stdio.h> #include <stdlib.h>#define STACK_INIT_SIZE 100#define STACKINCREAMENT 10#define OVERFLOW -1#define ERROR 0#define OK 1typedef int Status;typedef int ElemType;typedef struct { ElemType *base; ElemType *top; int stacksize;} st;Status InitStack(st &ss){ ss.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if (!ss.base) exit(OVERFLOW); ss.top = ss.base; ss.stacksize = STACK_INIT_SIZE; return OK;}Status GetTop(st &ss, ElemType &e){ if (ss.base == ss.top) return ERROR; e = *(ss.top - 1); return OK;}Status Push(st &ss, ElemType e){ if (ss.top - ss.base >= ss.stacksize) { ss.base = (ElemType*)realloc(ss.base, ss.stacksize + STACKINCREAMENT * sizeof(ElemType)); if (!ss.base) exit(OVERFLOW); ss.top = ss.base + ss.stacksize; ss.stacksize = ss.stacksize + STACKINCREAMENT * sizeof(int); } *ss.top = e; ss.top++; return OK;}Status Pop(st &ss, ElemType &e){ if (ss.top == ss.base) return ERROR; e = *(--ss.top); return OK;}Status DestroyStack(st &ss){ free(ss.base); ss.base = NULL; ss.top = NULL; ss.stacksize = 0; return OK;} Status CleatStack(st &ss){ ss.top = ss.base; ss.stacksize = STACK_INIT_SIZE; return OK;}int StackEmpty(st &ss){ if (ss.top == ss.base) return 1; return 0;}int StackLen(st &ss){ return (ss.top - ss.base);}void print(st &ss){ int m = StackLen(ss); int i; for (i = 0; i < m; i++) { ElemType e; Pop(ss,e); printf("%d ", e); } printf("\n"); return ;}int main(){ st ss; InitStack(ss); int m, i; if(StackEmpty(ss)) printf("empty\n"); printf("how many nums do you want to printf\n"); scanf("%d", &m); for (i = 0; i < m; i++) { int e; scanf("%d", &e); Push(ss, e); } printf("%d\n", StackLen(ss)); print(ss); system ("pause"); return 0; }