#ifndef STRACK
#define STRACK
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int ElemType;
#define INCREASEMENT_SIZE 10
typedef struct STRACKNODE{
ElemType* top;
ElemType* base;
int count;
}Strack, *StrackPtr;
StrackPtr InitStrack();//初始化栈
void PushStrack(StrackPtr s, ElemType e);//把元素压入栈
bool StrackEmpty();//判断栈是否为空
void StrackClear(StrackPtr s);//清空栈
void StrackFree(StrackPtr s);//释放栈
void GetTop(StrackPtr s, ElemType* e);//得到栈顶元素,并将其赋值给e
int StrackLength(StrackPtr s);//栈的长度
void Pop(StrackPtr s, ElemType* e);//删除栈顶元素,并将其赋值给e
void Print(StrackPtr s); //打印元素
#endif
//
#include "strack.h"
int INITSIZE = 100;
StrackPtr InitStrack()
{
StrackPtr s = (StrackPtr)malloc(sizeof(Strack));
s->top = s->base = (ElemType*)malloc(sizeof(ElemType)*INITSIZE);
s->count = 0;
return s;
}
void PushStrack(StrackPtr s, ElemType e)
{
if (s->top - s->base >= INITSIZE)
{
s->base = (ElemType*)realloc(s->base, (INITSIZE + INCREASEMENT_SIZE)*sizeof(ElemType));
s->top = s->base + INITSIZE;
INITSIZE += INCREASEMENT_SIZE;
}
*(s->top)++ = e;
s->count++;
}
bool StrackEmpty(StrackPtr s)
{
if (s->base == s->top)
{
return true;
}
else
{
return false;
}
}
void StrackClear(StrackPtr s)
{
if (s->base == s->top)
{
return;
}
s->count = 0;
s->top = s->base;
}
void StrackFree(StrackPtr s)
{
if (s->base == NULL)
{
return;
}
free(s->base);
}
void GetTop(StrackPtr s, ElemType* e)
{
*e = *(s->top - 1);
}
int StrackLength(StrackPtr s)
{
return s->count;
}
void Pop(StrackPtr s, ElemType* e)
{
if (s->top == s->base)
{
return;
}
s->count--;
s->top--;
*e = *(s->top);
}
void Print(StrackPtr s)
{
for (int i = 1; i <= s->count; i++)
{
printf("%d ", *(s->top - i));
}
}
///
#include "strack.h"
int main()
{
int a;
StrackPtr s = InitStrack();
for (int i = 0; i < 10; i++)
{
PushStrack(s, i);
}
Print(s);
printf("%d ", StrackLength(s));
Pop(s, &a);
printf("%d ", a);
Print(s);
StrackClear(s);
printf("%d ", StrackLength(s));
StrackFree(s);
system("pause");
return 0;
//进制数转换
/*StrackPtr s = InitStrack();
printf("请输入你要转换的十进制整数");
int a;
scanf("%d", &a);
printf("你要转换为几进制数");
int b;
scanf("%d", &b);
while (a)
{
PushStrack(s, a % b);
a /= b;
}
Print(s);
system("pause");
return 0;*/
}
//