Implement Queue using Stack (with Example)
Автор: Techdose
Загружено: 2019-03-15
Просмотров: 21712
This lecture explains two methods to implement queue using stacks only. The first method assumes Enqueue operation to be costly and second method assumes dequeue operation to be costly. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
Algo: EnQueue Costly
enQueue(q, x)
1) While stack1 is not empty, push everything from stack1 to stack2.
2) Push x to stack1 (assuming size of stacks is unlimited).
3) Push everything back to stack1.
Here time complexity will be O(n)
deQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and return it
Here time complexity will be O(1)
CODE LINK: https://drive.google.com/open?id=1B4O...
Algo: DeQueue Costly
enQueue(q, x)
1) Push x to stack1 (assuming size of stacks is unlimited).
Here time complexity will be O(1)
deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from stack1 to stack2.
3) Pop the element from stack2 and return it.
Here time complexity will be O(n)
CODE LINK: https://drive.google.com/open?id=1fqt...
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: