基本概念:
LIFO: Last In First Out 后进先出,先进后出。
栈(stack)是仅限定在表尾进行插入和删除操作的线性表。
栈就是一个线性表,只不过,栈的Insert 和 delete只能在表尾。
栈的基本操作:
这里使用栈的顺序结构:用一组地址连续的空间存储数据
创建栈
typedef int ElementType;
typedef struct Stack
{
ElementType* a;
int top; //栈顶的下一个元素下标
int Capacity; //容量
}ST;
初始化栈
我们一开始先给栈开辟4个ElementType自定义类型的空间。
void StackInit(ST* ps)
{
ps->a = (ElementType*)malloc(sizeof(ElementType)*4);
if (ps->a == NULL)
{
printf("malloc 失败\n");
exit(-1);
}
else
{
ps->Capacity = 4;
ps->top = 0;
}
}
进栈
首先判断栈的空间是否已满?如果没满,直接返回栈顶元素,如果满了,就需要开辟新空间。
void StackPush(ST* ps, ElementType x)
{
if (ps->top == ps->Capacity)
{
ElementType* temp = (ElementType*)realloc(ps->a, ps->Capacity * 2 * sizeof(ElementType));
if (temp == NULL)
{
printf("realloc 失败\n");
exit(-1);
}
else
{
ps->a = temp;
ps->Capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
出栈
void StackPop(ST* ps)
{
assert(ps->top > 0);
ps->top--;
}
取栈顶元素
我们初始化的时候top的意思就是栈顶元素的下一个元素。
ElementType StackTop(ST* ps)
{
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
求数据个数
因为top的含义就是栈顶元素的下一个。
int StackSize(ST* ps)
{
return ps->top;
}
判空
这里我们使用bool判断,如果返回值是1就是空,反之则不为空。
bool StackEmpty(ST* ps)
{
return ps->top == 0;
}
整体代码
StackQueue.h
#pragma once
//数组实现栈的操作
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
typedef int ElementType;
typedef struct Stack
{
ElementType* a;
int top; //栈顶的下一个元素下标
int Capacity; //容量
}ST;
//初始化
void StackInit(ST* ps);
//销毁
void StackDestory(ST* ps);
//进栈
void StackPush(ST* ps, ElementType x);
//出栈
void StackPop(ST* ps);
//取栈顶元素
ElementType StackTop(ST* ps);
//求数据个数
int StackSize(ST* ps);
//判空
bool StackEmpty(ST* ps);
//打印
void Stackprint(ST* ps);
StackQueue.c
#include "StackQueue.h"
//初始化
void StackInit(ST* ps)
{
ps->a = (ElementType*)malloc(sizeof(ElementType)*4);
if (ps->a == NULL)
{
printf("malloc 失败\n");
exit(-1);
}
else
{
ps->Capacity = 4;
ps->top = 0;
}
}
//销毁
void StackDestory(ST* ps)
{
free(ps->a);
ps->a = NULL;
ps->top = ps->Capacity = 0;
}
//进栈
void StackPush(ST* ps, ElementType x)
{
if (ps->top == ps->Capacity)
{
ElementType* temp = (ElementType*)realloc(ps->a, ps->Capacity * 2 * sizeof(ElementType));
if (temp == NULL)
{
printf("realloc 失败\n");
exit(-1);
}
else
{
ps->a = temp;
ps->Capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps->top > 0);
ps->top--;
}
//取栈顶元素
ElementType StackTop(ST* ps)
{
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
//求数据个数
int StackSize(ST* ps)
{
return ps->top;
}
//判空
bool StackEmpty(ST* ps)
{
return ps->top == 0;
}
//打印
void Stackprint(ST* ps)
{
for (int i = ps->top - 1; i > 0; i--)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
test.c
#include "StackQueue.h"
int main()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
StackPush(&st, 5);
StackPush(&st, 6);
Stackprint(&st);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
int len = StackSize(&st);
printf("栈的长度:%d",len);
printf("\n");
bool temp = StackEmpty(&st);
if (temp)
{
printf("栈为空\n");
}
else
{
printf("栈不为空\n");
}
StackDestory(&st);
return 0;
}