+ 1
I have some questions related to queue data structure(using c++)
1). Suppose a Queue is maintained by circular array queue with N=12 memory cells. Find the number of elements in queue if Front=4 and Rear=8. 2). A queue Q containing n items and an empty stack S are given. It is required to transfer all the items from the queue to the stack, so that the item at the front of the queue is on the top of the stack, and the order of all the other items is preserved. Show this how this can be done in O(n) time using only a constant amount of additional storage. Note that the only operations which can be performed on the queue and stack are Delete, Insert, Push and Pop. Do not assume any implementation of the queue or stack.
3 ответов
+ 1
Akib Reza Well that is a full question
0
Your first question is unclear. Can you explain a bit more?
- 1
I don't really get the first one.
The second one can be solved with another stack with O(n) additional space.
stack s1, s2;
Pop from the queue
push to s2
pop from s2
push to s1
s1 is the answer
O(n) time and O(n) extra space