Can some one share some ideas on the error of this code?


  • 0
    N
    class Stack {
    public:
        list<int> l;
        // Push element x onto stack.
        void push(int x) {
            l.push_back(x);
        }
    
        // Removes the element on top of the stack.
        void pop() {
            if(!empty()){
            for(int i=1;i<l.size();i++)
            {
                l.push_back(l.front());
                l.pop_front();
            }
            l.pop_front();}
            else cout<<"There is not element here."<<endl;
        }
    
        // Get the top element.
        int top() {
            if(!empty()){
            for(int i=1;i<l.size();i++)
            {
                l.push_back(l.front());
                l.pop_front();
            }
            l.front();}
            else cout<<"There is not element here."<<endl;
        }
    
        // Return whether the stack is empty.
        bool empty() {
            return l.empty();
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.