Allora il testo del programma che devo realizzare è quella a questo link:
http://forum.html.it/forum/showthrea...readid=1435948
Il prof ci ha dato la risoluzione delle classi lista e nodo:
lista:
codice:
public class ListaSemplice {
private Nodo testa;
public ListaSemplice(){
testa=null;
}
public void addHead(String info){
Nodo newNodo=new Nodo(info, testa);
testa=newNodo;
}
public void addTail(String info){
if(testa==null){
addHead(info);
}else{
Nodo elem;
elem=findTail();
Nodo newNodo=new Nodo(info,null);
elem.setNext(newNodo);
}
}
public void insert(String info, int pos) throws IndexOutOfBoundsException {
if(pos>size()){
throw new IndexOutOfBoundsException();
}else{
if(testa==null|pos==0){
addHead(info);
}else{
Nodo prec;
prec=findNodo(pos-1);
Nodo newNodo=new Nodo(info,prec);
prec.setNext(newNodo);
}
}
}
public void deleteHead() throws IndexOutOfBoundsException {
if(testa==null){
throw new IndexOutOfBoundsException();
}
testa=testa.getNext();
}
public void deleteTail() throws IndexOutOfBoundsException {
if(testa==null){
throw new IndexOutOfBoundsException();
}
Nodo elem;
elem=testa;
while(elem.getNext().getNext()!=null){
elem=elem.getNext();
}
elem.setNext(null);
}
public void delete(int pos) throws IndexOutOfBoundsException {
if(pos>size()){
throw new IndexOutOfBoundsException();
}else{
if(pos==0){
deleteHead();
}else{
Nodo element;
element=findNodo(pos-1);
element.setNext(element.getNext().getNext());
}
}
}
public void clear(){
testa=null;
}
public String getFirst() throws NullPointerException {
if(testa==null){
throw new NullPointerException();
}
return testa.getInfo();
}
public String getLast() throws NullPointerException {
if(testa==null){
throw new NullPointerException();
}
return findTail().getInfo();
}
public String get(int pos) throws IndexOutOfBoundsException,NullPointerException {
if(testa==null){
throw new NullPointerException();
}
if(pos>size()){
throw new IndexOutOfBoundsException();
}else{
if(pos==0){
return getFirst();
}else{
return findNodo(pos).getInfo();
}
}
}
public void setFirst(String info) throws NullPointerException {
if(testa==null){
throw new NullPointerException();
}
testa.setInfo(info);
}
public void setLast(String info)throws NullPointerException {
if(testa==null){
throw new NullPointerException();
}
findTail().setInfo(info);
}
public void set(String info, int pos) throws IndexOutOfBoundsException,NullPointerException {
if(testa==null){
throw new NullPointerException();
}
if(pos>size()){
throw new IndexOutOfBoundsException();
}else{
if(pos==0){
setFirst(info);
}else{
findNodo(pos).setInfo(info);
}
}
}
@Override
public String toString(){
return testa.toString();
}
public int size(){
Nodo elem;
int i=0;
elem=testa;
while(elem.getNext()!=null){
elem=elem.getNext();
i++;
}
return i;
}
protected Nodo findTail() {
Nodo elem;
elem=testa;
while(elem.getNext()!=null){
elem=elem.getNext();
}
return elem;
}
protected Nodo findNodo(int pos) throws IndexOutOfBoundsException,NullPointerException {
if(testa==null){
throw new NullPointerException();
}
if(pos>size()){
throw new IndexOutOfBoundsException();
}else{
Nodo element;
element=testa;
for(int i=0;i<pos;i++){
element=element.getNext();
}
return element;
}
}
}
nodo:
codice:
public class Nodo {
private String info;
private Nodo next;
public Nodo(){
this.info=null;
this.next=null;
}
public Nodo(String dato, Nodo next){
this.info = dato;
this.next = next;
}
public String getInfo(){
return info;
}
public Nodo getNext(){
return next;
}
public void setInfo(String dato){
this.info=dato;
}
public void setNext(Nodo next){
this.next=next;
}
@Override
public String toString(){
String s;
s=this.info+" "+this.next;
return s;
}
}
Solo che io non ho capito come ha fatto queste due classi qualcuno sarebbe così gentile da spiegarmele, grazie!
Questa volta non chiedo una risoluzione ma una spiegazione, grazie!