Validating Credit Card Numbers

  • + 2 comments

    Could you please share your solution,if had passed all the test cases? Here is my solution which is bit more efficient than the one suggested in the book; yet it has not passed all the test cases.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <queue>
    using namespace std;
    
    class MyQueue {
        public:
            stack<int> stack_newest_on_top, stack_oldest_on_top;
            void push(int x) {
                while(true != stack_oldest_on_top.empty()) {
                    int tmp = stack_oldest_on_top.top();
                    stack_oldest_on_top.pop();
                    stack_newest_on_top.push(tmp);
                }
                stack_newest_on_top.push(x);
            }
    
            void pop() {
                if(true != stack_newest_on_top.empty()) {
                    while(true != stack_newest_on_top.empty()) {
                        int tmp = stack_newest_on_top.top();
                        stack_newest_on_top.pop();
                        stack_oldest_on_top.push(tmp);
                    }
                    stack_oldest_on_top.pop();
                }
                else if (true != stack_oldest_on_top.empty()) {
                    stack_oldest_on_top.pop();
                }
            }
    
            int front() {
                int rt_value = 0;
                if(true != stack_newest_on_top.empty()) {
                    while(true != stack_newest_on_top.empty()) {
                        int tmp = stack_newest_on_top.top();
                        stack_newest_on_top.pop();
                        stack_oldest_on_top.push(tmp);
                    }
                    stack_oldest_on_top.pop();
                }
                else if (true != stack_oldest_on_top.empty()) {
                    rt_value = stack_oldest_on_top.top();
                }
                return rt_value;
            }
    };
    
    int main() {
        MyQueue q1;
        int q, type, x;
        cin >> q;
    
        for(int i = 0; i < q; i++) {
            cin >> type;
            if(type == 1) {
                cin >> x;
                q1.push(x);
            }
            else if(type == 2) {
                q1.pop();
            }
            else cout << q1.front() << endl;
        }
        return 0;
    }
    

    image