use std::collections::VecDeque;
struct Stack {
queue: VecDeque<i32>,
}
impl Stack {
fn new() -> Self {
Stack { queue: VecDeque::new() }
}
fn push(&mut self, x: i32) {
self.queue.push_back(x);
let size = self.queue.len();
for _ in 0..size - 1 {
if let Some(val) = self.queue.pop_front() {
self.queue.push_back(val);
}
}
}
fn pop(&mut self) -> Option<i32> {
self.queue.pop_front()
}
fn print_stack(&self) {
for val in &self.queue {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(10);
stack.push(20);
stack.push(30);
stack.print_stack();
stack.pop();
stack.print_stack();
}The stack is implemented using one queue by pushing the new element and then rotating the queue so that the new element is at the front. This makes pop() remove the most recently pushed element.
After pushing 10, 20, 30, the queue order is 30, 20, 10. Popping removes 30, leaving 20, 10.
use std::collections::VecDeque;
struct Stack {
q1: VecDeque<i32>,
q2: VecDeque<i32>,
}
impl Stack {
fn new() -> Self {
Stack {
q1: VecDeque::new(),
q2: VecDeque::new(),
}
}
fn push(&mut self, x: i32) {
self.q2.push_back(x);
while let Some(val) = self.q1.pop_front() {
self.q2.push_back(val);
}
std::mem::swap(&mut self.q1, &mut self.q2);
}
fn pop(&mut self) -> Option<i32> {
self.q1.pop_front()
}
fn print_stack(&self) {
for val in &self.q1 {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(5);
stack.push(15);
stack.push(25);
stack.print_stack();
stack.pop();
stack.print_stack();
stack.push(35);
stack.print_stack();
}Push adds the new element to q2, then moves all elements from q1 to q2, then swaps q1 and q2. This keeps the newest element at the front of q1.
After pushing 5, 15, 25, q1 is [25, 15, 5]. Popping removes 25, leaving [15, 5]. Pushing 35 results in [35, 15, 5].
use std::collections::VecDeque;
struct Stack {
queue: VecDeque<i32>,
}
impl Stack {
fn new() -> Self {
Stack { queue: VecDeque::new() }
}
fn push(&mut self, x: i32) {
self.queue.push_back(x);
for i in 0..self.queue.len() {
if let Some(val) = self.queue.pop_front() {
self.queue.push_back(val);
}
}
}
fn pop(&mut self) -> Option<i32> {
self.queue.pop_front()
}
}
fn main() {
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
stack.pop();
}The loop uses the queue length as the upper bound, but the queue length changes inside the loop because elements are popped and pushed back. This causes the loop to never end, resulting in an infinite loop.
use std::collections::VecDeque;
struct Stack {
q1: VecDeque<i32>,
q2: VecDeque<i32>,
}
impl Stack {
fn new() -> Self {
Stack {
q1: VecDeque::new(),
q2: VecDeque::new(),
}
}
fn push(&mut self, x: i32) {
self.q2.push_back(x);
while let Some(val) = self.q1.pop_front() {
self.q2.push_back(val);
}
std::mem::swap(&mut self.q1, &mut self.q2);
}
fn pop(&mut self) -> Option<i32> {
self.q1.pop_front()
}
fn print_internal_queue(&self) {
for val in &self.q1 {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(100);
stack.push(200);
stack.pop();
stack.push(300);
stack.print_internal_queue();
}After pushing 100 and 200, q1 is [200, 100]. Popping removes 200, leaving [100]. Pushing 300 moves 300 to front, then 100 behind, so q1 is [300, 100].
In this implementation, push inserts the new element into the empty queue and then moves all elements from the other queue to it, making push O(n). Pop simply removes from the front of the queue, which is O(1).
