version1.c#include <stdio.h>
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
} sqStack;
void InitStack(sqStack *stack, ElemType *begin, ElemType *end);
void Push(sqStack *stack, ElemType e);
void Pop(sqStack *stack, ElemType *e);
int main()
{
ElemType buff[100];
sqStack stack;
ElemType e;
InitStack(&stack, buff, buff + (sizeof(buff) / sizeof(buff[0])));
for(int i = 0; i < 10; ++i)
{
Push(&stack, i);
}
for(int i = 0; i < 10; ++i)
{
Pop(&stack, &e);
printf("%d ", e);
}
printf("\n");
return 0;
}
void InitStack(sqStack *stack, ElemType *begin, ElemType *end)
{
stack->base = begin;
stack->top = end;
}
void Push(sqStack *stack, ElemType e)
{
*--stack->top = e;
}
void Pop(sqStack *stack, ElemType *e)
{
*e = *stack->top++;
}
version2.c#include <stdio.h>
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
} sqStack;
void InitStack(sqStack *stack, ElemType *begin, ElemType *end);
void Push(sqStack *stack, ElemType e);
void Pop(sqStack *stack, ElemType *e);
int main()
{
ElemType buff[100];
sqStack stack;
ElemType e;
InitStack(&stack, buff, buff + (sizeof(buff) / sizeof(buff[0])));
for(int i = 0; i < 10; ++i)
{
Push(&stack, i);
}
for(int i = 0; i < 10; ++i)
{
Pop(&stack, &e);
printf("%d ", e);
}
printf("\n");
return 0;
}
void InitStack(sqStack *stack, ElemType *begin, ElemType *end)
{
stack->base = begin;
stack->top = end - 1;
}
void Push(sqStack *stack, ElemType e)
{
*stack->top-- = e;
}
void Pop(sqStack *stack, ElemType *e)
{
*e = *++stack->top;
}
|