Решение на упр.04 задача 1 от Петър Самичков
Резултати
- 4 точки от тестове
- 0 бонус точки
- 4 точки общо
- 0 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::fmt::Display;
trait Collection<T> {
fn push(&mut self, item: T);
fn pop(&mut self) -> Option<T>;
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
}
struct Deque<T>{
container: Vec<T>
}
impl <T> Collection<T> for Deque<T> {
fn push(&mut self , item:T){
self.container.push(item);
}
fn pop(&mut self) -> Option<T>{
self.container.pop()
}
fn is_empty(&self) -> bool{
self.container.is_empty()
}
fn len(&self) -> usize{
self.container.len()
}
}
impl <T> Deque<T> {
fn pop_front(&mut self) -> Option<T>{
if !self.container.is_empty(){
Some(self.container.remove(0))
}
else {
None
}
}
fn pop_back(&mut self)-> Option<T>{
self.pop()
}
fn push_front(&mut self, data: T){
self.container.insert(0, data);
}
fn push_back(&mut self , data: T){
self.push(data);
}
}
struct Queue<T>{
container:Vec<T>
}
impl <T> Collection<T> for Queue<T>{
fn push(&mut self , item:T){
self.container.push(item);
}
fn pop(&mut self) -> Option<T>{
if !self.container.is_empty(){
Some(self.container.remove(0))
}
else {
None
}
}
fn is_empty(&self) -> bool{
self.container.is_empty()
}
fn len(&self) -> usize{
self.container.len()
}
}
struct Stack<T>{
container: Vec<T>
}
impl <T> Collection<T> for Stack<T> {
fn push(&mut self , item:T){
self.container.push(item);
}
fn pop(&mut self) -> Option<T>{
self.container.pop()
}
fn is_empty(&self) -> bool{
self.container.is_empty()
}
fn len(&self) -> usize{
self.container.len()
}
}
enum data_struct<T> {
Deque(Deque<T>),
Stack(Stack<T>),
Queue(Queue<T>)
}
fn print_data_struct<T:Display>(someD: &mut data_struct<T>){
match someD{
(data_struct::Deque(dq)) => {
println!("Deque");
print!("<->");
for i in &dq.container {
print!("{} <-> " , i);
}
}
(data_struct::Stack(st))=>{
println!("Stack");
for i in &st.container{
print!("{} -> " , i);
}
}
(data_struct::Queue(q))=>{
println!("Queue");
for i in &q.container{
print!(" -> {}" , i);
}
}
}
}
fn main(){
let mut q = Queue {container:Vec::new()};
q.push(1);
q.push(23);
q.push(48);
q.push(2);
let ds = &mut data_struct::Queue(q);
print_data_struct(ds);
}
