https://github.com/t-eckert/stack_max
Learning project. `StackMax` is a stack of integers that returns the largest integer in O(1) time.
https://github.com/t-eckert/stack_max
Last synced: about 1 year ago
JSON representation
Learning project. `StackMax` is a stack of integers that returns the largest integer in O(1) time.
- Host: GitHub
- URL: https://github.com/t-eckert/stack_max
- Owner: t-eckert
- Created: 2019-08-04T05:37:02.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-05T01:34:22.000Z (almost 7 years ago)
- Last Synced: 2025-06-04T02:55:22.968Z (about 1 year ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# StackMax
A learning project in Rust. An instance of `StackMax` works as a stack normally does, giving a user access to `push()`, `peek()`, and `pop()` functions. This stack additionally has the ability to return the maximum value in the stack.
This is accomplished by using two `Vec` to handle state inside of `StackMax`. One of these `Vec`, called `numbers`, is a stack of all numbers pushed onto `StackMax`. The other, called `maximums` (though should it be `maxima`?), is a stack of only the maximum numbers pushed to the `numbers` stack.
When a number is pushed to `StackMax`, it is added to the `numbers` stack then compared to the number at the top of the `maximums` stack. If the number being pushed is greater than the value at the top of the `maximums` stack or if the stack is empty, the number is pushed to the `maximums` stack.
When a number is popped from `StackMax`, it is removed from the `numbers` stack and compared to the number at the top of the `maximums` stack. If they are equal, the number at the top of the `maximums` stack is also removed.
## Demo
The StackMax object is initialized.
``` text
StackMax {
numbers:
maximums:
}
```
4 is pushed to StackMax.
``` text
StackMax {
numbers: 4
maximums: 4
}
```
2 is pushed to StackMax.
``` text
StackMax {
numbers: 4, 2
maximums: 4
}
```
6 is pushed to StackMax.
``` text
StackMax {
numbers: 4, 2, 6
maximums: 4, 6
}
```
6 is popped from StackMax.
``` text
StackMax {
numbers: 4, 2
maximums: 4
}
```
2 is popped from StackMax.
``` text
StackMax {
numbers: 4
maximums: 4
}
```
4 is popped from StackMax.
``` text
StackMax {
numbers:
maximums:
}
```