在数组的首尾分别建立栈来构成双向栈:
代码如下:
//双向栈的实现
#include <iostream>
#include <string>
#define MAXSIZE 100
using namespace std;
struct Data
{
public:
int bookNum[MAXSIZE];
int top[2] ;
};
//双向栈的初始化
Data* SeqStackInit()
{
Data *s = new Data;
s->top[0] = -1;
s->top[1] = MAXSIZE;
return s;
}
//双向栈的判空
int SeqStackEmpty(Data* s)
{
if (s->top[1]-s->top[0] == 1)
{
return true;
}
else
return false;
}
//双向栈的入栈
void SeqStackPush(Data* s, int i, int x)
{
if (i == 0)
{
if (s->top[1]-s->top[0] == 1)
{
cout <<"栈满!"<<endl;
}
s->top[0]++;
s->bookNum[s->top[0]] = x;
cout<<"入栈元素:"<<s->bookNum[s->top[0]]<<endl;
}
else if (i == 1)
{
if (s->top[1]-s->top[0] == 1)
{
cout <<"