防止系统重定义,我们自己定义
1.一个是顺序栈
2.一个是链栈
#ifndef _STACK_H_
#define _STACK_H_
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdbool.h>
//顺序栈
#define Stack_ElemType char
#define STACK_DEFAULT_SIZE 8
typedef struct SeqStack
{
Stack_ElemType* base; //栈空间
size_t capacity;//栈容量
int top; //栈顶指针
}SeqStack;
//函数申明
void SeqStackInit(SeqStack* pst);
bool SeqStackFull(SeqStack* pst);
bool SeqStackEmpty(SeqStack* pst);
Stack_ElemType SeqStackTop(SeqStack* pst);
void SeqStackPush(SeqStack* pst, Stack_ElemType v);
void SeqStackPop(SeqStack* pst);
void SeqStackShow(SeqStack* pst);
void SeqStackDestroy(SeqStack* pst);
//函数定义
void SeqStackInit(SeqStack* pst)
{
assert(pst != NULL);
pst->base = (Stack_ElemType*)malloc(sizeof(Stack_ElemType)* STACK_DEFAULT_SIZE );
assert(pst->base != NULL);
pst->capacity = STACK_DEFAULT_SIZE;
pst->top = 0;
}
bool SeqStackFull(SeqStack* pst)
{
assert(pst != NULL);
return pst->top >= pst->capacity;
}
bool SeqStackEmpty(SeqStack* pst)
{
assert(pst != NULL);
return pst->top == 0;
}
Stack_ElemType SeqStackTop(SeqStack* pst)
{
assert(pst != NULL && !SeqStackEmpty(pst));
return pst->base[pst->top-1];//因为插入时先 赋值再++ ,所以top在栈顶元素之上
}
void SeqStackPush(SeqStack* pst, Stack_ElemType v)
{
assert(pst != NULL );
if (SeqStackFull(pst))
{
printf("栈已满,%c 不能入栈.\n", v);
return;
}
pst->base[pst->top++] = v;
}
void SeqStackPop(SeqStack* pst)
{
assert(pst != NULL);
if (SeqStackEmpty(pst))
{
printf("栈已空, 不能出栈.\n");
return;
}
pst->top--;
}
void SeqStackShow(SeqStack* pst)
{
assert(pst != NULL);
int i = 0;
while (i<= pst->top-1)//避免对栈顶指针的操作,否则影响后续操作;
{
printf("%c\n", pst->base[pst->top-1-i]);
++i;
}
//assert(pst != NULL);
//for (int i = pst->top - 1; i >= 0; --i)
//printf("%c\n", pst->base[i]);
}
void SeqStackDestroy(SeqStack* pst)
{
assert(pst != NULL);
free(pst->base);
pst->base = NULL;
pst->capacity = pst->top = 0;
}
//链栈
typedef struct StackNode
{
Stack_ElemType data;
struct StackNode* next;
}StackNode;
typedef StackNode* LinkStack;
//函数申明
void LinkStackInit(LinkStack* pst);
bool LinkStackEmpty(LinkStack pst);
Stack_ElemType LinkStackTop(LinkStack pst);
void LinkStackPush(LinkStack* pst, Stack_ElemType v);
void LinkStackPop(LinkStack* pst);
void LinkStackShow(LinkStack pst);
void LinkStackDestroy(LinkStack* pst);
void LinkStackInit(LinkStack* pst)
{
assert(pst != NULL);
*pst = NULL;
}
bool LinkStackEmpty(LinkStack pst)
{
return pst == NULL;
}
Stack_ElemType LinkStackTop(LinkStack pst)
{
assert(pst != NULL);
return pst->data;
}
void LinkStackPush(LinkStack* pst, Stack_ElemType v)
{
assert(pst != NULL);
StackNode* s = (StackNode*)malloc(sizeof(StackNode));
s->data = v;
s->next = *pst;
*pst = s;
}
void LinkStackPop(LinkStack* pst)
{
assert(pst != NULL);
StackNode* s = *pst;
*pst = (*pst)->next;
free(s);
}
void LinkStackShow(LinkStack pst)
{
while (pst != NULL)
{
printf("%c\n", pst->data);
pst = pst->next;
}
}
void LinkStackDestroy(LinkStack* pst)
{
assert(pst != NULL);
while (!LinkStackEmpty(*pst))
{
StackNode* s = *pst;
*pst = (*pst)->next;
free(s);
}
}
#endif /* _STACK_H_ */
主函数的测试
#include"stack.h"
/*void main()
{
LinkStack st;
LinkStackInit(&st);
LinkStackPush(&st, 'A');
LinkStackPush(&st, 'B');
LinkStackPush(&st, 'C');
LinkStackPush(&st, 'D');
LinkStackPush(&st, 'E');
LinkStackShow(st);
printf("=====\n");
LinkStackPop(&st);
LinkStackShow(st);
printf("=====\n");
LinkStackPop(&st);
LinkStackShow(st);
LinkStackDestroy(&st);
}*/
/*
void main()
{
SeqStack st;
SeqStackInit(&st);
SeqStackPush(&st, 'A');
SeqStackPush(&st, 'B');
SeqStackPush(&st, 'C');
SeqStackPush(&st, 'D');
SeqStackPush(&st, 'E');
SeqStackShow(&st);
printf("top = %c\n",SeqStackTop(&st));
printf("=========\n");
SeqStackPop(&st);
SeqStackShow(&st);
printf("top = %c\n", SeqStackTop(&st));
printf("=========\n");
SeqStackPop(&st);
SeqStackShow(&st);
printf("top = %c\n", SeqStackTop(&st));
SeqStackDestroy(&st);
}*/
/*void main()
{
SeqStack st;
SeqStackInit(&st);
SeqStackPush(&st, 'B');
SeqStackPush(&st, 'Y');
SeqStackPush(&st, 'Z');
SeqStackPush(&st, 'O');
SeqStackPush(&st, 'L');
SeqStackPush(&st, 'L');
SeqStackPush(&st, 'E');
SeqStackPush(&st, 'H');
SeqStackPush(&st, 'k');
SeqStackShow(&st);
SeqStackDestroy(&st);
}
*/