栈是支持push和pop的数据结构。

push是在栈的顶端放入一组数据;pop是在栈的顶端取出一组数据。

Last in first out(LIFO):最后进入栈的一组数据会被最先取出.


 

样例:

 

#include
#include
using namespace std;
int main()
{
	stack s;//声明int类型数据存储的栈
	s.push(1);//『』→『1』
	s.push(2);//『1』→『1,2』
	s.push(3);//『1,2』→『1,2,3』
	printf("%dn",s.top());//3
	s.pop();//栈顶移除3,下同
	printf("%dn",s.top());
	s.pop();
	printf("%dn",s.pop());
	s.pop();//『1』→『』
	return 0;
}

返回栈顶元素:

// test_stack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	stack mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top()-=5;
	cout 
	

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注