BUPT OJ上没有stack.h,所以,所有栈的接口都要自己补全,这里展示的是链表实现。
含有min()的栈,要求min()返回当前栈最小值
方法一:使用单调栈
/*没有include stack.h*/
#include<iostream>
#include<string>
using namespace std;
struct Node{
int data;
Node *next;
Node():next(NULL){}
Node (int value):data(value),next(NULL){}
};
class stack{
private:
Node *head;
int node_count;
public:
stack():head(NULL),node_count(0){}
~stack(){
clear();
}
void push(int data)
{
Node *newnode=new Node(data);
if(newnode==NULL)
{
cout<<"Push error!"<<endl;
}
else
{
newnode->next=head;
head=newnode;
node_count++;
}
}
void pop()
{
if(head==NULL)
{
cout<<"Pop error!"<<endl;
}
else
{
Node *temp=head;
head=head->next;
delete temp;
node_count--;
}
}
int top()
{
if(head==NULL)
{
cout<<"Stack is empty!"<<endl;
}
else{
return head->data;
}
}
int size()
{
return node_count;
}
void clear()
{
while(size()!=0)
{
pop();
}
}
bool empty()
{
if(head==NULL)
return 1;
else
return 0;
}
};
stack sta;
stack min_sta;
class MinStack {
public:
void push(int x) {
sta.push(x);
if(min_sta.empty()||x<=min_sta.top())
min_sta.push(x);
}
void pop() {
if(sta.top()==min_sta.top())
{
min_sta.pop();
}
sta.pop();
}
int top() {
return sta.top();
}
int min() {
return min_sta.top();
}
bool empty()
{
return sta.empty();
}
void clear()
{
while(!sta.empty())
{
pop();
}
}
};
int main()
{
MinStack s;
string operation;
string errstr("operation error!");
while(true){
cin>>operation;
if(operation=="end") break;
else if(operation=="push"){
int x;
cin>>x;
s.push(x);
}
else if(operation=="pop"){
if(s.empty()) cout<<errstr<<endl;
else s.pop();
}
else if(operation=="top"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.top()<<endl;
}
else if(operation=="clear"){
s.clear();
}
else if(operation=="min"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.min()<<endl;
}
}
return 0;
}
编译器里一般会有stack.h,所以,直接include就好了,因为所有接口已经定义过了。这样一来代码可读性更强。
/*用一个单调的栈*/
/*要include stack.h*/
#include<iostream>
#include<string>
#include<stack.h>
using namespace std;
struct Node{
int data;
Node *next;
Node():next(NULL){}
Node (int value):data(value),next(NULL){}
};
class MinStack {
public:
void push(int x) {
sta.push(x);
if(min_sta.empty()||x<=min_sta.top())
min_sta.push(x);
}
void pop() {
if(sta.top()==min_sta.top())
{
min_sta.pop();
}
sta.pop();
}
int top() {
return sta.top();
}
int min() {
return min_sta.top();
}
bool empty()
{
return sta.empty();
}
void clear()
{
while(!sta.empty())
{
pop();
}
}
stack<int> sta;
stack<int> min_sta;
};
int main()
{
MinStack s;
string operation;
string errstr("operation error!");
while(true){
cin>>operation;
if(operation=="end") break;
else if(operation=="push"){
int x;
cin>>x;
s.push(x);
}
else if(operation=="pop"){
if(s.empty()) cout<<errstr<<endl;
else s.pop();
}
else if(operation=="top"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.top()<<endl;
}
else if(operation=="clear"){
s.clear();
}
else if(operation=="min"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.min()<<endl;
}
}
return 0;
}
方法二:在Node的结构体里加入minnum,用来记录最小值
/*包含min的栈*/
/*用变量minnum标记最小的node*/
#include<iostream>
#include<string>
using namespace std;
struct Node{
int data;
int minnum;
Node *next;
Node():next(NULL){}
Node (int value):data(value),next(NULL){}
};
class Stack{
private:
Node *head;
int node_count;
public:
Stack();
void push(int x);
int pop();
int top();
int min();
bool empty();
void clear();
};
Stack::Stack()
{
head=NULL;
node_count=0;
}
void Stack::push(int x)
{
node_count++;
Node *newnode=new Node;
newnode->next=head;
newnode->data=x;
newnode->minnum=x;
if(head!=NULL&&newnode->minnum>min())
{
newnode->minnum=min();
}
head=newnode;
}
int Stack::pop()
{
if(head==NULL)
cout<<"Stack is empty."<<endl;
int val=-1;
node_count--;
Node *temp=head;
val=head->data;
head=head->next;
delete temp;
temp=NULL;
return val;
}
int Stack::top()
{
if(head!=NULL)
{
return head->data;
}
else
cout<<"Stack is empty."<<endl;
}
int Stack::min()
{
if(head==NULL)
return -1;
return head->minnum;
}
bool Stack::empty()
{
if(node_count==0)
return true;
else
return false;
}
void Stack::clear()
{
while(node_count!=0)
{
pop();
}
}
int main()
{
Stack s;
string operation;
string errstr("operation error!");
while(true){
cin>>operation;
if(operation=="end") break;
else if(operation=="push"){
int x;
cin>>x;
s.push(x);
}
else if(operation=="pop"){
if(s.empty()) cout<<errstr<<endl;
else s.pop();
}
else if(operation=="top"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.top()<<endl;
}
else if(operation=="clear"){
s.clear();
}
else if(operation=="min"){
if(s.empty()) cout<<errstr<<endl;
else cout<<s.min()<<endl;
}
}
return 0;
}
运行结果: