栈:顺序栈 链栈
分别用顺序表和链表实现栈,完成入栈、出栈、窥探栈顶元素等操作……
commom.h
#ifndef _COMMON_H_
#define _COMMOM_H_
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <memory.h>
#define ElemType int
void Swap(ElemType *a, ElemType *b)
{
ElemType temp = *a;
*a = *b;
*b = temp;
}
#endif /*_COMMOM_H_*/
stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include"common.h"
///
//顺序栈
#define SEQ_STACK_DEFAULT_SIZE 8
#define SEQ_STACK_INC_SIZE 3
typedef struct SeqStack
{
ElemType *base;
size_t capacity;
int top;
}SeqStack;
static bool SeqStack_Inc(SeqStack *pst);
void SeqStackInit(SeqStack *pst);
bool SeqStackIsFull(SeqStack *pst);
bool SeqStackIsEmpty(SeqStack *pst);
void SeqStackPush(SeqStack *pst, ElemType x);
void SeqStackPop(SeqStack *pst);
ElemType SeqStackTop(SeqStack *pst); // SeqStackPeek();
void SeqStackPrint(SeqStack *pst);
void SeqStackDestroy(SeqStack *pst);
//扩容
static bool SeqStack_Inc(SeqStack *pst)
{
ElemType *new_base = (ElemType*)realloc(pst->base, sizeof(ElemType)*(pst->capacity + SEQ_STACK_INC_SIZE));
if (new_base == NULL)
return false;
pst->base = new_base;
pst->capacity += SEQ_STACK_INC_SIZE;
return true;
}
//初始化
void SeqStackInit(SeqStack *pst)
{
pst->base = (ElemType*)malloc(sizeof(ElemType)*SEQ_STACK_DEFAULT_SIZE);
assert(pst->base != NULL);
pst->capacity = SEQ_STACK_DEFAULT_SIZE;
pst->top = 0;
}
bool SeqStackIsFull(SeqStack *pst)
{
assert(pst != NULL);
return pst->top >= pst->capacity;
}
bool SeqStackIsEmpty(SeqStack *pst)
{
assert(pst != NULL);
return pst->top == 0;
}
//入栈
void SeqStackPush(SeqStack *pst, ElemType x)
{
assert(pst != NULL);
if (SeqStackIsFull(pst) && !SeqStack_Inc(pst))
{
printf("栈空间已满并且扩容失败,%d 不能入栈.\n", x);
return;
}
pst->base[pst->top++] = x;
}
//出栈
void SeqStackPop(SeqStack *pst)
{
assert(pst != NULL);
if (SeqStackIsEmpty(pst))
{
printf("栈已空,不能出栈.\n");
return;
}
pst->top--;
}
//窥探栈顶元素
ElemType SeqStackTop(SeqStack *pst)
{
assert(pst != NULL);
if (SeqStackIsEmpty(pst))
{
printf("栈已空,不能取栈顶元素.\n");
return;
}
return pst->base[pst->top - 1];
}
void SeqStackPrint(SeqStack *pst)
{
assert(pst != NULL);
for (int i = pst->top - 1; i >= 0; --i)
printf("%d\n", pst->base[i]);
printf("\n");
}
//摧毁栈
void SeqStackDestroy(SeqStack *pst)
{
assert(pst != NULL);
free(pst->base);
pst->base = NULL;
pst->capacity = pst->top = 0;
}
///
//链栈
typedef struct LinkStackNode
{
ElemType data;
struct LinkStackNode *link; //next
}LinkStackNode;
typedef struct LinkStack
{
LinkStackNode *top;
}LinkStack;
void LinkStackInit(LinkStack *pst);
void LinkStackPush(LinkStack *pst, ElemType x);
void LinkStackPop(LinkStack *pst);
ElemType LinkStackTop(LinkStack *pst);
void LinkStackPrint(LinkStack *pst);
void LinkStackDestroy(LinkStack *pst);
//初始化
void LinkStackInit(LinkStack *pst)
{
assert(pst != NULL);
pst->top = NULL;
}
//入栈
void LinkStackPush(LinkStack *pst, ElemType x)
{
assert(pst != NULL);
LinkStackNode *node = (LinkStackNode*)malloc(sizeof(LinkStackNode));
assert(node != NULL);
node->data = x;
node->link = pst->top;
pst->top = node;
}
//出栈
void LinkStackPop(LinkStack *pst)
{
assert(pst != NULL);
LinkStackNode *p = pst->top;
pst->top = p->link;
free(p);
}
//窥探栈顶元素
ElemType LinkStackTop(LinkStack *pst)
{
assert(pst != NULL);
if (pst->top == NULL)
{
printf("栈已空,不能出栈.\n");
return;
}
return pst->top->data;
}
void LinkStackPrint(LinkStack *pst)
{
assert(pst != NULL);
LinkStackNode *p = pst->top;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->link;
}
printf("\n");
}
//摧毁
void LinkStackDestroy(LinkStack *pst)
{
assert(pst != NULL);
while (pst->top != NULL)
{
LinkStackNode *p = pst->top;
pst->top = p->link;
free(p);
}
}
#endif /* _STACK_H_ */