用堆栈(stack) 实现队列(queue)

原题在这里:

https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=stacks-queues

 

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  queries, where each query is one of the following  types:

  1. 1 x: Enqueue element  into the end of the queue.
  2. 2: Dequeue the element at the front of the queue.
  3. 3: Print the element at the front of the queue.

For example, a series of queries might be as follows:

Function Description

Complete the putpop, and peek methods in the editor below. They must perform the actions as described above.

Input Format

The first line contains a single integer, , the number of queries.

Each of the next  lines contains a single query in the form described in the problem statement above. All queries start with an integer denoting the query , but only query  is followed by an additional space-separated value, , denoting the value to be enqueued.

Constraints

  •  
  •  
  •  
  • It is guaranteed that a valid answer always exists for each query of types  and .

Output Format

For each query of type , return the value of the element at the front of the fifo queue on a new line.

Sample Input

10
1 42
2
1 14
3
1 28
3
1 60
1 78
2
2

Sample Output

14
14

Explanation

 

 

简单来说要求:只能使用2个堆栈(stack)来实现队列queue, 实现队列基本操作的三个功能,

1:在队列的尾部插入新元素 -> push(int x)

2:删除对头的元素 -> pop(), 不需要返回被删除的元素

3:返回队头的元素 -> top().

我的算法:

简单的方法,就每次插入元素,都进行反转,一个是正常的stack, 里面的元素跟队列元素的顺序第颠倒的, 第二个是反转的stack, 也是说里面的元素的顺序是跟队列的顺序是一致的。 这样的时间花销是比较大的。也就是说每次插入都是插入到第一个stack, 然后进行反转给到第二个队列。 每次删除元素,都是在第二个stack上进行删除,然后又重新反转写入到第一个stack当中。

用堆栈(stack) 实现队列(queue)_第1张图片

 

用堆栈(stack) 实现队列(queue)_第2张图片

 

 

高效的方法:

push()函数只管往stack1里加入新元素。不做任何其他事情。

pop()函数,只管将stack2的头元素进行删除, 但是如果stack2为空,就将stack1里面所有的元素存入stack2中,这个过程自然会将stack1的元素顺序进行颠倒,并且会使stack1变为空。

用堆栈(stack) 实现队列(queue)_第3张图片

用堆栈(stack) 实现队列(queue)_第4张图片

#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include

//using namespace std;

class MyQueue {

public:
    std::stack stack_newest_on_top, stack_oldest_on_top;
    void push(int x) {
                    stack_newest_on_top.push(x);
            }

    void pop() {
        if (stack_oldest_on_top.empty())
        {
            while (!stack_newest_on_top.empty())
            {
                stack_oldest_on_top.push(stack_newest_on_top.top());
                stack_newest_on_top.pop();
            }

            stack_oldest_on_top.pop();
        }
        else  stack_oldest_on_top.pop();

    }

    int front() {

        if (stack_oldest_on_top.empty())
        {
            while (!stack_newest_on_top.empty())
            {
                stack_oldest_on_top.push(stack_newest_on_top.top());
                stack_newest_on_top.pop();
            }

        //    stack_oldest_on_top.pop();
        }

     return stack_oldest_on_top.top();

    }
};

int main() {
    MyQueue q1;
    int q, type, x;
    std::cin >> q;

    for (int i = 0; i < q; i++) {
        std::cin >> type;
        if (type == 1) {
            std::cin >> x;
            q1.push(x);
        }
        else if (type == 2) {
            q1.pop();
        }
        else std::cout << q1.front() << std::endl;
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    return 0;
}
 

 

你可能感兴趣的:(算法)