#include<iostream>
using namespace std;
class Stack
{
public:
Stack();
~Stack();
private:
typedef struct Node
{
struct Node *P;
int x;
}*BitNode,BitValue;
BitNode Start;
BitNode Min_Start;
public:
void push(int x);
void pop(int *x,int*y);
int getMin();
};
Stack::Stack()
{
Start = (BitNode)malloc(sizeof(BitValue));
Min_Start = (BitNode)malloc(sizeof(BitValue));
Start->P = NULL;
Min_Start->P = NULL;
}
Stack::~Stack()
{
BitNode ST;
BitNode Min_ST;
ST = Start->P;
while (!ST)
{
Start->P = ST->P;
free(ST);
ST = Start->P;
}
free(Start);
Min_ST = Min_Start->P;
while (!Min_ST)
{
Min_Start->P = Min_ST->P;
free(Min_ST);
Min_ST = Min_Start->P;
}
free(Min_Start);
}
void Stack::push(int num)
{
BitNode Start_Input;
BitNode Min_Start_Input;
Start_Input = (BitNode)malloc(sizeof(BitValue));
Min_Start_Input = (BitNode)malloc(sizeof(BitValue));
Start_Input->x = num;
Start_Input->P = Start->P;
Start->P = Start_Input;
if (Min_Start->P != 0)
{
int return_num = Min_Start->P->x;
if (num < return_num)
Min_Start_Input->x = num;
else
Min_Start_Input->x = return_num;
}
else
{
Min_Start_Input->x = num;
}
Min_Start_Input->P = Min_Start->P;
Min_Start->P = Min_Start_Input;
}
int Stack::getMin()
{
return Min_Start->P->x;
}
void Stack::pop(int *x,int *y)
{
*x = Start->P->x;
*y = Min_Start->P->x;
BitNode pop_Start;
BitNode pop_Min_Start;
pop_Start = Start->P;
pop_Min_Start = Min_Start->P;
Start->P = Start->P->P;
Min_Start->P = Min_Start->P->P;
free(pop_Start);
free(pop_Min_Start);
}
void main()
{
Stack it;
int x;
int y;
it.push(58);
it.push(8);
it.push(7);
it.push(18);
it.push(28);
it.push(58);
it.pop(&x, &y);
cout << x << endl;
cout << y << endl;
}