Example #1
0
void char_test()
{
  char next;

  MyStack<char> s;

  cout << "Enter some text\n";

  // read characters in one by one until newline reached

  cin.get(next);
  while (next != '\n') {

    // push latest character

    s.push(next);
    cin.get(next);
  }

  cout << "Written backward that is:\n";

  // output all characters stored in stack

  while (!s.empty())
    cout << s.pop();
  cout << "\n";

}
Example #2
0
void string_test()
{
  string next;
  char c;

  MyStack<string> s;

  cout << "Enter a sentence or two\n";

  // read from terminal word by word

  while (cin >> next) {

    // put latest word into stack

    s.push(next);

    // was that the last word on the line?

    c = cin.get();

    if (c == '\n')
      break;
    else
      cin.putback(c);

  }

  cout << "Written backward that is:\n";
  
  while (!s.empty())
    cout << s.pop() << " ";
  cout << "\n";

}
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");
}
Example #4
0
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;
}
Example #5
0
void testStack() {
	MyStack s;
	for(int i = 1; i <= 10; i++)
		s.push(i);
	MyStack s2 = s;
	cout << "s:\n";
	while (!s.empty())
		cout << s.pop() << endl;
	MyStack s3;
	s3 = s;
	for(int i = 11; i <= 20; i++)
			s.push(i);
	cout << "s2:\n";
	while (!s2.empty())
		cout << s2.pop() << endl;
	cout << "Живи сме!\n";
}
Example #6
0
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;
}