Share my c++ code


  • 0
    A
    class Stack 
    {
    public:
    // Push element x onto stack.
    queue <int> q;
    
    void push(int x)
    {
    	q.push(x);
    }
    
    // Removes the element on top of the stack.
    void pop()
    {
    	size_t i = 0;
    	size_t size = q.size();
    	for (i = 0; i < size - 1; i++)
    	{				
    		q.push(q.front());
    		q.pop();
    	}
    	q.pop();
    }
    
    // Get the top element.
    int top()
    {
    	return (q.back());
    }
    
    // Return whether the stack is empty.
    bool empty()
    {
    	return q.empty();
    }
    };

Log in to reply
 

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