C与C++数据结构与算法------栈(Stack)与队列(queue)。

线性结构-----栈(Stack)

(Stack):也称作堆栈,仅限制在表尾中进行插入与删除数据的线性表。在栈中我们可通过定义指针,如指向栈顶的(top)指针,指向栈尾的(bottom)指针。如果栈中已有SizeStack个时,称栈已经满了,此时若继续添加元素则将导致数据元素的上益.。
HWJ@文健
按照其存储方式:顺序栈与链式栈 。

  • [ 基本内容]
    【0】存储结构
    【1】初始化(InitStack).
    【2】判断空栈(StackEmpty)
    【3】取栈顶元素(Gettop).
    【4】元素的入栈(pushStack).
    【5】将栈顶元素出栈(PopStack).
    【6】求栈长(StackLength)。
    【7】清除栈(ClearStack).

【0】存储结构

#define StackSize 100
typedef struct {
   
    DataType stack[StackSize];    //DataType为数据类型
    int top;    //top为栈顶指针
} seqStack;
 【1】初始化(InitStack).
void InitStack(SeqStack *s){
   
    s->top=-1;   //将栈顶指针置为-1
}

【2】判断空栈(StackEmpty)。

int StackEmpty (SeqStack s){
   
  if(s.top==0)
   return 1;
  else 
   return 0;
   }

【3】取栈顶元素(Gettop).

int Getop(SeqStack s, DataType *e){
   
   if(S.top<=0){
   
       printf("栈已经空了\n");
       return 0;
      }
    else{
   
        *e=s.stack[S.top-1]  //取栈顶指针
        return 1;
   } 
}

【4】元素的入栈(pushStack).

int PushStack(SeqStack *s, DataType e){
   
  if(s->top>=StackSize){
   
     printf("栈已经满了\n");
     return 0;
    }
  else {
   
     s->stack[s->top]=e;   //将元素e的值赋值入栈
     s->top++;   //栈顶指针加一
     return 1;    
   }
}

【5】将栈顶元素出栈(PopStack).

int PopStack (SeqStack *s, DataType *e){
   
    if(s->top==0)
    {
       printf("栈已经满了\n");
         return 0;
}
else{
   
    s->top--;
    *e=s->stack[s->top];
    return 1;
   }
}
    【6】求栈长(StackLength)。
int StackLength(SeqStack *s){
   
   return s->top;
}
    【7】清除栈(ClearStack).

                
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值