数据结构第三章顺序堆栈代码

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值