my java data structure assignment requirements are to create a food management to assign the food to the victim of the natural disaster and it also requires to handle 3 types of victim which are child, oldFolks and adult.
What I wanted to achieve is say I have a LinkedList to arrange the priorities. So now I have a Person
object that downcast to Victim
and Patient
. I need to handle the victim.
Victim object
setPriority
(here I categories the victim into priority 1,2,3 which arechild
,oldfolks
andadults
based on their DOB)
So now I will have a victim object and priority of it.
My idea is in the linkedlist ADT, I divides it into 3 portions which is first child
, 2nd is oldfolks
and 3rd is adults
.
Above is a picture of my idea, when adding a new victim and the priority is 2, I need to get the last old folk and then put the new victim at the back and then increment the lastOldFolk
location.
Below is what I have done so far:-
public boolean addByPriority(T newEntry, int priority) {
Node newNode = new Node(newEntry);
System.out.println(firstNode);
if (firstNode == null) {//if empty list then straight away assign
firstNode = newNode;
switch (priority) {//update the last location of each priorities
case 1:
lastChild++;
lastSenior++;
lastAdult++;
break;
case 2:
lastSenior++;
lastAdult++;
break;
case 3:
lastAdult++;
break;
}
return true;
} else if (firstNode != null && priority == 1) {//if priority is 1 then add here
Node node = firstNode;
for (int i = 0; i < lastChild; i++) {
node = node.next;
}
Node savedNext = node.next;
node.next = newNode;
node.next.next = savedNext;
lastChild++;
} else if (firstNode != null && priority == 2) {
} else {
}
length++;
return true;
}
So now at my main program, I added 3 of each priorities after that I add another newEntry
with priority 1, it will store at 4th position of priority 1 but doesn't work if I add another new priority 1. I am new to data structure, I hope someone can enlighten me.
P/S: I'm not allowed to use arraylist or any Java API to complete the task, I have to create my own ADT to the solve the problem. Thanks.
Since your scaning the list and not directly accessing the placement location, there is no need to keep track on last of <> each type.
you can do a simple while loop, until you reach a node who priority, is lower than the new node or null, that is when you should add the new node. if the list is null, just put the new node.