#include <stdio.h>
#include <stdlib.h>
#define ElemType int
#define MAXSIZE 10
typedef struct
{
ElemType Stack[MAXSIZE];
int Lefttop;
int Righttop;
}DUPSQSTACK;
DUPSQSTACK * InitDupStack(DUPSQSTACK *s);
int Empty_DupStack(DUPSQSTACK *s);
int Full_DupStack(DUPSQSTACK *s);
int Push_DupStack(DUPSQSTACK *s,char status,ElemType x);
int Pop_DupStack(DUPSQSTACK *s,char status,ElemType x);
int DestroyDupStack(DUPSQSTACK *s);
DUPSQSTACK * InitDupStack(DUPSQSTACK *s)
{
s=(DUPSQSTACK *)malloc(sizeof(DUPSQSTACK));
if(s)
{
s->Lefttop=-1;
s->Righttop=MAXSIZE;
return s;
}
else
{
printf("Fail to apply for the memory.\n");
exit(0);
}
}
int Empty_DupStack(DUPSQSTACK *s)
{
return(s->Lefttop==-1&&s->Righttop==MAXSIZE); // if s->Lefttop=-1&&s->Righttop=MAXSIZE,return 1;
}
int Full_DupStack(DUPSQSTACK *s)
{
return(s->Lefttop+1==s->Righttop);
}
int Push_DupStack(DUPSQSTACK *s,char status,ElemType x)
{
if(!Full_DupStack(s))
{
if('L'==status)
{
s->Stack[++s->Lefttop]=x;
printf("enter the left stack element : %d\n",s->Stack[s->Lefttop]);
return 0;
}
else if('R'==status)
{
s->Stack[--s->Righttop]=x;
printf("enter the right stack element : %d\n",s->Stack[s->Righttop]);
return 0;
}
else
{
printf("Input error , the program ends.\n");
exit(0);
}
}
else
{
printf("The stack is full.\n");
exit(0);
}
}
int Pop_DupStack(DUPSQSTACK *s,char status,ElemType x)
{
if(Empty_DupStack(s))
{
printf("The stack is empty,program is ended.\n");
exit(0);
}
else
{
if('L'==status)
{
if(s->Lefttop<0)
exit(0);
else
{
printf("out the left stack element : %d\n",s->Stack[s->Lefttop]);
x=s->Stack[s->Lefttop--];
}
}
else if('R'==status)
{
if(s->Righttop>=MAXSIZE)
{
exit(0);
}
else
{
printf("out the right stack element : %d\n",s->Stack[s->Righttop]);
x=s->Stack[s->Righttop++];
}
}
else
{
printf("Input error,program ends .\n");
exit(0);
}
return 0;
}
}
int DestroyDupStack(DUPSQSTACK *s)
{
free(s);
s=NULL;
return 0;
}
int main()
{
DUPSQSTACK *S=NULL;
ElemType x;
S=InitDupStack(S);
Push_DupStack(S,'L',1);
Push_DupStack(S,'L',2);
Push_DupStack(S,'R',3);
Push_DupStack(S,'R',4);
Pop_DupStack(S,'L',x);
Pop_DupStack(S,'L',x);
Pop_DupStack(S,'R',x);
DestroyDupStack(S);
return 0;
}
共享栈基本用法(顺序存储结构)
最新推荐文章于 2023-03-21 22:53:36 发布
本文介绍了一种特殊的双端栈数据结构,并提供了完整的C语言实现代码。该双端栈能够在两端分别进行压栈和弹栈操作,适用于需要高效利用内存空间的场景。文章详细解释了初始化、判断空满状态、压栈、弹栈及销毁等核心操作。
&spm=1001.2101.3001.5002&articleId=45536299&d=1&t=3&u=d3bdc051fca8467f865b9cd85c40f963)
452

被折叠的 条评论
为什么被折叠?



