Cola Pila
Clase List:
package Listas;
public class List {
private ListNode firstNode;
private ListNode lastNode;
private String name;
public List(){this("List");
}
public List(String listname){
name=listname;
firstNode=lastNode=null;
}
public void insertAtFront(Object insertItem){if(isEmpty())
firstNode=lastNode=new ListNode(insertItem);
else
firstNode=new ListNode(insertItem,firstNode);
}
public void insertAtBack(Object insertItem){if(isEmpty())
firstNode=lastNode=new ListNode(insertItem);
else
lastNode=lastNode.nextnode=new ListNode(insertItem);
}
public ObjectremoveFromFront()throws EmptyListException{
if(isEmpty())
throw new EmptyListException(name);
Object removedItem=firstNode.data;
if(firstNode==lastNode)firstNode=lastNode=null;
else
firstNode=firstNode.nextnode;
return removedItem;
}
public Object removeFromBack()throws EmptyListException{
if(isEmpty())throw new EmptyListException(name);
Object removedItem=lastNode.data;
if(firstNode==lastNode)
firstNode=lastNode=null;
else{ListNode current=firstNode;
while(current.nextnode!=lastNode)
current=current.nextnode;
lastNode=current;
current.nextnode=null;
}
returnremovedItem;
}
public boolean isEmpty(){
return firstNode==null;
}
public void print(){
if(isEmpty()){
System.out.printf("Empty%s\n",name);return;
}
System.out.printf("The %s is: ",name);
ListNode current=firstNode;
while(current!=null){
System.out.printf("%s ",current.data);...
Regístrate para leer el documento completo.