⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arraystack.cpp

📁 数组堆栈与链表堆栈的实现代码
💻 CPP
字号:
#include<iostream.h>

enum error_code{
	success,
	overflow,
	underflow,
};

typedef char stack_entry;

const int maxstack=10;

class stack{
public:
	stack();
	bool empty() const;
	error_code pop();
	error_code push(const stack_entry &item);
	error_code top(stack_entry &item) const;
private:
	int count;
	stack_entry entry[maxstack];
};

stack::stack()
{
	count=0;
}

error_code stack::push(const stack_entry &item)
{
	error_code outcome=success;
	if(count>=maxstack)
		outcome=overflow;
	else 
		entry[count++]=item;
	return outcome;
}

error_code stack::pop()
{
	error_code outcome=success;
	if(count==0) 
		outcome=underflow;
	else 
		--count;
	return outcome;
}

error_code stack::top(stack_entry &item) const
{
	error_code outcome=success;
	if(count==0) 
		outcome=underflow;
	else
		item=entry[count-1];
	return outcome;
}

bool stack::empty() const
{
	bool outcome=true;
	if(count>0) outcome=false;
	return outcome;
}

void main()
{
	stack test;
	char x;
	test.push('a');
	test.top(x);
	cout<<x;
	cin>>x;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -