seqstack
系统中的栈 是在缓存区上
数据结构中的栈 在堆上
========================
1、空增栈
2、空减栈
3、满增栈
4、满减栈
空栈 ,top指示器,表示的是新元素待插入的位置
满栈,top指示器指的是, 最后压栈的元素的位置
顺序栈的基本操作:
#include "seqstack.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
SeqStack *CreateSeqStack(int size) //创建栈
{
SeqStack* ss = ( SeqStack*)malloc(sizeof(SeqStack));
if(NULL ==ss)
{
perror("CreateSeqStack error malloc1");
return NULL;
}
ss->head = ( DATATYPE*)malloc(sizeof(DATATYPE)*size);
if(NULL ==ss->head)
{
perror("CreateSeqStack error malloc2");
return NULL;
}
ss->tlen = size;
ss->top = 0;
return ss;
}
int PushSeqStack(SeqStack *ss, DATATYPE *data) //入栈或者说压栈
{
if(NULL == ss ||NULL ==data)
{
fprintf(stderr,"SeqStack or data is null \n");
return 1;
}
if(IsFullSeqStack(ss))
{
fprintf(stderr,"PushSeqStack full\n");
return 1;
}
memcpy(&ss->head[ss->top],data,sizeof(DATATYPE));
ss->top++;
return 0;
}
int PopSeqStack(SeqStack *ss) //出栈或者说弹栈
{
if(NULL == ss )
{
fprintf(stderr,"SeqStack is null \n");
return 1;
}
if(IsEmptySeqStack(ss))
{
fprintf(stderr,"PopSeqStack is empty \n");
return 1;
}
ss->top--;
return 0;
}
int IsEmptySeqStack(SeqStack *ss) //判断是否为空
{
return 0 == ss->top;
}
int IsFullSeqStack(SeqStack *ss) //判断是否为满
{
return ss->top == ss->tlen;
}
DATATYPE *GetTopSeqStack(SeqStack *ss) //获得栈顶的元素
{
if(IsEmptySeqStack(ss))
{
return NULL;
}
return &ss->head[ss->top-1];
}
int DestroySeqStack(SeqStack *ss) //销毁栈
{
free(ss->head);
free(ss);
return 0;
}
链栈的基本操作:
#include "linkstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
LinkStackList *CreateLinkStackList()
{
LinkStackList *ls=(LinkStackList*)malloc(sizeof(LinkStackList));
if(NULL == ls)
{
perror("CreateLinkStackList malloc");
return NULL;
}
ls->top =NULL;
ls->clen = 0 ;
return ls;
}
int PushLinkStack(LinkStackList *ls, DATATYPE *data)
{
LinkStackNode* newnode = malloc(sizeof(LinkStackNode));
if(NULL == newnode)
{
perror("PushLinkStack malloc");
return 1;
}
memcpy(&newnode->data,data,sizeof(DATATYPE));
newnode->next = NULL;
if(IsEmptyLinkStack(ls))
{
ls->top = newnode;
}
else
{
newnode->next = ls->top;
ls->top = newnode;
}
ls->clen++;
return 0;
}
int PopLinkStack(LinkStackList *ls)
{
if(IsEmptyLinkStack(ls))
{
return 1;
}
LinkStackNode* tmp = ls->top;
ls->top = ls->top->next;
free(tmp);
ls->clen--;
return 0 ;
}
int IsEmptyLinkStack(LinkStackList *ls)
{
return 0 == ls->clen;
}
int GetSizeLinkStack(LinkStackList *ls)
{
return ls->clen;
}
DATATYPE *GetTopLinkStack(LinkStackList *ls)
{
if(IsEmptyLinkStack(ls))
return NULL;
return &ls->top->data;
}
int DestroyLinkStack(LinkStackList *ls)
{
int len = GetSizeLinkStack(ls);
int i = 0 ;
for(i = 0 ;i<len;i++)
{
PopLinkStack(ls);
}
free(ls);
return 0;
}
标签:NULL,return,ss,day4,int,ls,数据结构,top
From: https://blog.csdn.net/weixin_66436813/article/details/140715145