c语言调用库实现栈,终于把栈的实现库遍好了(C语言也可以用哦)

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

/*mystack.h*/

#ifndef MYSTACK_H

#define MYSTACK_H

#define StackMax 10000

template

struct mystack

{

public:

StackType item[StackMax];//Storage container

int now;//Amount

mystack(){now=0;}//Constructor(to initialize 'now')

typedef StackType iterator;//Types of loop variable

void clear()//This function will clear the stack(initialization).

{

while(!empty())

del();

}

void del()//This function will remove top element(Non-return).

{

item[--now]=(StackType)0;

}

void push(StackType num)//This function will insert element.

{

item[now++]=num;

}

StackType pop()//This function will remove top element.

{

StackType res=item[--now];

item[now]=(StackType)0;

return res;

}

StackType top()//This function will return the value.

{

return item[now-1];

}

void swap(mystack&x)//This function will swap contents.

{

int i=now,maxnum=(now>x.now?now:x.now);

now=x.now;

x.now=i;

StackType t;

for(int i=0;i

{

t=item[i];

item[i]=(StackType)x.item[i];

x.item[i]=t;

}

}

bool empty()//This function will test whether container is empty.If it's not empty,it will return 1(true).

{

return now?0:1;

}

int size()//This function will return the amount of the value what is in the stack.

{

return now;

}

bool operator==(const mystack& x)

{

if(now!=x.now)return false;

for(int i=0;i

{

if(item[i]!=(StackType)x.item[i])

return false;

}

return true;

}

bool operator!=(const mystack&x)

{return (!(*this == x)); }

bool operator

{return (now

bool operator>(const mystack&x)

{return (now>x.now); }

bool operator<=(const mystack&x)

{return (now<=x.now); }

bool operator>=(const mystack&x)

{return (now>=x.now); }

mystack&operator=(const mystack& x)

{

now=x.now;

for(int i=0;i

item[i]=(StackType)x.item[i];

return *this;

}

mystack operator+(const mystack& x)//+ will connect two mystack's variables

{

mystack _X;

for(int i=0;i

_X.item[i]=item[i];

for(int j=now;j

_X.item[j]=(StackType)x.item[j-now];

_X.now=now+x.now;

return _X;

}

mystack&operator+=(const mystack& x)//"a+=b" is the same as "a=a+b".

{

*this=*this+x;

return *this;

}

mystack operator+(const StackType& x)//+ is the same as push but it won't change its value.

{

mystack _X;

for(int i=0;i

_X.item[i]=item[i];

_X.now=now;

_X.push(x);

return _X;

}

mystack&operator+=(const StackType& x)//+= is the same as push but it has returned value.

{

push(x);

return *this;

}

};

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值