use 2 stacks to simulate a queue

class Stack{
private:
    int cur = 0;
    int elem[2000001];
public:
    void push(int n);
    int pop();
    int peek();
    int size();
};

void Stack::push(int n){
    elem[cur] = n;
    cur++;
}

int Stack::pop(){
    cur--;
    return elem[cur + 1];
}

int Stack::peek(){
    return elem[cur - 1];
}

int Stack::size(){
    return cur;
}

class Queue{
private:
    Stack s1, s2;
public:
    void enqueue(int n);
    int dequeue();
    int peek();
    int size();
};

void Queue::enqueue(int n){
    if(s1.size() == 0){
        while(s2.size()){
            s1.push(s2.pop());
        }
    }

    s1.push(n);
}

int Queue::dequeue(){
    if(s2.size() == 0){
        while(s1.size()){
            s2.push(s1.pop());
        }
    }
    return s2.pop();
}

int Queue::peek(){
    if(s2.size() == 0){
        while(s1.size()){
            s2.push(s1.pop());
        }
    }

    return s2.peek();
}

int Queue::size(){
    return s1.size()? s1.size() : s2.size();
}
时间: 2024-08-25 19:26:33

use 2 stacks to simulate a queue的相关文章

232. Implement Queue using Stacks && 225. Implement Stack using Queues

232. Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whet

Coursera Algorithms week2 栈和队列 Interview Questions: Queue with two stacks

题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations. 题目要求用栈实现队列的所有操作. 1 import java.util.Iterator; 2 import edu.princeton.cs.algs4.Stack; 3 public class QueueWith2Stacks<Item>{

LintCode-Implement Queue by Stacks

As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the va

Lintcode40 Implement Queue by Two Stacks solution 题解

[题目描述] As the title described, you should only use two stacks to implement a queue's actions.The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.Both pop and top methods should return t

Implement Queue by Two Stacks

As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the va

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

Implement Queue using Two Stacks

/* how to use two stacks to implement the queue: offer, poll, peek,size, isEmpty offer(3) offer(2) poll() offer(1) peek() offer(6) poll() poll() 3 2 2 1 in (3 2) (1 6) out (2) (3) 6 (1) stack1(in): is the only stack to store new elements when adding

Implementing Stacks Using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

Implementing Stack Using Queue

Question: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must