reversing a stack using another stack

preview_player
Показать описание
How reversal is done in a stack using only one additional stack....
By : Anmol Agnihotri (B.tech computer science, Keshav Mahavidyalaya, University of Delhi)
Рекомендации по теме
Комментарии
Автор

best explanation i've seen till date

scienceshorts
Автор

LEGEND ❤❤❤❤❤❤❤❤❤❤ just a mistake at timestamp 0:20 second 5th will go into stack 2 not in stack 1
void reverseStack(stack<int> &input, stack<int> &extra)
{
if(input.size()<=1)
{
return ; // its the base case pretty simple to understand
}

int temp = input.top(); // store the top of the stack 1 in a temp variable and then pop it out of the stack 1
input.pop();

reverseStack(input, extra); // this is the recursive call after this assume that the stack already got reversed then we work on solving the small problem

while(input.size()!=0) // until the input stack is not empty remove element from it and push them into the extra stack
{
int lastelement = input.top();
input.pop();
extra.push(lastelement);
}

extra.push(temp); // pusht the element which we poped first in the extra stack "HERE THE VIDEO CREATOR WENT WRONG IT WILL NOT DIRECTLY ENTER THE ORIGINAL STACK IT WILL FIRST GET PUSHED INTO THE SECOND STACK THEN INTO THE ORIGINAL STACK


while(extra.size()!=0) // now pop the elements from the extra stack into the input stack
{
int lastelement = extra.top();
extra.pop();
input.push(lastelement);
}
} check this for reference

anantsaxena
Автор

u have taken nice example bro...thank so much

gym_
Автор

the guy is in microsoft now ....legend

rishayshandilya
Автор

Thank u so much for the awesome Eg... : )

swarnalisaha
Автор

anmol bro, i can't understood the logic can you help me? pls reply

Vijay-fibm