package de.ostfalia.algo.ws18.s2; import de.ostfalia.algo.ws18.base.IManagement; import de.ostfalia.algo.ws18.base.IMember; import de.ostfalia.algo.ws18.s1.LinkedListNode; public class Management extends de.ostfalia.algo.ws18.s1.Management implements IManagement { public Management() { super(); } public Management(String csvMembers[]) { super(csvMembers); } public Management(String filename) { super(filename); } /** * Add an element at TODO. * * @return true */ @Override public boolean insert(IMember value) { this.size++; LinkedListNode newNode = new LinkedListNode(value); if (this.head == null) { this.head = newNode; return true; } this.numberOfOperations++; if (newNode.getValue().getKey() < head.getValue().getKey()) { newNode.setNext(this.head); this.head = newNode; return true; // added at the head } LinkedListNode currentNode = this.head; while (currentNode.getNext() != null) { this.numberOfOperations++; if (newNode.getValue().getKey() < currentNode.getNext().getValue().getKey()) { newNode.setNext(currentNode.getNext()); currentNode.setNext(newNode); return true; // added in between } currentNode = currentNode.getNext(); } currentNode.setNext(newNode); return true; // added at the tail } @Override public IMember search(long key) { this.numberOfOperations = 0; if (this.size == 0) { return null; // won't find } LinkedListNode currentNode = this.head; do { this.numberOfOperations++; long currentKey = currentNode.getValue().getKey(); if (currentKey == key) { return currentNode.getValue(); // found } if (currentKey > key) { return null; // won't find } currentNode = currentNode.getNext(); } while (currentNode != null); return null; // not found } }