c++中的栈的基本用法及实现

// 第四次练习4.25start.cpp : 定义控制台应用程序的入口点。
#include
#include
#include
using namespace std;
//栈模型
//栈的算法 和 数据类型的分离
void play04()
{
	stack	s;
	//入栈	入栈顺序:101 102 103。。。
	for (int i = 0; i < 10; ++i)
	{
		s.push(i + 101);
	}
	cout << "栈的大小:" << s.size() << endl;
	//出栈	出栈顺序:。。。103 102 101
	while (!s.empty())
	{
		int tmp = s.top();//获取栈顶元素
		cout << tmp << endl;
		s.pop();//弹出栈顶元素
	}
}
//teacher结点
class Teacher
{
public:
	char	name[32];
	int		age;
public:
	void printTeacher()
	{
		cout << "age: " << age << endl;
	}
};
int main()
{
	play04();

	Teacher t1, t2, t3;
	t1.age = 34;
	t2.age = 35;
	t3.age = 36;
	stack s1;
	s1.push(t1);
	s1.push(t2);
	s1.push(t3); 
	while (!s1.empty())
	{
		Teacher tmp1 = s1.top();
		tmp1.printTeacher();
		s1.pop();
	}

	stack s2;
	s2.push(&t1);
	s2.push(&t2);
	s2.push(&t3);

	while (!s2.empty())
	{
		Teacher *p = s2.top();
		p->printTeacher();
		s2.pop();
	}

	system("pause");
	return 0;
}

你可能感兴趣的:(c++中的栈的基本用法及实现)