Listas simplesmente encadeadas

0 respostas
java
K

foi me passado esses exercicio mas não to conseguindo fazer
alguem pode me ajudar por favor

Crie um novo método para a classe List (listas simplesmente encadeadas) que remova primeiro elemento ímpar (valor da chave) da lista, caso exista. Se o elemento for removido o método deve retornar true, caso contrário, retornar false. Utilize a assinatura a seguir:
public boolean remove_impar();

Segue as Classe

<strong>public class List {</strong>

private Node firstNode;

private Node lastNode;

private String name;

public List() {

this( list );

}

public List( String listName ) {

name = listName;

firstNode = lastNode = null;

}

public Node getFirst(){

return firstNode;

}

public Node getLast(){

return lastNode;

}

public  void insertAtFront( Object insertItem ) {

if ( isEmpty() )

firstNode = lastNode = new Node( insertItem );

else

firstNode = new Node( insertItem, firstNode );

}

public  void insertAtBack( Object insertItem ) {

if ( isEmpty() )

firstNode = lastNode = new Node( insertItem );

else{

lastNode.setNext (new Node( insertItem ));

lastNode = lastNode.getNext();

}

}

public  Object removeFromFront() throws EmptyListException {

if ( isEmpty() )

throw new EmptyListException( name );

Object removedItem = firstNode.getData();

if ( firstNode == lastNode )

firstNode = lastNode = null;

else

firstNode = firstNode.getNext();

return removedItem;

}

public  Object removeFromBack() throws EmptyListException {

if ( isEmpty() )

throw new EmptyListException( name );

Object removedItem = lastNode.getData();

if ( firstNode == lastNode )

firstNode = lastNode = null;

else {

Node current = firstNode;

while ( current.getNext() != lastNode )

current = current.getNext();

lastNode = current;

current.setNext(null);

}

return removedItem;

}

public boolean isEmpty() {

return firstNode == null;

}

public  void print() {

if ( isEmpty() ) {

System.out.println( "Empty " + name );

return;

}

System.out.print( "The " + name + " is: " );

Node current = firstNode;

while ( current != null ) {

System.out.print( current.getData().toString() + " " );

current = current.getNext();

}

System.out.println( “\n );

}

}
<strong>public class ListTest {</strong>

public static void main( String args[] ) {

List list = new List();

Boolean bool = Boolean.TRUE;

Character character = new Character( $ );

Integer integer = new Integer( 34567 );

String string = hello;

list.insertAtFront( bool );

<a href="//list.print">//list.print</a>();

list.insertAtFront( character );

<a href="//list.print">//list.print</a>();

list.insertAtBack( integer );

<a href="//list.print">//list.print</a>();

list.insertAtBack( string );

<a href="//list.print">//list.print</a>();

try {

Object removedObject = list.removeFromFront();

System.out.println( removedObject.toString() + " removed" );

list.print();

removedObject = list.removeFromFront();

System.out.println( removedObject.toString() + " removed" );

list.print();

removedObject = list.removeFromBack();

System.out.println( removedObject.toString() + " removed" );

list.print();

removedObject = list.removeFromBack();

System.out.println( removedObject.toString() + " removed" );

list.print();

}

catch ( EmptyListException emptyListException ) {

emptyListException.printStackTrace();

}

}

}
<strong>public class EmptyListException extends RuntimeException {</strong>

public EmptyListException() {

this( List );   // call other EmptyListException constructor

}

public EmptyListException( String name ) {

super( name + " is empty" );

}

} // end class EmptyListException
<strong>class Node {</strong>

private Object data;

private Node nextNode;

public Node( Object object ) {

this( object, null );

}

public Node( Object object, Node node ) {

data = object;

nextNode = node;

}

public Object getData()  {

return data; // return Object in this node

}

public void setData (Object element){

data = element;

}

public Node getNext() {

return nextNode; // get next node

}

public void setNext(Node o)

{

nextNode = o;

}

}
Criado 2 de maio de 2018
Respostas 0
Participantes 1