比较笨的C模拟
#define Ele int
#define MAX 100000
struct Stack {
Ele Top;
struct Stack* Next;
};
typedef struct Stack Stack;
static Stack *Sta;
static Stack *M;
Stack* CreateStack(Ele item) {
Stack* S;
S = (Stack*)malloc(sizeof(Stack));
if (S == NULL)
{
printf("内存错误");
return NULL;
}
S->Next = NULL;
S->Top = item;
return S;
}
void Push(Ele item, Stack* S)
{
if(!Sta)
{
Sta = (Stack*)malloc(sizeof(Stack));
Sta->Next=NULL;
}
if(!M)
{
M = (Stack*)malloc(sizeof(Stack));
M->Next=NULL;
M->Top =MAX;
}
Stack* tem = (Stack*)malloc(sizeof(Stack));
tem->Next = S->Next;
tem->Top = item;
S->Next = tem;
}
int Top(Stack* S)
{
if(!Sta)
{
Sta = (Stack*)malloc(sizeof(Stack));
Sta->Next=NULL;
}
if(!M)
{
M = (Stack*)malloc(sizeof(Stack));
M->Next=NULL;
M->Top =MAX;
}
if(S->Next==NULL)
return MAX;
return (S->Next->Top);
}
int IsEmpty(Stack* S)
{
return (S->Next == NULL);
}
int Pop(Stack* S) /*栈顶元素出栈*/
{
Stack* p;
Ele tmp;
if (IsEmpty(S)) {
printf("Pop an empty stack!\n");
return -1;
}
else {
p = S->Next;
S->Next = p->Next;
tmp = p->Top;
free(p);
return tmp;
}
}
void push(int value ) {
int tmp;
if(!Sta)
{
Sta = (Stack*)malloc(sizeof(Stack));
Sta->Next=NULL;
}
if(!M)
{
M = (Stack*)malloc(sizeof(Stack));
M->Next=NULL;
M->Top =MAX;
}
Push(value, Sta);
tmp=Top(M);
if(value<tmp)
Push(value, M);
else
Push(tmp, M);
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return 无
*/
void pop() {
Pop(Sta);
Pop(M);
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int top() {
// write code here
return Top(Sta);
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int min() {
// write code here
return Top(M);
}