栈:操作受限的线性表
栈限定仅在表尾进行插入或删除操作的线性表,表尾:栈顶(top),表头:栈底 后进先出
逻辑结构:与线性表相同,一对一关系,数据元素连续存储。
物理存储结构:顺序栈和链栈
入栈:top<= val
top += 1;
出栈:top -= 1;
top => val
#ifndef MY_SEQSTACK_H
#define MY_SEQSTACK_H
#define STACK_INIT_SIZE 100
#define STACKINC 1.5
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0;
#define OVERFLOW -2;
#define NULLPTR -3;
typedef int Status;//状态
typedef int SElemtype;
typedef struct
{
SElemtype *base;//栈底指针
SElemtype *top;//栈顶指针
int stacksize;//栈的容量
}SeqStack;
Status InitStack(SeqStack *ps);
void DestroyStack(SeqStack *ps);
void ClearStack(SeqStack *ps);
int StackLength(const SeqStack *ps);
bool StackEmpty(const SeqStack *ps);
bool StackFull(const SeqStack *ps);
Status Push(SeqStack *ps,SElemtype val);
Status Pop(SeqStack *ps,SElemtype *pval);
Status GetTop(const SeqStack *ps,SElemtype *pval);//取栈顶元素
bool IncSize(SeqStack *ps);//增容
#endif
初始化
InitStack(SeqStack *ps)
{
assert(ps != NULL);
ps->base = (SElemtype*)malloc(sizeof(SElemtype)*STACK_INIT_SIZE);
if(NULL == base)
{
return OVERFLOW;
}
ps->top = ps->base;
stacksize = STACK_INIT_SIZE;
return OK;
}
销毁
void DestroyStack(SeqStack *ps)
{
assert(ps != NULL);
free(ps->base);
ps->base = NULL;
ps->top = NULL;
ps->stacksize = 0;
}
清空
void ClearStack(SeqStack *ps)
{
assert(ps != NULL);
ps->top = ps->base;
}
入栈
Status Push(SeqStack *ps,SElemtype val)
{
assert(ps != NULL);
if(StackFull(ps) && !IncSize(ps))
{
return OVERFLOW;
}
*ps->top = val;
ps->top += 1;
return OK;
}
得到栈顶元素
Status GetTop(const SeqStack *ps,SElemtype *pval)
{
assert(ps != NULL);
if(StackEmpty(ps))
{
return ERROR;
}
if(pval == NULL) return NULLPTR;
*pval = *(ps->top-1);
return OK;
}
出栈
Status Pop(SeqStack *ps,SElemtype *pval)
{
assert(ps != NULL);
if(EmptyStack(ps))
{
return ERROR;
}
if(pval == NULL) return NULLPTR;
ps->top -= 1;
*pval = *ps->top;
return OK;
}
栈里面元素的个数
int LengthStack(const SeqStack *ps)
{
assert(ps != NULL);
return (ps->top - ps->base);
}
判满
bool StackFull(const SeqStack *ps)
{
assert(ps != NULL);
return LengthStack(ps) == ps->stacksize;
}
判空
bool StackEmpty(const SeqStack *ps)
{
assert(ps != NULL);
return LengthStack(ps) == 0;
}
增容
bool Incsize(SeqStack *ps)
{
assert(ps != NULL);
int total = ps->stacksize * STACKINC;
SElemtype *newdata = (SElemtype*)realloc(ps->base,sizeof(SElemtype) * total);
if(NULL == newdata)
{
return false;
}
ps->base = newdata;
ps->top = ps->base + ps->stacksize;
ps->stacksize = total;
return true;
}
测试代码:
#include<stdio.h>
#include"My_SeqStack.h"
int main()
{
SeqStack mys;
int val;
InitStack(&mys);
for(int i = 0;i < 10;i++)
{
Push(&mys,i);
printf("%d ",i);
}
printf("\n");
printf("%d\n",GetTop(&mys,&val));
while(!StackEmpty(&mys))
{
Pop(&mys,&val);
printf("%d ",val);
}
printf("\n");
DestroyStack(&mys);
return 0;
}
运行结果:
增容测试:
链栈实现:
#ifndef LINKSTACK_H
#define LINKSTACK_H
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0;
#define OVERFLOW -2;
#define NULLPTR -3;
typedef int Status;//状态
typedef int SElemtype;
typedef struct StackNode
{
SElemtype data;//元素
struct StackNode *next;//结点
}StackNode,*PStackNode;
typedef struct
{
PStackNode top;//栈顶指针
int cursize;//数据元素个数
}LinkStack;
Status InitStack(LinkStack *ps);
void DestroyStack(LinkStack *ps);
void ClearStack(LinkStack *ps);
int StackLength(const LinkStack *ps);
bool StackEmpty(const LinkStack *ps);
Status Push(LinkStack *ps,SElemtype val);
Status Pop(LinkStack *ps,SElemtype *pval);
Status GetTop(const LinkStack *ps,SElemtype *pval);//取栈顶元素
#endif
#ifndef LINKSTACK_H
#define LINKSTACK_H
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0;
#define OVERFLOW -2;
#define NULLPTR -3;
typedef int Status;//状态
typedef int SElemtype;
typedef struct StackNode
{
SElemtype data;//元素
struct StackNode *next;//结点
}StackNode,*PStackNode;
typedef struct
{
PStackNode top;//栈顶指针
int cursize;//数据元素个数
}LinkStack;
初始化
```c
Status InitStack(LinkStack *ps)
{
assert(ps != NULL);
ps->top = Buynode();
if(NULL == ps->top)
{
return OVERFLOW;
}
ps->cursize = 0;
return OK;
}
申请结点空间
StackNode *Buynode()//结点
{
StackNode *s = (StackNode*)malloc(sizeof(StackNode));
if(s != NULL)
{
memset(s,0,sizeof(StackNode));
}
return s;
}
长度:
int StackLength(const LinkStack *ps)
{
assert(ps != NULL);
return ps->cursize;
}
判空
bool StackEmpty(const LinkStack *ps)
{
assert(ps != NULL);
//return ps->cursize == 0;
//return StackLength(ps) == 0;
return ps->top->next == NULL;
}
入栈:
Status Push(LinkStack *ps,SElemtype val)
{
assert(ps != NULL);
StackNode *s = Buynode();
if(NULL == s)
{
return OVERFLOW;
}
s->data = val;
s->next = ps->top->next;//头插法
ps->top->next = s;
ps->cursize += 1;
return OK;
}
获取栈顶元素
Status Gettop(const LinkStack *ps,SElemtype *pval)
{
assert(ps != NULL);
if(pval == NULL)
{
return NULLPTR;
}
*pval = ps->top->next->data;
return OK;
}
释放结点
void Freenode(StackNode *p)
{
free(p);
}
出栈:
Status Pop(LinkStack *ps,SElemtype *pval)
{
assert(ps != NULL);
if(pval == NULL)
{
return NULLPTR;
}
StackNode *q = ps->top->next;
ps->top->next = q->next;
*pval = q->data;
Freenode(q);
ps->cursize -= 1;
return OK;
}
清空
void ClearStack(LinkStack *ps)
{
assert(ps != NULL);
ps->cursize = 0;
while(ps->top->next != NULL)
{
StackNode *q = ps->top->next;
ps->top->next = q->next;
Freenode(q);
}
}
销毁:
void DestroyStack(LinkStack *ps)
{
assert(ps != NULL);
ClearStack(ps);
Freenode(ps->top);
ps->top = NULL;
}
运行结果: