You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicbooleanpush(Tkey)
{
// this check is only needed due to inconsistent function// descriptionif(key == null)
returnfalse;
// create new nodeMListElement<T> node = newMListElement<T>(key);
// test if new head must be set// or if it should be inserted before headif(getHead() == null || getComp().compare(key, getHead().getKey()) < 0) {
node.setNext(getHead());
setHead(node);
returntrue;
}
// traverse through list to correct positionMListElement<T> it = getHead();
while(it.getNext() != null) {
// check if we found the position. if so insert and returnif(getComp().compare(key, it.getNext().getKey()) < 0) {
node.setNext(it.getNext());
it.setNext(node);
returntrue;
}
it = it.getNext();
}
// insert as last elementit.setNext(node);
returntrue;
}
// clone list elements flat --------------------------------------------------------publicListElement<T> clone(ListElement<T> el)
{
ListElement<T> ret = newListElement<T>(el.getData());
ListElement<T> it = ret;
while(el.hasNext()) {
it.setNext(newListElement<T>(el.next().getData()));
it = it.next();
el = el.next();
}
returnret;
}
// duplicate every second element ---------------------------------------------publicMListElement<T> duplicateEverySecondElement(MListElement<T> head)
{
booleandup = true;
MListElement<T> it = head;
while(it != null) {
if(dup) {
MListElement<T> clone = newMListElement(it.getKey());
clone.setNext(it.getNext());
it.setNext(clone);
it = it.getNext();
}
it = it.getNext();
dup = !dup;
}
returnhead;
}
// clone linked list flat ------------------------------------------------------------// note: the method LinkedList.setSize is not advertised but needed and exists// the core of this function was copied from clone list elements flatpublicLinkedList<T> clone(LinkedList<T> list)
{
LinkedList<T> copy = newLinkedList<T>();
if(list.getFirst() == null)
returncopy;
ListElement<T> el = list.getFirst();
ListElement<T> ret = newListElement<T>(el.getData());
ListElement<T> it = ret;
while(el.hasNext()) {
it.setNext(newListElement<T>(el.next().getData()));
it = it.next();
el = el.next();
}
copy.setFirst(ret);
copy.setLast(it);
copy.setSize(list.size());
returncopy;
}
// remove duplicates ----------------------------------------------------------------------publicListElement<T> removeDuplicates(ListElement<T> head, LinkedList_ComparableComparator<T> comp)
{
if(head == null)
returnhead;
ListElement<T> it = head;
while(it.hasNext()) {
if(comp.compare(it.getData(), it.next().getData()) == 0)
it.setNext(it.next().next());
elseit = it.next();
}
returnhead;
}
// insert single ----------------------------------------------------------------------// make sure to not forget the last 'setLast' check...// make sure to get the idx downcounting rightpublicbooleaninsertSingle(ListElement<T> el, intidx)
{
if(el == null || contains(el))
returnfalse;
if(idx == 0)
returninsertSingleFirst(el);
ListElement<T> it = getFirst();
while(--idx > 0 && it != null) it = it.next();
if(it == null)
returnfalse;
el.setNext(it.next());
it.setNext(el);
setSize(size() + 1);
if(el.next() == null)
setLast(el);
returntrue;
}
// insert single first ----------------------------------------------------------------------publicbooleaninsertSingleFirst(ListElement<T> el)
{
if(contains(el) || el == null)
returnfalse;
el.setNext(getFirst());
setFirst(el);
setSize(size() + 1);
if(el.next() == null)
setLast(el);
returntrue;
}
// insert single last ----------------------------------------------------------------------publicbooleaninsertSingleLast(ListElement<T> el)
{
if(contains(el) || el == null)
returnfalse;
// check for special (empty) caseif(getLast() != null) getLast().setNext(el);
elsesetFirst(el);
el.setNext(null);
setLast(el);
setSize(size() + 1);
returntrue;
}
// insert whole list ------------------------------------------------------------// the hardest beast so far. Would not be surprised if there are still bugs// in it that are just not caught by the unit tests.// Some key points (that were not so clear after reading the exercise) are:// - the list to insert (el) may be invalid, e.g. have loops in itself// - in this case it seems like it should just false be returned and the original// list should stay untouched (at least this works with unit tests)// - the algorithm must assure that no element in el that is already in the listpublicbooleaninsert(ListElement<T> el, intidx)
{
// we probably traverse the whole list here many times...// but i guess it was meant to be this way and who cares about performance anyways// since we are already in java...if(idx == 0)
returninsertFirst(el);
if(contains(el) || el == null)
returnfalse;
// iterate to correct positionListElement<T> it = getFirst();
while(--idx > 0 && it != null) it = it.next();
// index out of boundsif(it == null)
returnfalse;
// store the beginning of the restListElement<T> rest = it.next();
// find the end of the list to insert// also count its size and make sure the list does not// contain any elements already inserted// insert the elements step by step// needed since we cannot know el is a valid// list without loops.intaddSize = 0;
ListElement<T> end = it;
ListElement<T> next = el;
while(next != null) {
++addSize;
end.setNext(next);
next = next.next();
end.next().setNext(rest);
end = end.next();
if(next != null && contains(next)) {
it.setNext(rest);
returnfalse;
}
}
// connect itsetSize(size() + addSize);
// set last if there was no end listif(end.next() == null)
setLast(end);
returntrue;
}
// insert first --------------------------------------------------------------------------------// another really ugly one for the same reasons as the one above// we have to check the inserted list for loops as well as for elements already// in the list while not allowed to use a set for seen elements or sth.// The implemented idea is to insert el element-by-element in the front of// the list after checking that it is not already in there.// Again (this time the unit tests explicity need this behaviour) should the method// fail if el contains loop, although this is nowhere specified in the exercise.// And again el may contain an invalid list, although the documentation states otherwise.publicbooleaninsertFirst(ListElement<T> el)
{
if(el == null || contains(el))
returnfalse;
// set first elementListElement<T> next = el.next();
ListElement<T> oldFirst = getFirst();
// we count the number of elements addedintaddSize = 1;
el.setNext(getFirst());
setFirst(el);
if(oldFirst == null)
setLast(el);
while(next != null) {
// assure that the element is not already in the list// this also checks for loops in el since we already inserted some// elements.if(contains(next)) {
setFirst(oldFirst);
returnfalse;
}
++addSize;
ListElement<T> nextNext = next.next();
// insert next after elnext.setNext(oldFirst);
el.setNext(next);
el.next().setNext(oldFirst);
// check for last elementif(oldFirst == null)
setLast(next);
// variantel = next;
next = nextNext;
}
// increase size by counted number of elementssetSize(size() + addSize);
returntrue;
}
// insert last --------------------------------------------------------------------------------// The base idea (and bad documentation/exercise) the same as above but// the implementation is a bit easier sine we append (more naturally in a linked list)// to the endpublicbooleaninsertLast(ListElement<T> el)
{
if(el == null || contains(el))
returnfalse;
ListElement<T> oldLast = getLast();
intaddSize = 1;
// handle empty 'this' listif(oldLast == null) setFirst(el);
elseoldLast.setNext(el);
setLast(el);
el = el.next();
getLast().setNext(null);
// append all remaining elementswhile(el != null) {
if(contains(el)) {
setLast(oldLast);
oldLast.setNext(null);
returnfalse;
}
++addSize;
getLast().setNext(el);
setLast(el);
el = el.next();
getLast().setNext(null);
}
setSize(size() + addSize);
returntrue;
}
// get -------------------------------------------------------------------------------------publicListElement<T> get(intidx)
{
ListElement<T> it = getFirst();
while(--idx >= 0 && it != null) it = it.next();
return (idx == -1) ? it : null;
}
// remove --------------------------------------------------------------------------------publicListElement<T> remove(intidx)
{
if(idx >= size() || idx < 0)
returnnull;
ListElement<T> it = getFirst();
// check if head should be removedif(idx == 0) {
setFirst(getFirst().next());
if(getLast() == it) setLast(null);
setSize(size() - 1);
returnit;
}
while(--idx > 0 && it != null) it = it.next();
if(it == null || it.next() == null) returnnull;
ListElement<T> ret = it.next();
it.setNext(ret.next());
setSize(size() - 1);
if(it.next() == null) setLast(it);
returnret;
}
// remove first --------------------------------------------------------------------------------publicListElement<T> removeFirst()
{
if(getFirst() == null)
returnnull;
ListElement<T> ret = getFirst();
setFirst(getFirst().next());
setSize(size() - 1);
if(getFirst() == null || getFirst().next() == null)
setLast(getFirst());
returnret;
}
// remove last --------------------------------------------------------------------------------publicListElement<T> removeLast()
{
if(getFirst() == null)
returnnull;
ListElement<T> it = getFirst();
setSize(size() - 1);
if(getFirst() == getLast()) {
setFirst(null);
setLast(null);
returnit;
}
while(it.next().next() != null) it = it.next();
ListElement<T> ret = it.next();
setLast(it);
it.setNext(null);
returnret;
}
ArrayList
// removepublicbooleanremove(inti)
{
ArrayListElement<T> it = getFirst();
while(i >= 0 && it != null) {
if(i < it.getN()) {
for(; i < it.getN() - 1; ++i) {
it.getData()[i] = it.getData()[i + 1];
}
it.decN();
returntrue;
}
i -= it.getN();
it = it.getNext();
}
returnfalse;
}
// insert// holy fuck what am i doing with my lifepublicbooleaninsertAtPosition(intpos, Listobject<T> key)
{
if(pos < 0 || getFirst() == null)
returnfalse;
ArrayListElement<T> it = getFirst();
while(pos >= 0 && it != null) {
// case 2if(pos > it.getN()) {
pos -= it.getN();
it = it.getNext();
continue;
}
// case 3// splitif(it.getN() == it.getData().length) {
ArrayListElement<T> next = newArrayListElement<T>(getArrayLength());
next.setNext(it.getNext());
it.setNext(next);
intitn = it.getN();
for(intj = 0; j < it.getN() / 2; ++j) {
it.decN();
it.getNext().incN();
it.getNext().getData()[j] = it.getData()[(itn + 1) / 2 + j];
}
}
if(pos > it.getN()) {
pos -= it.getN();
it = it.getNext();
}
for(inti = it.getN() - 1; i >= pos; --i)
it.getData()[i + 1] = it.getData()[i];
it.getData()[pos] = key;
it.incN();
returntrue;
}
returnfalse;
}
// containspublicbooleancontains(Tdata)
{
ArrayListElement<T> it = getFirst();
while(it != null) {
for(inti = 0; i < it.getN(); ++i)
if(data == it.getData()[i].getData())
returntrue;
it = it.getNext();
}
returnfalse;
}