📄 doublestack.cpp
字号:
// doublestack.cpp: implementation of the doublestack class.
//
//////////////////////////////////////////////////////////////////////
#include "doublestack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
doublestack::doublestack()
{
}
doublestack::~doublestack()
{
}
void doublestack::initiate()
{
top[0]=-1;
top[1]=max;
}
int doublestack::empty(int i)
{
if(i<0 || i>2)
{
printf("Error 'i'!\n");
return 2;
}
else if(i==0)
{
if(top[0]==-1) return 1;
else return 0;
}
else if(i==1)
{
if(top[1]==max) return 1;
else return 0;
}
else
{
if(top[0]==-1 && top[1]==max) return 1;
else return 0;
}
}
int doublestack::push(elemt elm, int i)
{
int inc;
if((top[1])-(top[0])==1)
{
printf("Stack is full already!\n");
return 0;
}
else if(i<0 || i>1)
{
printf("Error 'i'!\n");
return 0;
}
else
{
if(i==0)
{
top[0]++;
data[top[0]]=elm;
return 1;
}
else if(i==1)
{
top[1]--;
data[top[1]]=elm;
return 1;
}
}
}
int doublestack::pop(elemt *elm, int i)
{
int inc;
if(i<0 || i>1)
{
printf("Error 'i'!\n");
return 0;
}
else if(i==0 && top[0]==-1 || i==1 && top[1]==max)
{
printf("Stack empty!\n");
return 0;
}
else
{
if(i==0) inc=1;
else inc=-1;
top[i]-=inc;
*elm=data[top[i]+inc];
return 1;
}
}
elemt doublestack::gettop(int i)
{
if(i<0 || i>1)
{
printf("Error 'i'!\n");
return 0;
}
else if(i==0 && top[0]==-1 || i==1 && top[1]==max)
{
printf("Stack empty!\n");
return 0;
}
else
{
return data[top[i]];
}
}
int doublestack::clear(int i)
{
if(i<0 || i>2)
{
printf("Error 'i'!\n");
return 0;
}
else if(i==0)
{
top[0]=-1;
return 1;
}
else if(i==1)
{
top[1]=max;
return 1;
}
else
{
top[0]=-1;
top[1]=max;
return 1;
}
}
int doublestack::size(int i)
{
if(i<0 || i>2)
{
printf("Error 'i'!\n");
return 0;
}
else if(i==0)
{
return top[0]+1;
}
else if(i==1)
{
return max-top[1];
}
else
{
return top[0]+1+max-top[1];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -