2.1:栈

1.栈的定义:

栈是多个元素组成的有序数列,只能在栈顶进行插入和删除。
栈具有后进先出或先进后出(LIFO)的特性。

2.顺序栈的实现:

顺序栈的存储结构与线性表的顺序存储结构(顺序表)一致。

class stack{
  publicstack();//初始化栈
  bool empty() const;//判断栈是否为空
  bool full() const;//判断栈是否为满
  error_code get_top(elementtype &x)const;///取栈顶元素
  error_code push(const elementtype x);//将元素压入栈
  error_code pop();//元素出栈

 private:
 int count;//表示元素个数
 elementtype data[maxlen];//定义数组存放元素
}

(1)初始化栈

stack::stack()
{count=0;}

(2)判断栈是否为空

bool stack::empty() const{
 if(count==0)
  return true;
 return false;
}

(3)取栈顶元素

error_code stack::get_top(elementtype &x)const
{
  if(empty())
    return underflow;
   else{
      x=data[count-1];
      return success;
   }
}

(4)入栈运算

 error_code stack::push(const elementtype x)
 {
   if(full())
    return overflow;
   else{
      data[count]=x;
      count++;
      return success;
   }
 }

(5)出栈运算

error_code pop()
{
  if(empty())
    return underflow;
    count--;
    return success;
}

(6)判断栈是否为满

bool full() const
{
  if(count==maxlen)
    return true;
    return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值