大话数据结构——栈的顺序存储

#include<iostream>

//#include<time.h>
//#include <stdlib.h>

using namespace std;

#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0

#define MAXSIZE 100//数组的最大大小
typedef int status;//返回的状态值
typedef int elemtype;//节点里数据的类型

typedef struct  
{
    elemtype data[MAXSIZE];//存放栈数据的数组
    int top;//指示栈顶元素的下标,假定当为空栈的时候为-1
}stacklist;

//进栈操作
status push_stack(stacklist *L,elemtype e)
{
    if(L->top==MAXSIZE-1)
        return ERROR;
    L->top++;
    L->data[L->top]=e;
    return OK;
}

//出栈操作
status pull_stack(stacklist *L,elemtype &e)
{
    if(L->top==-1)
        return ERROR;
    e=(L->data[L->top]);
    L->top--;
    return OK;
}

void max(int &a,int &b)
{
    int temp;
    if(a>=b)
    {
        temp=a;
        a=b;
        b=temp;
    }
}


int main()
{
    stacklist L;
    L.data[0]=1;
    L.data[1]=2;
    L.data[2]=3;
    L.top=2;

    push_stack(&L,6);
    cout<<L.top<<' '<<L.data[L.top]<<endl;

    elemtype e=2;
    pull_stack(&L,e);
    cout<<e<<endl;


    system("pause");
    return 1;

}

 

转载于:https://www.cnblogs.com/yanliang12138/p/4328263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值