-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.rs
More file actions
59 lines (49 loc) · 1.03 KB
/
Copy pathstack.rs
File metadata and controls
59 lines (49 loc) · 1.03 KB
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
56
57
58
59
/*!
* stack.rs
*
* Description: Stack implemenation in Rust
*
* Author: Int Main
* Created: <Date created>
*
* Modified By: <Name of person modifying the file>
* Last Modified: <Date of last modification>
*/
// Rest of the code goes here...
struct Stack<T> {
data: Vec<T>,
}
impl<T> Stack<T> {
fn new() -> Self {
Stack { data: Vec::new() }
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
fn push(&mut self, item: T) {
self.data.push(item);
}
fn pop(&mut self) -> Option<T> {
self.data.pop()
}
fn peek(&self) -> Option<&T> {
self.data.last()
}
}
//Driver function
fn main()
{
let mut stack: Stack<i32> = Stack::new();
stack.push(1);
stack.push(2);
stack.push(3);
println!("Top of the stack is: {}", stack.peek().unwrap());
stack.push(4);
stack.push(5);
stack.push(6);
while stack.is_empty() != true
{
let item = stack.pop();
println!("Popped item: {}", item.unwrap());
}
}