#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <initializer_list>
#include <deque>
#include <list>
#include <array>
#include <forward_list>
#include <sstream>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <memory>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <type_traits>
#include <utility>
#include <tuple>
#include <bitset>
#include <regex>
#include <random>
#include <iomanip>
#include <ctime>
#include <cmath>
#define ERROR -1
using namespace::std;
using ElementType = int;
typedef int Position;
struct SNode;
typedef SNode* PtrToSNode;
using Stack = PtrToSNode;
struct SNode {
SNode(int M,Position t=-1):top(t),MaxSize(M){
Data = new ElementType[M];
}
bool IsFull(Stack &S)
{
return (S->top == S->MaxSize - 1);
}
bool push(Stack &S, ElementType X)
{
if (IsFull(S))
{
cout << "The stack is full.Invalid push." << endl;
return false;
}
else
{
S->Data[++(S->top)] = X;
return true;
}
}
bool IsEmpty(Stack &S)
{
return (S->top == -1);
}
ElementType pop(Stack &S)
{
if (IsEmpty(S))
{
cout << "The stack is empty.Invalid pop." << endl;
return ERROR;
}
else
{
return (S->Data[(S->top)--]);
}
}
private:
ElementType *Data;
Position top;
int MaxSize=10;
};
Stack CreateStack(int MaxSize)
{
Stack S = new SNode(MaxSize, -1);
return S;
}
struct MySNode;
typedef MySNode* PtrToMySNode;
struct MySNode {
ElementType* Data;
Position Top1;
Position Top2;
int MaxSize;
};
typedef PtrToMySNode MyStack;
MyStack createMyStack(int Maxsize)
{
MyStack mys = (MyStack)malloc(sizeof(MySNode));
mys->Data = (ElementType *)malloc(Maxsize * sizeof(ElementType));
mys->Top1 = -1;
mys->Top2 = Maxsize;
return mys;
}
bool IsMyStackFull(MyStack S)
{
return (S->Top2 - S->Top1 == 1);
}
bool IsMyFirstStackEmpty(MyStack S)
{
return (S->Top1 == -1);
}
bool IsMySecondStackEmpty(MyStack S)
{
return (S->Top2 == S->MaxSize);
}
bool mypush(MyStack S, ElementType X,int Tag)
{
if (IsMyStackFull(S))
{
cout << "Mystack is full.Invalid push." << endl;
return false;
}
else
{
if (Tag == 1)
{
S->Data[++(S->Top1)] = X;
}
else
{
S->Data[--(S->Top2)] = X;
}
}
return true;
}
ElementType mypop(MyStack S, int Tag)
{
if (Tag == 1)
{
if (IsMyFirstStackEmpty(S))
{
cout << "First Stack is empty.Invalid pop." << endl;
return ERROR;
}
else
{
return S->Data[(S->Top1)--];
}
}
else
{
if (IsMySecondStackEmpty(S))
{
cout << "Second Stack is empty.Invalid pop." << endl;
return ERROR;
}
else
{
return S->Data[(S->Top2)++];
}
}
}
int main()
{
MyStack S;
S = createMyStack(10);
mypush(S, 1, 1);
mypush(S, 2, 3);
cout << mypop(S, 1) << endl;
mypop(S, 1);
cout << mypop(S, 2) << endl;
}