当前位置: 首页>>代码示例>>C++>>正文


C++ MyStack::top方法代码示例

本文整理汇总了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");
}
开发者ID:tiankonguse,项目名称:leetcode-solutions,代码行数:35,代码来源:implement-stack-using-queues.cpp

示例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;
}
开发者ID:gerhard2202,项目名称:Data-Structures-Classwork,代码行数:17,代码来源:listsandbox.cpp

示例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;
}
开发者ID:beyondaymk,项目名称:ctci150,代码行数:11,代码来源:p0302.cpp

示例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;
		}
	}
开发者ID:nguyenkristie,项目名称:cpp-projects,代码行数:49,代码来源:ExpressionTester.cpp

示例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;
	}
开发者ID:nguyenkristie,项目名称:cpp-projects,代码行数:25,代码来源:ExpressionTester.cpp

示例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;
}
开发者ID:beyondaymk,项目名称:ctci150,代码行数:16,代码来源:p0302.cpp

示例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;
}
开发者ID:chenhch8,项目名称:data_structure,代码行数:20,代码来源:week4_1.cpp

示例8: libFunc

int libFunc()
{
  MyStack s;
  return s.top();
}
开发者ID:MagnusTiberius,项目名称:code,代码行数:5,代码来源:library.cpp


注:本文中的MyStack::top方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。