c++ - How can I remove the oldest element from stack? -


How can I remove the oldest element from the stack instead of throwing fullStackException? Is there no algorithm about it?

Do not confuse yourself with the queue? A heap that allows the removal of the oldest element can be called dequeue. In any case there are many dimensions to this question

I would not say that the removal of the oldest element (a single) stack is possible. Although you can always see the oldest element. Define the variable by placing a variable when the first element is inserted. And just use that variable whenever you will peep.

And if you really want to remove the oldest element and you only have permission to use the stack, then you can do this by using two stacks

in stack 1 All elements include Stack 2 is empty.

pseudocode

  while (! Stack1.empty ()) {stack2.push (stack1.pop ()); } Stack2.pop (); While (! Stack2.empty ()) {stack1.push (); // Or instead of 2 when you can swap stack 1 and stack 2 (swap two variables)}}   

The above code is actually known as 2 stacks you can see it

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -