#[cfg(test)]
mod unit_test;
use std::collections::LinkedList;
use std::mem::replace;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node<T> {
item: T,
next: Option<Box<Node<T>>>,
}
impl<T> Node<T> {
pub fn item(&self) -> &T {
&self.item
}
pub fn item_mut(&mut self) -> &mut T {
&mut self.item
}
pub fn next(&self) -> &Option<Box<Node<T>>> {
&self.next
}
pub fn next_mut(&mut self) -> &mut Option<Box<Node<T>>> {
&mut self.next
}
}
#[derive(Debug, Default, Clone)]
pub struct Stack<T> {
first: Option<Box<Node<T>>>,
len: usize,
}
impl<T> Stack<T> {
pub fn new() -> Self {
Self {
first: None,
len: 0,
}
}
pub fn init(s: T) -> Self {
let node = Node {
item: s,
next: None,
};
Self {
first: Some(Box::new(node)),
len: 1,
}
}
pub fn first(&self) -> &Option<Box<Node<T>>> {
&self.first
}
pub fn first_mut(&mut self) -> &mut Option<Box<Node<T>>> {
&mut self.first
}
pub fn is_empty(&self) -> bool {
self.first.is_none()
}
pub fn len(&self) -> usize {
self.len
}
}
impl<T: Clone> Stack<T> {
pub fn pop(&mut self) -> Option<T> {
match self.first {
Some(ref node) => {
let item = node.item.clone();
self.first = node.next.clone();
self.len -= 1;
Some(item)
}
None => panic!("cannot pop, stack is empty"),
}
}
pub fn push(&mut self, s: T) {
let oldfirst = self.first.clone();
let new_node = Node {
item: s,
next: oldfirst,
};
self.first = Some(Box::new(new_node));
self.len += 1;
}
}
#[derive(Debug, Clone)]
pub struct ListStack<T> {
list: LinkedList<T>,
}
impl<T> Default for ListStack<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> ListStack<T> {
pub fn new() -> Self {
Self {
list: LinkedList::new(),
}
}
pub fn init(s: T) -> Self {
let mut res = Self {
list: LinkedList::new(),
};
res.push(s);
res
}
pub fn is_empty(&self) -> bool {
self.list.is_empty()
}
pub fn len(&self) -> usize {
self.list.len()
}
pub fn pop(&mut self) -> Option<T> {
self.list.pop_back()
}
pub fn push(&mut self, element: T) {
self.list.push_back(element)
}
}
#[derive(Debug, Clone)]
pub struct VecStack<T> {
n: usize,
vec: Vec<Option<T>>,
}
impl<T> Default for VecStack<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> VecStack<T> {
pub fn new() -> Self {
Self {
n: 0,
vec: vec![None],
}
}
pub fn with_capacity(capacity: usize) -> Self {
if capacity > 0 {
let mut vector = Vec::with_capacity(capacity);
for _ in 0..capacity {
vector.push(None);
}
Self { n: 0, vec: vector }
} else {
panic!("capacity shoul be > 0");
}
}
pub fn is_empty(&self) -> bool {
self.n == 0
}
pub fn len(&self) -> usize {
self.n
}
pub fn push(&mut self, obj: T) {
if self.n < self.vec.len() {
let _ = replace(&mut self.vec[self.n], Some(obj));
self.n += 1;
if self.n == self.vec.len() {
self.double();
}
} else {
panic!("cannot push, stack is full or has capacity 0");
}
}
pub fn pop(&mut self) -> Option<T> {
if self.n > 0 && self.n <= self.vec.len() {
let elt = replace(&mut self.vec[self.n - 1], None);
self.n -= 1;
if self.n <= self.vec.len() / 4 {
self.halve();
}
elt
} else {
panic!("cannot pop, stack is empty");
}
}
fn double(&mut self) {
let mut vector = Vec::with_capacity(self.vec.len());
for _ in 0..self.vec.len() {
vector.push(None);
}
self.vec.append(&mut vector);
}
fn halve(&mut self) {
self.vec.truncate(self.vec.len() / 2);
}
}