POJ——栈的基本操作(AC)

本文介绍了一个简单的栈类实现,使用C++编程语言,并通过一组测试用例验证其正确性。该栈类支持基本操作如Push、Pop等,并提供检查栈是否为空或已满的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>

#define CAPACITY_DEFAULT   100 
#define TEST_GROUP_MAX     100

typedef int ElementType;

using namespace std;

class Stack{
public:
	Stack(void){
		top_ = 0;
		array = new ElementType [ CAPACITY_DEFAULT ];
	}
	Stack(int capacity){
		top_ = 0;
		array = new ElementType [ capacity ];
	}
	~Stack(void){
		delete[] array;
	}
	void MakeEmpty(void){
		top_ = 0;
	}
	void RevertOutput(void){
		for ( int i=0; i<top_; i++ ) 
			std::cout << array[ i ] << " ";
		std::cout << std::endl;
	}
	void Push(ElementType e){
		array[ top_++ ] = e;
	}
	ElementType Pop(void){
		return array[ --top_ ];
	}
	int IsEmpty(void){
		return top_ <= 0; 
	}
	int IsFull(void){
		return top_ >= capacity_;
	}
private:
	int capacity_;
	int top_;
	ElementType *array;
};

int _err_table[ TEST_GROUP_MAX ] = { 0 };
//Stack _stack[ TEST_GROUP_MAX ];
Stack *_stack;

void GroupTest(int grp);

int main(int argc, char**argv){
	int i;
	int group;
	cin >> group;
	_stack = new Stack [ group ];
	for ( i=0; i<group; i++){
		GroupTest(i);
	}

	for ( i=0; i<group; i++ ){
		if ( _err_table[ i ] == 0 )
			_stack[ i ].RevertOutput();
		else
			cout << "error" << endl;
	}
	delete[] _stack;
	return 0;	
}

void GroupTest(const int grp){
	int i;
	int num;
	int k;
	string str;
	cin >> num; 
	for ( i=0; i<num; i++ ){
		cin >> str;
		if ( str == "push" ){
			cin >> k;
			_stack[ grp ].Push(k);
		}else if ( !_stack[grp].IsEmpty() ){
			_stack[ grp ].Pop();
		}else{
			_err_table[ grp ] = 1;
		}
	}
}

<think>好的,我现在需要帮助用户了解基本操作以及与POJ相关的实现示例。首先,我要回顾的定义和基本操作,确保自己理解正确。是一种后进先出(LIFO)的数据结构,主要操作有push、pop、top、isEmpty和size。用户提到了POJ,可能是指北京大学的在线评测系统,所以需要提供相关的题目示例。 根据用户提供的引用内容,引用[2]和[3]详细说明了基本操作和STL中的实现方式。特别是引用[3]中的代码示例,展示了如何使用C++的STL,包括头文件引入、定义、push、pop、top等操作。这可能对用户有帮助。 接下来,我需要考虑用户可能需要的POJ题目。常见的相关题目有括号匹配、表达式求值等。比如POJ 2559(直方图中最大的矩形),通常用单调解决,这也是引用[3]中提到的单调的应用。不过用户可能希望看到更基础的示例,比如括号匹配,这在POJ中可能有对应的题目,比如POJ 3991。 需要确保示例代码的正确性,并解释清楚每个步骤。同时,根据用户要求,所有行内数学表达式要用$...$,但这里可能涉及较少数学内容,主要关注代码和操作步骤。另外,需要生成相关问题,帮助用户进一步学习。 可能的问题点:用户是否熟悉C++的STL?是否需要其他语言的示例?但用户的问题中提到了POJ,通常使用C++较多,所以用C++示例是合适的。另外,需要确保引用的正确引用标识,比如在解释的应用时引用对应的文献。 最后,检查是否符合系统级指令,如格式、引用标识、问题生成等。确保回答结构清晰,分点说明,并包含代码示例和相关问题。</think>### 基本操作POJ实现示例 (Stack)是一种**后进先出(LIFO)**的线性数据结构,核心操作包括: 1. **`push`**:将元素压入顶,时间复杂度$O(1)$; 2. **`pop`**:移除顶元素,时间复杂度$O(1)$; 3. **`top`**:获取顶元素,时间复杂度$O(1)$; 4. **`isEmpty`**:判断是否为空; 5. **`size`**:返回中元素数量[^2][^3]。 --- ### 的C++ STL实现示例 ```cpp #include <iostream> #include <stack> using namespace std; int main() { stack<int> s; s.push(10); // :10 s.push(20); // :10 → 20 cout << "顶元素: " << s.top() << endl; // 输出20 s.pop(); // 移除20,顶变为10 cout << "大小: " << s.size() << endl; // 输出1 return 0; } ``` --- ### POJ相关题目实现示例 #### **POJ 2559: 直方图中最大矩形** **问题描述**:给定直方图的高度数组,计算其能形成的最大矩形面积。 **实现思路**:使用**单调**快速找到每个高度向左右扩展的边界[^3]。 ```cpp #include <cstdio> #include <stack> using namespace std; const int MAXN = 1e5+5; int height[MAXN]; long long maxArea(int n) { stack<int> s; long long max_area = 0; for (int i = 0; i <= n; i++) { while (!s.empty() && (i == n || height[s.top()] > height[i])) { int h = height[s.top()]; s.pop(); int width = s.empty() ? i : i - s.top() - 1; max_area = max(max_area, (long long)h * width); } s.push(i); } return max_area; } ``` --- #### **POJ 3991: 括号匹配** **问题描述**:给定字符串,判断其括号是否合法匹配。 **实现思路**:用记录未匹配的左括号,遇到右括号时弹出顶匹配。 ```cpp #include <cstdio> #include <stack> using namespace std; bool isValid(string s) { stack<char> stk; for (char c : s) { if (c == '(') stk.push(c); else if (c == ')') { if (stk.empty()) return false; stk.pop(); } } return stk.empty(); } ``` --- ### 的应用场景 1. **函数调用**:保存函数调用时的返回地址和局部变量; 2. **表达式求值**:中缀转后缀表达式,如计算$3 + 5 \times (2 - 6)$; 3. **撤销操作**:编辑器中的“撤销”功能依赖记录操作历史; 4. **浏览器历史记录**:通过实现前进/后退功能[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值