Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá pessoal, como posso fazer uma função para remover elementos da minha lista?
<?php
$lista = new Lista();
$lista->add( new Cliente( 'Guedes' ) );
echo $lista->get()->getName() , PHP_EOL."<br>";;
?>
<?php
interface DoubleLinkedListItem {
public function hasNext();
public function hasPrevious();
public function next();
public function previous();
public function setNext( DoubleLinkedListItem $next );
public function setPrevious( DoubleLinkedListItem $previous );
}
abstract class AbstractListItem implements DoubleLinkedListItem {
/**
* @var ListItem
*/
private $next;
/**
* @var ListItem
*/
private $previous;
public function hasNext(){
return !is_null( $this->next );
}
public function hasPrevious(){
return !is_null( $this->previous );
}
public function next(){
if ( $this->hasNext() ){
return $this->next;
} else {
throw new RuntimeException( 'Não temos um próximo' );
}
}
public function previous(){
if ( $this->hasPrevious() ){
return $this->previous;
} else {
throw new RuntimeException( 'Não temos um anterior' );
}
}
public function setNext( DoubleLinkedListItem $next ){
$this->next = $next;
}
public function setPrevious( DoubleLinkedListItem $previous ){
$this->previous = $previous;
}
}
interface Clients {
public function getName();
// public function getAddress();
}
class Cliente extends AbstractListItem implements Clients {
private $name;
public function __construct( $name ){
$this->name = $name;
}
public function getName(){
return $this->name;
}
}
class DoubleLinkedList {
private $first;
private $last;
private $current;
public function add( DoubleLinkedListItem $item ){
if ( is_null( $this->first ) ){
$this->first = $item;
} elseif ( !is_null( $this->last ) ){
$item->setPrevious( $this->last );
$this->last->setNext( $item );
}
$this->last = $item;
}
public function getFirst(){
if ( !is_null( $this->first ) ){
return $this->first;
} else {
throw new RuntimeException( 'Lista vazia.' );
}
}
public function getLast(){
if ( !is_null( $this->last ) ){
return $this->last;
} else {
throw new RuntimeException( 'Lista vazia.' );
}
}
public function get(){
if ( is_null( $this->current ) ){
$this->current = $this->getFirst();
} else {
$this->current = $this->current->next();
}
return $this->current;
}
}
?>Carregando comentários...