Algorithm To Push And Pop In Python