本文整理汇总了C++中MyStack::top方法的典型用法代码示例。如果您正苦于以下问题:C++ MyStack::top方法的具体用法?C++ MyStack::top怎么用?C++ MyStack::top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyStack
的用法示例。
在下文中一共展示了MyStack::top方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_qa
void test_qa(AnsType& expectAns, OpreateType& opreateParam, InitType& initData, DataType1 firstData = DataType1()) {
AnsType ans;
MyStack work;
for(int i=0; i<opreateParam.size(); i++) {
int ansTmp = -1;
if(opreateParam[i] == "push") {
work.push(firstData[i]);
}
if(opreateParam[i] == "pop") {
ansTmp = work.pop();
}
if(opreateParam[i] == "top") {
ansTmp = work.top();
}
if(opreateParam[i] == "empty") {
ansTmp = work.empty();
}
ans.push_back(ansTmp);
}
int index = getIndex();
bool check = eq(ans, expectAns);
if(!check) {
printf("index %d: NO\n", index);
output("opreateParam", opreateParam);
output("initData", initData);
output("firstData", firstData);
output("ans", ans);
output("expectAns", expectAns);
} else {
printf("index %d: YES\n", index);
}
printf("\n");
}
示例2: main
int main(){
MyStack<int> a;
MyQueue<int> c;
a.push(4);
a.push(5);
cout << a.top() << endl;
a.pop();
cout << a.top() << endl;
c.enqueue(4);
c.enqueue(5);
cout << c.front() << endl;
c.dequeue();
cout << c.front() << endl;
}
示例3: push
bool StackWithMin::push(int d){
Node* p = new Node(d);
p->next = head;
head = p;
++size;
if(d<minValue){
minS.push(d);
minS.top(minValue);
}
return true;
}
示例4: processChar
void processChar( char item, MyStack<Token> &stack, string &operations, bool &OK )
{
// Note:
// I could check top of stack before adding tokens
// and catch many errors "now" instead of later.
// I'd rather be lazy and let collapse() catch the errors.
Token token = toToken(item);
switch (token)
{
case TOKEN_VALUE:
if ( stack.isEmpty() ) { stack << token; return; }
switch ( stack.top() )
{
case TOKEN_LEFT_GROUP: stack << token; break;
case TOKEN_ADDITION: stack << token; break; // don't collapse yet
case TOKEN_MULTIPLICATION: collapse( stack << token, operations, OK ); break;
default: OK = false; break;
}
break;
case TOKEN_ADDITION:
collapse( stack, operations, OK);
stack << TOKEN_ADDITION;
break;
case TOKEN_MULTIPLICATION:
case TOKEN_LEFT_GROUP:
stack << token;
break;
case TOKEN_RIGHT_GROUP:
// clear any pending addidion
collapse( stack, operations, OK );
if ( !OK ) return;
// convert ( value ) to value
if ( !stack.isEmpty() && TOKEN_VALUE == stack.pop()
&& !stack.isEmpty() && TOKEN_LEFT_GROUP == stack.pop() )
stack << TOKEN_VALUE;
else
OK = false;
break;
case TOKEN_UNKNOWN:
OK = false;
break;
}
}
示例5: collapse
// remove one complete arithmetic operation, if it is there
void collapse( MyStack<Token> &stack, string &operations, bool &OK )
{
// It should end with a value
if ( stack.isEmpty() || stack.top() != TOKEN_VALUE ) return;
stack.pop();
// if that was the ONLY thing on the stack or it is preceeded by (, it is OK
if ( stack.isEmpty() || TOKEN_LEFT_GROUP == stack.top() )
{
stack << TOKEN_VALUE;
return;
}
// The value should be preceeded with an operator
Token operation;
if ( stack.isEmpty() || !isOperator(operation = stack.pop()) ) { OK = false; return; }
operations += ((operation==TOKEN_ADDITION) ? "+" : "*");
// The operator should be preceeded with a value
if ( stack.isEmpty() || stack.pop() != TOKEN_VALUE ) { OK = false; return; }
// sucessful collapse - to a single value
stack << TOKEN_VALUE;
}
示例6: pop
bool StackWithMin::pop(int& d){
if(empty())
return false;
d = head->data;
Node* p = head;
head = head->next;
delete p;
--size;
if(d==minValue){
minS.pop(minValue);
minS.top(minValue);
if(minS.empty())
minValue = INT_MAX;
}
return true;
}
示例7: main
int main() {
MyStack<int> st;
for (int i = 0; i < 100; i++)
st.push(i);
std::cout << st.size() << std::endl << std::endl;
for (int i = 0; i < 10; i++)
st.pop();
std::cout << st.size() << std::endl << std::endl;
for (int i = 0; i < 10; i++)
st.push(i);
std::cout << st.size() << std::endl << std::endl;
while (!st.empty()) {
int k;
st.top(k);
std::cout << k << std::endl;
st.pop();
}
std::cout << st.size() << std::endl << std::endl;
return 0;
}
示例8: libFunc
int libFunc()
{
MyStack s;
return s.top();
}