栈是一种 特殊的线性表
栈仅能在线性表的的一端操作进行操作
栈顶:允许操作的一端
栈底:不允许操作的一端
图解:
1.初始化栈
2.入栈
3.出栈
seq_stack.h
#pragma once
#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#define STACK_INIT_SIZE 100 //栈的初始大小
#define STACKINC 1.5 //栈的
typedef int States; //状态值
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3
typedef int ElemType; //栈内数据类型
typedef struct SeqStack
{
ElemType *base; //栈底指针
ElemType *top; //栈顶指针
int capacity; //栈的容量
}SeqStack;
States Init_Stack(SeqStack *pstack); //初始化栈
void Destory_Stack(SeqStack *pstack); //摧毁栈
void Clear_Stack(SeqStack *pstack); //清空栈
int Stack_Lenth(const SeqStack *pstack); //栈的长度
bool Stack_Empty(const SeqStack *pstack); //判空
bool Stack_Full(const SeqStack *pstack); //判满
States Push_Stack(SeqStack *pstack, ElemType val); //入栈
States GetTop_Stack(const SeqStack *pstack, ElemType *pval); //取栈顶元素
States PopStack(SeqStack *pstack, ElemType *pval); //取栈顶元素并删除栈顶元素
#pragma once
#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#define STACK_INIT_SIZE 100 //栈的初始大小
#define STACKINC 1.5 //栈的
typedef int States; //状态值
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULLPTR -3
typedef int ElemType; //栈内数据类型
typedef struct SeqStack
{
ElemType *base; //栈底指针
ElemType *top; //栈顶指针
int capacity; //栈的容量
}SeqStack;
States Init_Stack(SeqStack *pstack); //初始化栈
void Destory_Stack(SeqStack *pstack); //摧毁栈
void Clear_Stack(SeqStack *pstack); //清空栈
int Stack_Lenth(const SeqStack *pstack); //栈的长度
bool Stack_Empty(const SeqStack *pstack); //判空
bool Stack_Full(const SeqStack *pstack); //判满
States Push_Stack(SeqStack *pstack, ElemType val); //入栈
States GetTop_Stack(const SeqStack *pstack, ElemType *pval); //取栈顶元素
States PopStack(SeqStack *pstack, ElemType *pval); //取栈顶元素并删除栈顶元素
.cpp
#include "seq_stack.h"
States Init_Stack(SeqStack *pstack) //初始化栈
{
assert(pstack != NULL);
ElemType *tmp = (ElemType*)malloc(sizeof(ElemType)*STACK_INIT_SIZE);
if (tmp == NULL)
{
return OVERFLOW;
}
pstack->base = tmp;
pstack->top = pstack->base;
pstack->capacity = STACK_INIT_SIZE;
return OK;
}
void Destory_Stack(SeqStack *pstack) //摧毁栈
{
assert(pstack != NULL);
free(pstack->base);
pstack->base = NULL;
pstack->top = NULL;
pstack->capacity = 0;
}
void Clear_Stack(SeqStack *pstack) //清空栈
{
assert(pstack != NULL);
pstack->top = pstack->base;
}
int Stack_Lenth(const SeqStack *pstack) //栈的长度
{
assert(pstack != NULL);
return pstack->top - pstack->base;
}
bool Stack_Empty(const SeqStack *pstack) //判空
{
assert(pstack != NULL);
return pstack->base == pstack->top;
}
bool Stack_Full(const SeqStack *pstack) //判满
{
assert(pstack != NULL);
return Stack_Lenth(pstack) == pstack->capacity;
}
States Push_Stack(SeqStack *pstack, ElemType val) //入栈
{
assert(pstack != NULL);
if (Stack_Full(pstack))
{
return ERROR;
}
*pstack->top = val;
pstack->top += 1;
return OK;
}
States GetTop_Stack(const SeqStack *pstack, ElemType *pval) //取栈顶元素
{
assert(pstack != NULL);
if (Stack_Empty(pstack))
{
return ERROR;
}
if (pval == NULL)
{
return NULLPTR;
}
*pval = *(pstack->top - 1);
return OK;
}
States PopStack(SeqStack *pstack, ElemType *pval) //取栈顶元素并删除栈顶元素
{
assert(pstack != NULL);
if (Stack_Empty(pstack))
{
return ERROR;
}
if (pval == NULL)
{
return NULLPTR;
}
pstack->top -= 1;
*pval = *pstack->top;
return OK;
}
main.cpp
int main()
{
SeqStack my_seqstack;
int val;
Init_Stack(&my_seqstack);
for (int i = 0; i < 10; i++)
{
Push_Stack(&my_seqstack, i);
}
GetTop_Stack(&my_seqstack, &val);
printf("%d \n", val);
for (int i = 0; i < 10; i++)
{
PopStack(&my_seqstack, &val);
printf("%d ", val);
}
printf("\n");
val = Stack_Lenth(&my_seqstack);
Clear_Stack(&my_seqstack);
Destory_Stack(&my_seqstack);
_CrtDumpMemoryLeaks();
system("pause");
return 0;
}
增容函数
bool Inc_Size(SeqStack *pstack) //增容
{
assert(pstack != NULL);
int total = pstack->capacity*STACKINC;
ElemType *newdata = (ElemType*)realloc(pstack->base, sizeof(ElemType)*total);
if (newdata == NULL)
{
return false;
}
pstack->base = newdata;
pstack->top = pstack->base + pstack->capacity;
pstack->capacity = total;
return true;
}