Skip to content

Commit

Permalink
栈的基本操作
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS authored Aug 26, 2016
1 parent fdbcad7 commit b06fcee
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cstdio>
#include <cstring>

#define ELEMENT_COUNT 100000

using namespace std;

// Requirement: every element in stack must be positive.
// Otherwise, M[0] should be set to -INF.
int S[ELEMENT_COUNT], M[ELEMENT_COUNT];
int sp = 1;

void push(int x)
{
S[sp] = x;
if (x > M[sp - 1])
{
M[sp] = x;
}
else
{
M[sp] = M[sp - 1];
}
sp++;
}

int pop()
{
return S[--sp];
}

bool empty()
{
return sp == 1;
}

int get_max()
{
return M[sp - 1];
}

int main()
{
while (true)
{
char o[10];
int a;
scanf(" %s", o);
if (strcmp(o, "push") == 0)
{
scanf("%d", &a);
push(a);
}
else if (strcmp(o, "pop") == 0)
{
printf("%d\n", pop());
}
else if (strcmp(o, "getmax") == 0)
{
printf("%d\n", get_max());
}
else if (strcmp(o, "abort") == 0)
{
return 0;
}
else
{
printf("No such instruction.\n");
}
}

return 0;
}

0 comments on commit b06fcee

Please sign in to comment.