一种是结构体中直接定义数组,一种我们动态开辟内存。
typedef int ElemType;
typedef struct Stack
{
ElemType data[STACK_INIT_SIZE];
int top;
}Stack,*Sxtack;
void Init_Stack(Sxtack pstack)
{
assert(pstack != nullptr);
pstack->top = 0;
}
bool IsFull(Sxtack pstack)
{
assert(pstack != nullptr);
return pstack->top == STACK_INIT_SIZE;
}
bool Push(Sxtack pstack, ElemType val)
{
assert(pstack != nullptr);
if (IsFull(pstack))
{
printf("Push error\n");
}
else
{
pstack->data[pstack->top] = val;
pstack->top++;
}
return true;
}
bool IsEmpty(Sxtack pstack)
{
assert(pstack != nullptr);
return pstack->top == 0;
}
bool Pop(Sxtack pstack, ElemType &val)
{
assert(pstack != nullptr);
if (IsEmpty(pstack))
{
printf("Pop error\n");
}
else
{
val = pstack->data[pstack->top - 1];
pstack->top--;
}
return true;
}
bool GetTop_Elem(Sxtack pstack, ElemType &val)
{
assert(pstack != nullptr);
if (IsEmpty(pstack)) return false;
val = pstack->data[pstack->top - 1];
return true;
}
void Print_Stack(Sxtack pstack)
{
assert(pstack != nullptr);
int n = 0;
while (!(IsEmpty(pstack)))
{
Pop(pstack, n);
printf("%3d",n);
}
printf("\n");
}
可以根据个人喜好看用哪种方式,数组的话,如果不够用直接把栈的初始值改一下就好,ElemType data[STACK_INIT_SIZE],即方括号里的大小,指针的话就不用管了。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define STACK_INIT_SIZE 10
typedef int ElemType;
typedef struct Stack
{
ElemType* elem;
int top;
int stacksize;
}Stack, * SLstack;
void Init_Stack(SLstack pstack)
{
assert(pstack != nullptr);
pstack->stacksize = STACK_INIT_SIZE;
pstack->top = 0;
pstack->elem = (ElemType*)malloc(sizeof(ElemType) * pstack->stacksize);
}
bool IsFull(SLstack pstack)
{
assert(pstack != nullptr);
return pstack->top == pstack->stacksize;
}
bool IsEmpty(SLstack pstack)
{
assert(pstack != nullptr);
return pstack->top == 0;
}
void Dilatation(SLstack pstack)
{
assert(pstack != nullptr);
ElemType* p = (ElemType*)realloc(pstack->elem, sizeof(ElemType) * (pstack->stacksize + STACKINCREMENT));
if (nullptr == p) exit(1);
pstack->elem = p;
pstack->stacksize += STACKINCREMENT;
}
bool Push(SLstack pstack, ElemType& val)
{
assert(pstack != nullptr);
if (IsFull(pstack))
{
Dilatation(pstack);
}
pstack->elem[pstack->top] = val;
pstack->top++;
return true;
}
bool GetTop(SLstack pstack, ElemType& val)
{
assert(pstack != nullptr);
if (IsEmpty(pstack)) return false;
val = pstack->elem[pstack->top - 1];
return true;
}
bool Pop(SLstack pstack, ElemType& val)
{
assert(pstack != nullptr);
if (IsEmpty(pstack)) return false;
val = pstack->elem[pstack->top - 1];
pstack->top--;
return true;
}
void Print_stack(SLstack pstack)
{
assert(pstack != nullptr);
ElemType top = pstack->top;
//while (!(IsEmpty(pstack)))
while (top != 0)
{
printf("%3d", pstack->elem[top - 1]);
top--;
}
/*while (!(IsEmpty(pstack)))
{
Pop(pstack, val);
printf("%3d", val);
}*/
printf("\n");
}
int Length(SLstack pstack)
{
assert(pstack != nullptr);
return pstack->top;
}
void Clear_Stack(SLstack pstack)
{
assert(pstack != nullptr);
pstack->top = 0;
}
void Destroy(SLstack pstack)
{
assert(pstack != nullptr);
free(pstack->elem);
//memset(pstack,0,12);
Clear_Stack(pstack);
pstack->elem = nullptr;
pstack->stacksize = 0;
}
int main()
{
Stack mystack;
Init_Stack(&mystack);
for (int i = 0; i < 15; ++i)
{
Push(&mystack, i);
}
Print_stack(&mystack);
printf("%3d", Length(&mystack));
Clear_Stack(&mystack);
Print_stack(&mystack);
printf("%3d", Length(&mystack));
Destroy(&mystack);
Print_stack(&mystack);
return 0;
}