顺序栈的C++代码

顺序栈的C++实现,代码有所参考

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MaxSize 20

struct SeqStack{
    int Data[MaxSize];  // 存储元素的数组
    int Top;       		// 栈顶指针下标 
	SeqStack(){
		Top = 0;
		for(int i=0;i<MaxSize;i++)
			Data[i] = 0;
	}
};

bool Push(SeqStack &L, int add){
    if(L.Top == MaxSize-1){
    	cout<<"栈空间已满"<<endl;
        return 0;
    }
    L.Data[L.Top] = add;
    L.Top++;
    return 1;
}

int Pop(SeqStack &L){
    if(L.Top==0){
    	cout<<"空栈"<<endl;
        return 0;
    }
    int temp = L.Data[L.Top];
    L.Data[L.Top] = 0;
    L.Top--;
    return temp;
}

bool isEmpty(SeqStack L){
    if(L.Top == 0)
        return 1;
    return 0;
}

bool isFull(SeqStack L){
    if(L.Top == MaxSize-1)
        return 1;
    return 0;
}

void display(SeqStack L){
	for(int i=0;i<L.Top;i++)
		cout<<L.Data[i]<<" ";
	cout<<endl;
}

int main(){
	SeqStack seq;
	for(int i=0;i<10;i++)
		Push(seq,i);
	Pop(seq);
	display(seq);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值