Skip to content

Implement Stacks and Queues in Rust. #924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Vincent Zalzal
- Jonathan D B Van Schenck
- James Goytia
- Sammy Plat
- Sammy Plat
- Jonathan Dönszelmann
- Ishaan Verma
- Delphi1024
Expand All @@ -60,4 +60,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Ridham177
- Hugo Salou
- Dimitri Belopopsky
+ Henrik Abel Christensen
- Henrik Abel Christensen
- Peanutbutter_Warrior
43 changes: 43 additions & 0 deletions contents/stacks_and_queues/code/rust/Queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::collections::VecDeque;

struct Queue<T> {
list: VecDeque<T>
}

impl<T> Queue<T> {
fn new() -> Self {
Queue{
list: VecDeque::new(),
}
}

// Note that this returns a reference to the value
// This is in contrast to dequeue which gives ownership of the value
fn front(&self) -> Option<&T> {
self.list.front()
}

fn dequeue(&mut self) -> Option<T> {
self.list.pop_front()
}

fn enqueue(&mut self, item: T) {
self.list.push_back(item);
}

fn size(&self) -> usize {
self.list.len()
}
}

fn main() {
let mut i32queue = Queue::new();

i32queue.enqueue(4);
i32queue.enqueue(5);
i32queue.enqueue(6);

println!("{:?}", i32queue.dequeue().unwrap()); // 4
println!("{:?}", i32queue.size()); // 2
println!("{:?}", i32queue.front().unwrap()); // 5
}
10 changes: 10 additions & 0 deletions contents/stacks_and_queues/code/rust/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Import('*')
from pathlib import Path

dirname = Path.cwd().parents[1].stem

env.rustc(f'#/build/rust/stack', '#/contents/stacks_and_queues/code/rust/Stack.rs')
env.Clean('rust', f'#/build/rust/stack.pdb')

env.rustc(f'#/build/rust/queue', '#/contents/stacks_and_queues/code/rust/Queue.rs')
env.Clean('rust', f'#/build/rust/queue.pdb')
41 changes: 41 additions & 0 deletions contents/stacks_and_queues/code/rust/Stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
struct Stack<T> {
list: Vec<T>
}

impl<T> Stack<T> {
fn new() -> Self {
Stack {
list: Vec::new(),
}
}

// Note that this returns a reference to the value
// This is in contrast to pop which gives ownership of the value
fn top(&self) -> Option<&T> {
self.list.last()
}

fn pop(&mut self) -> Option<T> {
self.list.pop()
}

fn push(&mut self, item: T) {
self.list.push(item);
}

fn size(&self) -> usize {
self.list.len()
}
}

fn main() {
let mut i32stack: Stack<i32> = Stack::new();

i32stack.push(4);
i32stack.push(5);
i32stack.push(6);

println!("{:?}", i32stack.pop().unwrap()); // 6
println!("{:?}", i32stack.size()); // 2
println!("{:?}", i32stack.top().unwrap()); // 5
}
4 changes: 4 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Here is a simple implementation of a stack:
[import, lang:"typescript"](code/typescript/stack.ts)
{% sample lang="java" %}
[import, lang:"java"](code/java/Stack.java)
{% sample lang="rust" %}
[import, lang:"rust"](code/rust/Stack.rs)
{% endmethod %}

Here is a simple implementation of a queue:
Expand All @@ -28,6 +30,8 @@ Here is a simple implementation of a queue:
[import, lang:"typescript"](code/typescript/queue.ts)
{% sample lang="java" %}
[import, lang:"java" ](code/java/Queue.java)
{% sample lang="rust" %}
[import, lang:"rust" ](code/rust/Queue.rs)
{% endmethod %}


Expand Down