关于栈的问题
我的目的是将逐个浮点数压入栈 然后每压入一个数就将栈中的全部元素打印出来
代码如下:
#define OK 0x0001#define ERROR 0x0000#define TRUE 0x0001#define FALSE 0x0000#define INFEASIBLE 0xFFFF#define STACK_INIT_SIZE 10#define STACKINCREMENT 10#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <malloc.h>typedef int Status;typedef float SElemType;typedef struct{ SElemType *base; SElemType *top; Status stacksize;}SqStack;void InitStack(SqStack S){ S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (!S.base) { printf("Memory allocation failure!"); exit(OVERFLOW); } S.base = S.top; S.stacksize = STACK_INIT_SIZE;}Status GetTop(SqStack S, SElemType *e){ if (S.base == S.top) return (ERROR); *e = *(S.top - 1); return (OK);}Status Push(SqStack S, SElemType e){ if (S.top - S.base == S.stacksize) // Stack fulls, Add storage space { S.base = (SElemType *) realloc (S.base, (S.stacksize + STACKINCREMENT * sizeof(SElemType))); S.top = S.stacksize + S.base; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK;}Status Pop(SqStack S, SElemType *e){ if (S.base == S.top) { printf("The Stack is Empty!"); return ERROR; } *e = *--S.top; return OK;}int main(){ SqStack S; int i = 0, count = 0; float fElem; InitStack(S); while (1) { scanf("%f", &fElem); Push(S, fElem); count++; // for (i = 0; i < count; i++) printf("%d\n", count); printf("%f\n", *(S.top - 1)); } return 0;}typedef struct SqStack_t{ SElemType *base; SElemType *top; Status stacksize;}SqStack, *LPSqStack;