Skip to content

Commit 8e6bc2b

Browse files
committed
[rust] Adiciona estrutura de dados pilha
1 parent 7dfbcfa commit 8e6bc2b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
4343
| [Lista Duplamente Encadeada][23] | [C/C++](./src/c/ListaDuplamenteEncadeada.c) | [Java](./src/java/ListaDuplamenteEncadeada.java) | [Python](./src/python/lista_duplamente_encadeada.py) | Go | [Ruby](./src/ruby/Lista_duplamente_encadeada.rb)| [JS](./src/javascript/ListaDuplamenteEncadeada.js) | Pascal | Swift | Rust |
4444
| [Lista Ligada Não Ordenada][24] | [C/C++](./src/c/ListaLigadaNaoOrdenada.c) | Java | Python | [Go](src/go/listasequencialnaoordenada/listaSequencialNaoOrdenada.go) | Ruby | JS | Pascal | Swift | Rust |
4545
| Lista Sequencial Ordenada | [C/C++](./src/c/ListaSequencialOrdenada.c) | Java | [Python](./src/python/lista_sequencial_ordenada.py) | [Go](src/go/listasequencialordenada/listaSequencialOrdenada.go) | Ruby | JS | Pascal | Swift | Rust |
46-
| [Pilha][31] | [C/C++](./src/c/Pilha.c) | [Java](./src/java/Pilha.java) | [Python](./src/python/pilha.py) | Go | [Ruby](./src/ruby/Pilha.rb) | [JS](./src/javascript/Pilha.js) | [Pascal](./src/pascal/pilha.pas) | Swift | Rust |
46+
| [Pilha][31] | [C/C++](./src/c/Pilha.c) | [Java](./src/java/Pilha.java) | [Python](./src/python/pilha.py) | Go | [Ruby](./src/ruby/Pilha.rb) | [JS](./src/javascript/Pilha.js) | [Pascal](./src/pascal/pilha.pas) | Swift | [Rust](./src/rust/pilha.rs) |
4747
| Pilha Ligada Dinâmica | [C/C++](./src/c/PilhaLigadaDinamica.c) | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |
4848

4949
| Algoritmos de Ordenação | C/C++ | Java | Python | Go | Ruby | JS | Pascal | Swift | Rust |

src/rust/pilha.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#[derive(Debug)]
2+
struct Pilha<T> {
3+
pilha: Vec<T>
4+
}
5+
6+
impl<T> Pilha<T> {
7+
fn new() -> Self {
8+
Pilha { pilha: Vec::new() }
9+
}
10+
11+
fn length(&self) -> usize {
12+
self.pilha.len()
13+
}
14+
15+
fn push(&mut self, item: T) {
16+
self.pilha.push(item)
17+
}
18+
19+
fn pop(&mut self) -> Option<T> {
20+
self.pilha.pop()
21+
}
22+
23+
fn is_empty(&self) -> bool {
24+
self.pilha.is_empty()
25+
}
26+
27+
fn peek(&self) -> Option<&T> {
28+
self.pilha.first()
29+
}
30+
}
31+
32+
fn main() {
33+
let mut pilha: Pilha<i32> = Pilha::<i32>::new();
34+
pilha.push(1);
35+
pilha.push(2);
36+
println!("{:?}", pilha);
37+
pilha.pop();
38+
println!("{:?}", pilha);
39+
println!("length: {:?}, is empty? {:?}", pilha.length(), pilha.is_empty());
40+
pilha.push(3);
41+
println!("{:?}", pilha.peek());
42+
}

0 commit comments

Comments
 (0)