69,368
社区成员
发帖
与我相关
我的任务
分享
#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;
void InitStack(LPSqStack S)
{
S = (LPSqStack)malloc(sizeof(SqStack));
S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S->base)
{
printf("Memory allocation failure!");
exit(OVERFLOW);
}
//S->base = S->top;//???
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
}
int main()
{
LPSqStack S;
int i = 0, count = 0;
float fElem;
InitStack(S);
//.....
return 0;
}
初始化只需要这么做。