什么是栈
一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除的一端称为栈顶,另一端称为栈低。栈中的数据元素遵守后进先出LIFO的原则。
栈的插入操作称为压栈或进栈,栈的删除操作称为退栈或出栈。栈的主要特点是“后进先出(last in first out,LIFO)”。
栈的性质
在操作系统中,栈是向下生长的。栈顶和栈底不是上下决定,而是有入栈方向决定!
(1)集合性。栈是由多个元素集合而成,当没有元素的空集合称为空栈;
(2)线性结构。除栈底元素和栈顶元素外,栈中任一元素均有唯一的前驱元素和后继元素;
(3)受限制的运算。只允许在栈顶实施压入或弹出操作,且栈顶位置由栈指针所指示;
(4)数学性质。当多个编号元素依某种顺序压入,且可任意时刻弹出时,所获得的编号元素排列的数目,恰好满足Catalan数列(组合数学数列)的计算
入栈和出栈
栈的插入操作称为压栈或进栈,栈的删除操作称为退栈或出栈。栈的主要特点是“后进先出(last in first out,LIFO)”。
初始化栈
void StackInit(ST* ps);
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
栈的销毁
void StackDestory(ST* ps);
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);//释放
ps->a = NULL;//置空
ps->capacity = ps->top = 0;//容量置0
}
入栈
void StackPush(ST* pS, STDataType x);
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
出栈
void StackPop(ST* ps);
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));//ps不为空才Pop
--ps->top;
}
获取栈顶元素
STDataType StackTop(ST* ps);
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];//访问top的前一个位置
}
检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
获取栈顶中有效元素个数
int StackSize(ST* ps);
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
Stack.h的代码:
#pragma once
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int STDataType;
typedef struct stack
{
STDataType* a;//定义一个数组a
int top;//标识插入栈顶的位置
int capacity;//容量
}ST;
//初始化栈
void StackInit(ST* ps);
//栈的销毁
void StackDestory(ST* ps);
//入栈
void StackPush(ST* pS, STDataType x);
//出栈
void StackPop(ST* ps);
//获取栈顶元素
STDataType StackTop(ST* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);
//获取栈顶中有效元素个数
int StackSize(ST* ps);
Stack.c的代码:
#include"Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);//释放
ps->a = NULL;//置空
ps->capacity = ps->top = 0;//容量置0
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));//ps不为空才Pop
--ps->top;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];//访问top的前一个位置
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
test.c的代码:
#include"Stack.h"
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
}
int main()
{
TestStack();
return 0;
}
栈的性质是后进先出或者说先进后出,在test.c代码块中1、2、3是先进的,先出2个元素就是3和2,4和5又是后进的,所以要比1先出,正确的顺序是32541.