简单数据结构---数组实现栈

/*
* File name  : LinkList.cpp
* Function   : 数组栈的C++实现
* Created on : 2016年4月20日
* Author     : beijiwei@qq.com
* Copyright  : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。
任何单位和个人不经本人允许不得用于商业用途
*/
#include <cstdio>
#include <iostream>

using namespace std;

#define MAX 10

typedef struct Astack {
	int data[MAX];
	int top;
}Astack;

void stack_init(Astack & S);

void stack_clear(Astack & S);

bool stack_is_empty(Astack & S);

bool stack_get_top(Astack & S, int & elem);

bool stack_push(Astack & S, int elem);

bool stack_pop(Astack & S, int &elem);

int stack_get_length(Astack & S);

int main(int argc, char** argv)
{
	Astack as;
	stack_init(as);
	cout << "Stack is empty ? " << stack_is_empty(as) << endl;
	stack_push(as, 10);
	stack_push(as, 11);
	stack_push(as, 12);
	stack_push(as, 13);
	cout << "Stack is empty ? " << stack_is_empty(as) << endl;
	cout << "The length of Stack as is : " << stack_get_length(as) << endl;
	int elem;
	stack_pop(as, elem);
	cout << "pop elem is : " << elem << endl;

	stack_pop(as, elem);
	cout << "pop elem is : " << elem << endl;

	stack_pop(as, elem);
	cout << "pop elem is : " << elem << endl;

	stack_pop(as, elem);
	cout << "pop elem is : " << elem << endl;

	stack_pop(as, elem);
	
	return 0;
}

void stack_init(Astack & S)
{
	S.top = -1;
}

void stack_clear(Astack & S)
{
	S.top =-1;
}

bool stack_is_empty(Astack & S) {
	return (S.top == -1) ? true : false;
}

bool stack_get_top(Astack & S, int & elem) {
	if (S.top == -1)
		return false;
	elem = S.data[S.top];
	return true;
}

bool stack_push(Astack & S, int elem)
{
	if (S.top == MAX - 1)
	{
		cout << "Stack is full, can not push !" << endl;
		return false;
	}
	S.data[++S.top] = elem;
	return true;
}

bool stack_pop(Astack & S, int &elem)
{
	if (S.top == -1)
	{
		cout << "Stack is empty, can not pop !" << endl;
		return false;
	}
	elem = S.data[S.top--];
	return true;
}

int stack_get_length(Astack & S) {
	return S.top+1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值