HackerRank C++ Solution – Queue using Two Stacks

Pubblicato il: 19 settembre 2022
sul canale di: JustWriteTheCode
977
15

I JustWriteTheCode of the solution to the "Queue using Two Stacks" problem present on HackerRank (1 Week Preparation Kit - Day 5).

Programming Language: C++.

Problem:
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed.
A basic queue has the following operations:
Enqueue: add a new element to the end of the queue.
Dequeue: remove the element from the front of the queue and return it.
In this challenge, you must first implement a queue using two stacks. Then process q queries, where each query is one of the following 3 types:
1: Enqueue element x into the end of the queue.
2: Dequeue the element at the front of the queue.
3: Print the element at the front of the queue.

Input Format:
The first line contains a single integer, q, denoting the number of queries.
Each line i of the q subsequent lines contains a single query in the form described in the problem statement above. All three queries start with an integer denoting the query type, but only query 1 is followed by an additional space-separated value, x, denoting the value to be enqueued.

Output Format:
For each query of type 3, print the value of the element at the front of the queue on a new line.


In questa pagina del sito puoi guardare il video online HackerRank C++ Solution – Queue using Two Stacks della durata di ore minuti seconda in buona qualità , che l'utente ha caricato JustWriteTheCode 19 settembre 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 977 volte e gli è piaciuto 15 spettatori. Buona visione!