Solution LinkedList
Solution LinkedList
Linked Lis
Exercise. Create the project LinkedList and add these classes, following the hierarchy.
We will use them later
LinkedListException(String message)
super(message)
InvalidIndexException(String message)
super(message)
-1-
}
ObjectNotFoundException(String message)
super(message)
EmptyListException(String message)
super(message)
1.- Create the class LinkedList. It will represent a linked list of objects
-2-
}
fi
{
fi
The class LinkedList will have the following elds: rstElement, lastElement (pointers to
the rst and last elements of the list). So, the type will be Element
You will use the system of Exceptions you created in the previous exercise:
Test your class with a MainClass that creates a LinkedList and adds some Strings to the
beginning and the end of the list. Print the list, extract objects, print again, etc
-3-
}
fi
}
fi
{
fi
{
fi
.
fi
t
public LinkedList()
rst = null
last = null
System.out.print(e.getObject())
e = e.getNext()
System.out.println()
-4-
}
fi
fi
}
fi
fi
fi
}
fi
;
fi
;
fi
fi
;
if ( rst == null)
throw new EmptyListException()
if (e == null)
throw new ObjectNotFoundException();
if ( rst == last)
rst = null
last = null
} else
if ( rst == e)
rst = e.getNext()
if (last == e)
last = previous
previous.setNext(e.getNext())
e.delete()
-5-
}
fi
fi
fi
fi
{
fi
fi
}
fi
;
fi
fi
)
fi
;
Element e = rst
int cont = 0
while ((e != null) && (cont < i))
e = e.getNext()
cont++
return cont
-6-
}
fi
{
fi
fi
;
-7-
}
-8-