功能:
1.初始化
2.销毁
3.入栈
4.出栈
顺序栈
SeqStack.h
#pragma once
#include <stdio.h>
#define MAX_SIZE 1000
typedef char SeqStackType;
//顺序栈
typedef struct SeqStack
{
SeqStackType data[MAX_SIZE];
int Top;
int Bottom;
}SeqStack;
//初始化
void SeqStackInit(SeqStack *seq);
//销毁
void SeqStackDestory(SeqStack *seq);
//入栈
void SeqStackPush(SeqStack *seq, SeqStackType value);
//出栈
void SeqStackPop(SeqStack *seq, SeqStackType *e);
void PrintStack(SeqStack *seq, char *msg);
SeqStack.c
#include "SeqStack.h"
#define TITLE printf("--------------%s-------------\n",__FUNCTION__)
void SeqStackInit(SeqStack *seq)
{
if (seq == NULL)
{
return;
}
seq->Top = 0;
seq->Bottom = 0;
}
void SeqStackPush(SeqStack *seq, SeqStackType value)
{
if (seq == NULL)
{
return ;
}
if (MAX_SIZE == seq->Top)
{
printf("栈满\n");
return ;
}
seq->data[++seq->Top] = value;
}
void SeqStackPop(SeqStack *seq, SeqStackType *e)
{
if(seq == NULL)
{
return ;
}
if (seq->Bottom == seq->Top)
{
printf("栈空\n");
return ;
}
*e = seq->data[seq->Top--];
}
void PrintStack(SeqStack *seq, char *msg)
{
if (seq == NULL)
{
return;
}
printf("\n\n\n%s", msg);
printf("[seq->top]->");
while((seq->Top) != 0)
{
printf("[%c]->", seq->data[seq->Top--]);
}
printf("[bottom]\n");
}
void SeqStackDestory(SeqStack *seq)
{
if (seq == NULL) {
return;
}
seq->Top = seq->Bottom = 0;
}
void TestPush() {
SeqStack seq;
TITLE;
SeqStackInit(&seq);
SeqStackPush(&seq, 'a');
SeqStackPush(&seq, 'b');
SeqStackPush(&seq, 'c');
SeqStackPush(&seq, 'd');
SeqStackPush(&seq, 'e');
SeqStackPush(&seq, 'f');
PrintStack(&seq, "入栈六个元素");
}
void TestPop() {
SeqStack seq;
TITLE;
SeqStackInit(&seq);
SeqStackPush(&seq, 'a');
SeqStackPush(&seq, 'b');
SeqStackPush(&seq, 'c');
SeqStackPush(&seq, 'd');
SeqStackPush(&seq, 'e');
SeqStackPush(&seq, 'f');
SeqStackType e[] = {0};
SeqStackPop(&seq, e);
printf("\n\n出栈一个元素:%c\n", *e);
}
void TestDestory() {
SeqStack seq;
TITLE;
SeqStackInit(&seq);
SeqStackPush(&seq, 'a');
SeqStackPush(&seq, 'b');
SeqStackPush(&seq, 'c');
SeqStackPush(&seq, 'd');
SeqStackPush(&seq, 'e');
SeqStackPush(&seq, 'f');
SeqStackDestory(&seq);
PrintStack(&seq, "销毁栈");
}
int main() {
TestPush();
TestPop();
TestDestory();
return 0;
}
链式栈
LinkStack.h
#pragma once
#include<stdio.h>
typedef char StackType;
//创建一个结点的结构体
typedef struct LinkNode{
StackType data;
struct LinkNode* _next;
}LinkNode;
//创建栈的结构体
typedef struct LinkStack{
LinkNode* top; //栈顶结点
LinkNode* bottom; //栈底结点
size_t size; //栈的长度
}LinkStack;
void DestoryNode(LinkNode **node);
//初始化链式栈
void LinkStackInit(LinkStack* link);
//入栈
void LinkStackPush(LinkStack* link, StackType value);
//出栈
StackType LinkStackPop(LinkStack* link);
//销毁
void LinkStackDestory(LinkStack* stack);
LinkStack.c
#include <malloc.h>
#include "LinkStack.h"
LinkNode *CreatNewNode(StackType value)
{
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = value;
p->_next = NULL;
return p;
}
//初始化链式栈
void LinkStackInit(LinkStack* link)
{
if (link == NULL)
{
return;
}
link->top = CreatNewNode('0');
link->bottom = CreatNewNode('0');
link->top->_next = link->bottom;
link->size = 0;
}
//入栈
void LinkStackPush(LinkStack* link, StackType value)
{
if (link == NULL)
{
return ;
}
LinkNode* node = CreatNewNode(value);
LinkNode* next = link->top->_next;
link->top->_next = node;
node->_next = next;
link->size++;
}
//出栈
StackType LinkStackPop(LinkStack* link)
{
if (link == NULL)
{
return -1;
}
if (link->size == 0 && link->top->_next == link->bottom)
{
printf("空栈\n");
return 0;
}
LinkNode *pop = link->top->_next;
link->top->_next = pop->_next;
link->size--;
StackType n = pop->data;
DestoryNode(&pop);
return n;
}
//销毁一个结点
void DestoryNode(LinkNode** node) {
if (node == NULL) {
return;
}
if (*node == NULL) {
return;
}
free(*node);
}
int LinkStackEmpty(LinkStack* link) {
if (link == NULL) {
return -1;
}
if (link->top->_next == link->bottom && link->size == 0) {
return 0;
}
else{
return 1;
}
}
//销毁
void LinkStackDestory(LinkStack* stack)
{
if (stack == NULL) {
return;
}
//判断是否为空栈
while (!LinkStackEmpty(stack)) {
//拆除结点
LinkNode* Delete = stack->top->_next;
stack->top->_next = Delete->_next;
stack->size--;
//销毁结点
DestoryNode(&Delete);
}
}