64,282
社区成员
发帖
与我相关
我的任务
分享
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);
int x = 99;
//pop(dstack,0,x);
//x = 1;
//std::cout << "the low stack is " << x << endl;
std::cout << "the low stack is "<< endl;
int x = 99;
//pop(dstack,0,x);
x = 1;
std::cout << "the low stack is " << x << endl;
//#include "stdafx.h"
#include <stdio.h>
#include "iostream"
using namespace std;
#include "string.h"
#include "stdlib.h"
#include "ctype.h"
#define RT_Success 0
#define RT_Failed 1
#define RT_NotImpl 2
#define Status int
typedef struct{
int *base[2];
int *top[2];
}BDStacktype; //双向栈类型
Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0] = new int;
tws.base[1] = tws.base[0] + m;
tws.top[0] = tws.base[0];
tws.top[1] = tws.base[1];
return RT_Success;
}
Status push(BDStacktype &tws,int i,int x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if(tws.top[0]>tws.top[1])
return RT_Failed;
switch(i)
{
case 0:
*(tws.top[0]) = x;
tws.top[0]++;
break;
case 1:
*(tws.top[1]) = x;
tws.top[1] --;
break;
default:
return RT_Failed;
}
return RT_Success;
}
Status pop(BDStacktype &tws,int i,int &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
switch(i)
{
case 0:
if (tws.base[0] == tws.top[0])
{
return RT_Failed;
}
tws.top[0]--;
x = *(tws.top[0]);
break;
case 1:
if (tws.base[1] == tws.top[1])
{
return RT_Failed;
}
tws.top[1] ++;
x = *(tws.top[1]);
break;
default:
return RT_Failed;
}
return RT_Success;
}
void main()
{
//return 0;
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);
int x = 99;
pop(dstack,0,x);
std::cout << "the low stack is " << x << endl;
//int y = 99;
//pop(dstack,1,y);
//std::cout<<"the high stack is "<<x<<endl;
//if (y>3)
//{
// std::cout<<"md"<<endl;
//}
//std::cout<<y<<endl;
}
//这样在我的VC6.0可以运行