c++ - I met some strange errors when I practiced in LeetCode -


i try use stack implement queues,description. solution below:

class myqueue { public:     /** initialize data structure here. */     stack<int> my_stack;     myqueue() {      }      /** push element x of queue. */     void push(int x) {         stack<int> my_new_stack;         my_new_stack.push(x);         for(int = 0; < my_stack.size();i++){             my_new_stack.push(my_stack.top());             my_stack.pop();         }         for(int = 0; i< my_new_stack.size();i++){             my_stack.push(my_new_stack.top());             my_new_stack.pop();         }     }      /** removes element in front of queue , returns element. */     int pop() {  // queue empty         int temp = my_stack.top();          my_stack.pop();         return temp;     }      /** front element. */     int peek() {         return my_stack.top();     }      /** returns whether queue empty. */     bool empty() {         return my_stack.empty(); ;     }   };  /**  * myqueue object instantiated , called such:  * myqueue obj = new myqueue();  * obj.push(x);  * int param_2 = obj.pop();  * int param_3 = obj.peek();  * bool param_4 = obj.empty();  */ 

my test case

["myqueue","empty","push","push","push","pop","pop","pop"]
[[],[],[1],[2],[3],[],[],[]]

while result [null,true,null,null,null,1,0,80] true result [null,true,null,null,null,1,2,3]

i know awkward in efficiency, have no idea 0 , 80 from.

thank kind help!

look @ this:

for(int = 0; < my_stack.size();i++){     my_new_stack.push(my_stack.top());     my_stack.pop(); } 

and think yourself: result of i < my_stack.size() condition each time through loop. hint: it's not original size of stack.

you far better off transferring my_stack my_new_stack long my_stack wasn't empty!

and, yes, word emphasised reason, nudge nudge wink wink :-)


i'd point out make scheme little more efficient. maintain stack in state it's primed extraction always. think of happen if insert thousand items before extracting any.

that's two thousand reversals of items on stack.

far better store current mode (insert or extract) , reverse when need to.

for example, see following pseudo-code:

def init():     # initialise in insert mode.      stack = []     mode = mode_insert  def reversestack():     # transfer items reverse order.      newstack = []     while not stack.empty():         newstack.push(stack.top())         stack.pop()      # use reversed stack now.      stack = newstack      # change mode.      if mode == mode_insert:         mode = mode_extract     else mode = mode_insert  def insert(item):     # put stack in right state , add item.      if mode == mode_extract:         reversestack()     stack.push(item)  def extract():     # put stack in right state item out.      if mode == mode_insert:         reversestack()     item = stack.top()     stack.pop()     return item 

this substantially more efficient if have long runs of similar operations , marginally less efficient if you're alternating between insertion , extraction.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -