Skip to content

Commit aa83fec

Browse files
Implement Stacks and Queues in Rust. (#924)
* Implement Stack * Implement Queue * Update markdown page * Add comments with output of prints * Use equivalent methods instead of reimplementing them * Add SConscript file Co-authored-by: James Schloss <[email protected]>
1 parent a3276be commit aa83fec

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

CONTRIBUTORS.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This file lists everyone, who contributed to this repo and wanted to show up her
5151
- Vincent Zalzal
5252
- Jonathan D B Van Schenck
5353
- James Goytia
54-
- Sammy Plat
54+
- Sammy Plat
5555
- Jonathan Dönszelmann
5656
- Ishaan Verma
5757
- Delphi1024
@@ -60,4 +60,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
6060
- Ridham177
6161
- Hugo Salou
6262
- Dimitri Belopopsky
63-
+ Henrik Abel Christensen
63+
- Henrik Abel Christensen
64+
- Peanutbutter_Warrior
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::collections::VecDeque;
2+
3+
struct Queue<T> {
4+
list: VecDeque<T>
5+
}
6+
7+
impl<T> Queue<T> {
8+
fn new() -> Self {
9+
Queue{
10+
list: VecDeque::new(),
11+
}
12+
}
13+
14+
// Note that this returns a reference to the value
15+
// This is in contrast to dequeue which gives ownership of the value
16+
fn front(&self) -> Option<&T> {
17+
self.list.front()
18+
}
19+
20+
fn dequeue(&mut self) -> Option<T> {
21+
self.list.pop_front()
22+
}
23+
24+
fn enqueue(&mut self, item: T) {
25+
self.list.push_back(item);
26+
}
27+
28+
fn size(&self) -> usize {
29+
self.list.len()
30+
}
31+
}
32+
33+
fn main() {
34+
let mut i32queue = Queue::new();
35+
36+
i32queue.enqueue(4);
37+
i32queue.enqueue(5);
38+
i32queue.enqueue(6);
39+
40+
println!("{:?}", i32queue.dequeue().unwrap()); // 4
41+
println!("{:?}", i32queue.size()); // 2
42+
println!("{:?}", i32queue.front().unwrap()); // 5
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Import('*')
2+
from pathlib import Path
3+
4+
dirname = Path.cwd().parents[1].stem
5+
6+
env.rustc(f'#/build/rust/stack', '#/contents/stacks_and_queues/code/rust/Stack.rs')
7+
env.Clean('rust', f'#/build/rust/stack.pdb')
8+
9+
env.rustc(f'#/build/rust/queue', '#/contents/stacks_and_queues/code/rust/Queue.rs')
10+
env.Clean('rust', f'#/build/rust/queue.pdb')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Stack<T> {
2+
list: Vec<T>
3+
}
4+
5+
impl<T> Stack<T> {
6+
fn new() -> Self {
7+
Stack {
8+
list: Vec::new(),
9+
}
10+
}
11+
12+
// Note that this returns a reference to the value
13+
// This is in contrast to pop which gives ownership of the value
14+
fn top(&self) -> Option<&T> {
15+
self.list.last()
16+
}
17+
18+
fn pop(&mut self) -> Option<T> {
19+
self.list.pop()
20+
}
21+
22+
fn push(&mut self, item: T) {
23+
self.list.push(item);
24+
}
25+
26+
fn size(&self) -> usize {
27+
self.list.len()
28+
}
29+
}
30+
31+
fn main() {
32+
let mut i32stack: Stack<i32> = Stack::new();
33+
34+
i32stack.push(4);
35+
i32stack.push(5);
36+
i32stack.push(6);
37+
38+
println!("{:?}", i32stack.pop().unwrap()); // 6
39+
println!("{:?}", i32stack.size()); // 2
40+
println!("{:?}", i32stack.top().unwrap()); // 5
41+
}

contents/stacks_and_queues/stacks_and_queues.md

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Here is a simple implementation of a stack:
2020
[import, lang:"typescript"](code/typescript/stack.ts)
2121
{% sample lang="java" %}
2222
[import, lang:"java"](code/java/Stack.java)
23+
{% sample lang="rust" %}
24+
[import, lang:"rust"](code/rust/Stack.rs)
2325
{% endmethod %}
2426

2527
Here is a simple implementation of a queue:
@@ -28,6 +30,8 @@ Here is a simple implementation of a queue:
2830
[import, lang:"typescript"](code/typescript/queue.ts)
2931
{% sample lang="java" %}
3032
[import, lang:"java" ](code/java/Queue.java)
33+
{% sample lang="rust" %}
34+
[import, lang:"rust" ](code/rust/Queue.rs)
3135
{% endmethod %}
3236

3337

0 commit comments

Comments
 (0)