栈
栈的概念
一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
- 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
- 出栈:栈的删除操作叫做出栈。出数据也在栈顶。
顺序栈代码实现
Stack.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//顺序表实现
typedef int STDataType;
typedef struct Stack
{
STDataType *a;
int top; //栈顶位置
int capacity; //栈容量
}Stack;//缩写ST
//都用一级指针实现
//对栈元素初始化
void StackInit(Stack *ps);
void StackDestroy(Stack *ps);
void StackPush(Stack *ps, STDataType x);
void StackPop(Stack *ps);
STDataType StackTop(Stack *ps);
bool StackEmpty(Stack *ps);
int StackSize(Stack *ps);
Stack.c
#include"Stack.h"
void StackInit(Stack *ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestroy(Stack *ps)
{
assert(ps);
if (ps->a)//非空,有空间占用再释放
{
free(ps->a);
}
ps->top = 0;
ps->capacity = 0;
ps->a = NULL;
}
void StackPush(Stack *ps,STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
STDataType *tmp = (STDataType *)realloc(ps->a, newcapacity*sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->capacity = newcapacity;
ps->a = tmp;
}
ps->a[ps->top++] = x;
//ps->top++;
}
void StackPop(Stack *ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
STDataType StackTop(Stack *ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
bool StackEmpty(Stack *ps)
{
assert(ps);
return ps->top == 0;//真(非零):返回tree。假(零):返回false
}
int StackSize(Stack *ps)
{
assert(ps);
return ps->top;
}
标签:ps,--,top,C语言,assert,void,STDataType,Stack
From: https://www.cnblogs.com/DSCL-ing/p/18344046