// 第四次练习4.25start.cpp : 定义控制台应用程序的入口点。
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
//栈模型
//栈的算法 和 数据类型的分离
void play04()
{
stack<int> 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<Teacher> s1;
s1.push(t1);
s1.push(t2);
s1.push(t3);
while (!s1.empty())
{
Teacher tmp1 = s1.top();
tmp1.printTeacher();
s1.pop();
}
stack<Teacher *> 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++中的栈的基本用法及实现
最新推荐文章于 2024-07-06 11:36:50 发布