栈是一种特殊的线性表,在固定的一端进行插入和删除,这端也成为栈顶,而不能进行插入删除的一端叫做栈底。
- typedef int STDatatype;
- typedef struct Stack
- {
- STDatatype* a;
- int capacity;
- int top;
- }ST;
- void StackInit(ST* ps)
- {
- //不初始化空间
- /*ps->a = NULL;
- ps->top = 0;
- ps->capacity = 0;*/
- //初始化空间
- ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
- if (ps->a == NULL)
- {
- perror("malloc");
- exit(-1);
- }
- ps->top = 0;
- ps->capacity = 4;
- }
入栈,往栈里存放数据。
- static void check_capacity(ST* ps)
- {
- if (ps->top == ps->capacity)
- {
- int newCapacity = ps->capacity * 2;
- STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
- if (tmp == NULL)
- {
- perror("realloc");
- exit(-1);
- }
- else
- {
- ps->a = tmp;
- ps->capacity == newCapacity;
- }
- }
- }
- void StackPush(ST* ps, STDatatype x)
- {
- assert(ps);
- check_capacity(ps);
- ps->a[ps->top] = x;
- ps->top++;//指向下一个
- }
- void StackDestroy(ST* ps)
- {
- assert(ps);
-
- free(ps->a);
- ps->a = NULL;
- ps->top = ps->capacity = 0;
- }
- bool StackEmpty(ST* ps)//判空
- {
- assert(ps);
- return ps->top == 0;
- }
出栈,删除栈顶元素。
- void StackPop(ST* ps)//出栈
- {
- assert(ps);
- //if(ps->top>0)
- assert(!StackEmpty);
- ps->top--;
- }
- STDatatype StackTop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty);
- return ps->a[ps->top-1];//top为最后一个元素的下一个
- }
返回栈的元素个数。
- int StackSize(ST* ps)
- {
- assert(ps);
- return ps->top;
- }
有的人会说,可以直接用ps.top来得到栈的个数,为什么还要封装,答案是封装可以不关注实现,我们这里是的top指向的是当前元素的下一个位置,那如果我从-1开始(没有元素),得到的top也不一样,需要注意这点。
- StackSize(&ps);
- //ps.top
- //stack.cpp
- #include "Stack.h"
- void StackInit(ST* ps)
- {
- /*ps->a = NULL;
- ps->top = 0;
- ps->capacity = 0;*/
- //初始化空间
- ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
- if (ps->a == NULL)
- {
- perror("malloc");
- exit(-1);
- }
- ps->top = 0;
- ps->capacity = 4;
- }
-
- static void check_capacity(ST* ps)
- {
- if (ps->top == ps->capacity)
- {
- int newCapacity = ps->capacity * 2;
- STDatatype* tmp = (STDatatype*)realloc(ps->a, newCapacity *sizeof(STDatatype));
- if (tmp == NULL)
- {
- perror("realloc");
- exit(-1);
- }
- else
- {
- ps->a = tmp;
- ps->capacity == newCapacity;
- }
- }
- }
- void StackPush(ST* ps, STDatatype x)
- {
- assert(ps);
- check_capacity(ps);
- ps->a[ps->top] = x;
- ps->top++;//指向下一个
- }
- void StackPop(ST* ps)//出栈
- {
- assert(ps);
- //if(ps->top>0)
- assert(!StackEmpty(ps));
- ps->top--;
- }
-
- STDatatype StackTop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
- return ps->a[ps->top-1];//top为最后一个元素的下一个
- }
- void StackDestroy(ST* ps)
- {
- assert(ps);
-
- free(ps->a);
- ps->a = NULL;
- ps->top = ps->capacity = 0;
- }
- bool StackEmpty(ST* ps)//判空
- {
- assert(ps);
- return ps->top == 0;
- }
- int StackSize(ST* ps)
- {
- assert(ps);
- return ps->top;
- }
- //stack.h
- #pragma once
- #include
- #include
- #include
- #include
-
- typedef int STDatatype;
- typedef struct Stack
- {
- STDatatype* a;
- int capacity;
- int top;
- }ST;
-
- void StackInit(ST* ps);
- void StackDestroy(ST* ps);
- void StackPush(ST* ps, STDatatype x);
- void StackPop(ST* ps);//出栈
- STDatatype StackTop(ST* ps);
-
- bool StackEmpty(ST* ps);
- int StackSize(ST* ps);
- //test.cpp
- #include "Stack.h"
- void TestStack1()
- {
- ST ps;
- StackInit(&ps);
- StackPush(&ps, 1);
- StackPush(&ps, 1);
- StackPush(&ps, 1);
- StackPush(&ps, 1);
- StackPush(&ps, 1);
- StackDestroy(&ps);
- }
- void TestStack2()
- {
- ST ps;
- StackInit(&ps);
- StackPush(&ps, 2);
- printf("%d",StackTop(&ps));
- StackPop(&ps);
- StackSize(&ps);
- //ps.top
- StackDestroy(&ps);
-
- }
-
- int main()
- {
- //TestStack1();
- TestStack2();
-
- return 0;
- }