栈作为线性数据结构中的线性表,最重要的特性是先进后出,或者说是后进先出,因此它又叫做先进后出表
栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈,删除则称为出栈
数据结构中的“栈”是一个概念,是逻辑存在的。计算机操作系统中的栈是指一块内存区域,该区域的管理(内存空间的分配与回收)采用类似数据结构中“栈”的特点进行操作。 操作系统中的栈是物理存在的。 无论哪一种栈,都遵循“后进先出”的特点。---这就是为什么他们名称相似的原因
Stack.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
//动态栈
typedef int StackType;
typedef struct Stack
{
int* a;
int top;//记录栈顶的位置
int capacity;//容量
}Stack;
//接口
void StackInit(Stack* ps);
void StackDestory(Stack* ps);
void StackPush(Stack* ps,int x);//栈顶插入
void StackPop(Stack* ps);//栈顶删除
bool StackEmpty(Stack* ps);//判断栈是否为空
int StackTop(Stack* ps);//取出栈顶元素
Stacktest.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
int main()
{
Stack stack;
StackInit(&stack);//---对栈修改需要取地址
//对栈的访问是要付出代价的---根据栈的定义来证明
StackPush(&stack, 1);
StackPush(&stack, 2);
StackPush(&stack, 3);
StackPush(&stack, 4);
//后进先出
while (!StackEmpty(&stack))
{
printf("%d ", StackTop(&stack));
StackPop(&stack);
}
printf("\n");
StackDestory(&stack);
return 0;
}
Stack.c
//这里有两种情况
//top是指向最后一个元素之后的空空间
//top是指向最后一个元素的位置,这样就会使得在插入数据的时候需要先移动
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
//void StackInit(Stack* ps)
//{
// assert(ps);
// //将栈初始化为0;
// ps->a = NULL;
// ps->top = 0;
// ps->capacity = 0;
//}
//void StackDestory(Stack* ps)
//{
// assert(ps);
// free(ps->a);
// ps->a = NULL;
// ps->capacity = ps->top = 0;
//}
//void StackPush(Stack* ps, int x)//栈顶插入
//{
// //分两种情况
// //第1种是先放数据后++,此时top初始化为0;
// //第2种是先++后放数据,此时top初始化为-1;
// assert(ps);
// //是否扩容
// if (ps->capacity == ps->top)
// {
// int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
// ps->a = realloc(ps->a, newcapacity * sizeof(int));
// if (ps->a==NULL)
// {
// printf("扩容失败\n");
// return;
// }
// ps->capacity = newcapacity;
// }
// ps->a[ps->top++] = x;
//}
//void StackPop(Stack* ps)//栈顶删除
//{
// assert(ps);
// assert(ps->top > 0);//top代表最后一个元素的下一个位置
// --ps->top;//因为如果要插入元素的话,那个数据是会被覆盖的
//
//
//}
//bool StackEmpty(Stack* ps)//判断栈是否为空
//{
// assert(ps);
// /*if (ps->top > 0)
// {
// return false;
//
// }
// else
// {
// return true;
// }*/
// //return ps->top ? false : true;
// return ps->top == 0;
//}
//int StackTop(Stack* ps)//取出栈顶元素
//{
// assert(ps);
// assert(ps->top > 0);
// return ps->a[ps->top - 1];
//}
//int StackSize(Stack* ps)
//{
// assert(ps);
// return ps->top;
//}
//接口
void StackInit(Stack* ps)
{
assert(ps);
ps->top = -1;//将top的值作为插入的地方
ps->capacity = 0;
ps->a = NULL;
}
void StackDestory(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = -1;
}
void StackPush(Stack* ps, int x)//栈顶插入
{
assert(ps);
if (ps->capacity == ps->top + 1)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->a = realloc(ps->a, newcapacity * sizeof(int));
if (ps->a == NULL)
{
printf("扩容失败\n");
return;
}
ps->capacity = newcapacity;
}
ps->a[++ps->top] = x;
}
void StackPop(Stack* ps)//栈顶删除
{
assert(ps);
assert(ps->top > -1);
--ps->top;
}
bool StackEmpty(Stack* ps)//判断栈是否为空
{
assert(ps);
return ps->top == -1;
}
int StackTop(Stack* ps)//取出栈顶元素
{
assert(ps);
return ps->a[ps->top];
}