Foundations of Fortune left class3- subject smallest element in the return stack 2

Foundations of Fortune left class3- subject smallest element in the return stack 2

1. Topic

Achieve a particular stack, the stack of the basic functions are implemented on the return stack and then to achieve the minimum operating elements.
[Requirements]
1. time complexity pop, push, getMin operations are O (1).
2. Stack type of design can use ready-made stack structure.

2. Analysis

1 can be used directly in STL Stack #include<stack>:
(. 1) empty () returns true stack is empty or
(2) pop () to remove the top element, does not return
(3) push () increased stack elements
(4) size () returns the number of elements in the stack
(. 5) top () returns the top element, does not remove
Note: pop () does not return only removed, and the top () returns not only removed

2. ideas: the use of two complete stacks ready, a first stack for storing data, all of the stack, the stack was completed in the above named mystack. The second stack is used for storing a minimum value, designated as follows using the help, if mystack on stacking, the comparator and the number of newly added elements help stack the stack, the stack anyone who is little pressure. If the stack is not empty then the two stack directly out of the stack.
. EG
1. Suppose the stack 10, and help myStack are empty, all pushed onto the stack;
2.13 stack, directly into the myStack, help stack 10, less than 13, 10 press-fitted;
3.5 stack, direct compression myStack into, help stack 10, more than five, 5 press-fitted.
4. Stack, mystack not empty, mystack5 the stack, help the top position of the stack 5, help the top 10 becomes.

3. core code

The two stacks stack, pop operations are encapsulated into function
(1) Drawing

void add_num(stack <int> &s1,stack <int> &s2,int num)
{
	s1.push(num);
	if(s2.empty())
	{
		s2.push(num);
	}
	else
	{
		s2.push(num > s2.top() ? s2.top():num);
	}
}

(2) out of the stack

void sub_num(stack <int> &s1,stack <int> &s2)
{
	
	if(s1.empty())
	{
		return;
	}
	else
	{
		s2.pop();
		s1.pop();
	}
}

3. Obtain a minimum

int getmin(stack <int> s2)
{
	return s2.top();
}

4. The complete code

#include<stack>
#include<iostream>


using namespace std;
int getmin(stack <int> s2)
{
	return s2.top();
}

void sub_num(stack <int> &s1,stack <int> &s2)
{
	
	if(s1.empty())
	{
		return;
	}
	else
	{
		s2.pop();
		s1.pop();
	}
}


void add_num(stack <int> &s1,stack <int> &s2,int num)
{
	s1.push(num);
	if(s2.empty())
	{
		s2.push(num);
	}
	else
	{
		s2.push(num > s2.top() ? s2.top():num);
	}
}

int main()
{
	stack <int> mystack;
	stack <int> help;
	add_num(mystack,help,10);
	cout<<getmin(help)<<endl;
	add_num(mystack,help,13);
	cout<<getmin(help)<<endl;
	add_num(mystack,help,5);
	cout<<getmin(help)<<endl;
	add_num(mystack,help,7);
	cout<<getmin(help)<<endl;
	add_num(mystack,help,3);
	cout<<getmin(help)<<endl;
	sub_num(mystack,help);
	cout<<getmin(help)<<endl;

	system("pause");
	return 0;
}

5. Results

(1) Results stack: the stack are assumed 10,13,5,7,3
Here Insert Picture Description
(2) Full Results: Suppose 10,13,5,7,3 respectively stack, the stack is then twice
Here Insert Picture Description
the minimum
Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1391

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103578253