[栈的基本操作]含有Min()的stack

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;
}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值