summaryrefslogtreecommitdiff
path: root/src/de/ostfalia/algo/ws18/s2/Management.java
blob: c88c13b34dee619b884ddd4e646c17c6f3a95b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<IMember> newNode = new LinkedListNode<IMember>(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<IMember> 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<IMember> 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
	}

}