Implement Stack (Ladder)

Description

Implement a stack. You can use any data structure inside a stack except stack itself to implement it.

Example

push(1)
pop()
push(2)
top()  // return 2
pop()
isEmpty() // return true
push(3)
isEmpty() // return false

Lintcode_ladder

Method

  1. x
  2. x

Example

  1. 1
    class Stack {
    public:
     // Push a new item into the stack
     void push(int x) {
         // Write your code here
         stack.push_back(x);
         return;
     }
     // Pop the top of the stack
     void pop() {
         // Write your code here
         if (!isEmpty()) {
             stack.pop_back();
         }
         return;
     }
     // Return the top of the stack
     int top() {
         // Write your code here
         if (isEmpty()) {
             return INT_MIN;
         } else {
             return stack.back(); 
         }
     }
     // Check the stack is empty or not.
     bool isEmpty() {
         // Write your code here
         return stack.empty();
     }
    private:    
     vector<int> stack;
    };
    

Similar problems

x

Tags

x

results matching ""

    No results matching ""