顺序栈的基本操作实现c++

注:若有问题或需要补充的内容请告之,新手上路,谢谢。

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<ctype.h>
using namespace std;

#define MAXSIZE 100

/*结构体*/
typedef struct lnode
{
    int data[MAXSIZE];
    int top;
}seqstack;
/*初始化*/
seqstack *init_stack()
{
    seqstack *s;
    s=(seqstack*)malloc(sizeof(seqstack));
    s->top=-1;
    return s;
}
/*判断栈空*/
bool is_empty_seqstack(seqstack s)
{
    if(s.top==-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
/*销毁栈*/
void delete_stack(seqstack *s)
{
    s->top=-1;
    printf("栈销毁成功\n");
}
/*判断栈满*/
bool is_full_seqstack(seqstack s)
{
    if(s.top==MAXSIZE-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
/*进栈*/
void push(seqstack *s,int e)
{
    if(is_full_seqstack(*s))
    {
        printf("栈满");
    }
    else
    {
        s->data[++s->top]=e;
    }
}
/*出栈*/
void pop(seqstack *s,int &e)
{
    if(is_empty_seqstack(*s))
    {
        printf("栈空");
    }
    else
    {
        e=s->data[s->top--];
    }
}
/*取栈顶元素*/
void get_top(seqstack s,int &e)
{
    if(is_empty_seqstack(s))
    {
        printf("栈空");
    }
    else
    {
        e=s.data[s.top];
    }
}
/*输出栈*/
void print(seqstack s)
{
    while(!is_empty_seqstack(s))
    {
        printf("%d",s.data[s.top--]);
    }
}
int main()
{
    seqstack *s;
    int l,e;
    s=init_stack();
    printf("请输入需要入栈的元素个数:");
    scanf("%d",&l);
    printf("请依次输入入栈的元素:");
    for(;l>0;l--)
    {
        scanf("%d",&e);
        push(s,e);
    }
    printf("依次出栈的元素为:");
    while(!is_empty_seqstack(*s))
    {
        pop(s,e);
        printf("%d",e);
    }
    printf("\n");
    delete_stack(s);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值