-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stack.rs
55 lines (44 loc) · 1.15 KB
/
Stack.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Define a Stack struct
struct Stack<T> {
elements: Vec<T>,
}
impl<T> Stack<T> {
// Create a new stack
fn new() -> Self {
Stack { elements: Vec::new() }
}
// Push an element onto the stack
fn push(&mut self, item: T) {
self.elements.push(item);
}
// Pop an element from the stack (if available)
fn pop(&mut self) -> Option<T> {
self.elements.pop()
}
// Peek at the top element without removing it
fn peek(&self) -> Option<&T> {
self.elements.last()
}
// Check if the stack is empty
fn is_empty(&self) -> bool {
self.elements.is_empty()
}
// Get the size of the stack
fn size(&self) -> usize {
self.elements.len()
}
}
fn main() {
let mut stack: Stack<i32> = Stack::new();
// Push elements onto the stack
stack.push(10);
stack.push(20);
stack.push(30);
println!("Stack size: {}", stack.size());
println!("Top element: {:?}", stack.peek());
// Pop elements off the stack
while !stack.is_empty() {
println!("Popped: {:?}", stack.pop());
}
println!("Stack is empty: {}", stack.is_empty());
}