LinkedList add new node based on priority number

522 views Asked by At

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 are child, oldfolks and adults 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.

enter image description here

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.

2

There are 2 answers

6
Ran Koretzki On BEST ANSWER

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.

if (firstNode == null) {//if empty list then straight away assign
     firstNode = newNode;
} else {
     if (newNode.getPriority() < firstNode.getPriority()) {
          newNode.next = firstNode;
          firstNode = newNode;
     } else {
         Node current = firstNode;
         while (current.getNext() != null && newNode.getPriority() >= current.getNext().getPriority()) 
             current = current.getNext();
         } 
         newNode.setNext(current.getNext());
         current.setNext(newNode); 
     }
} 
3
Antiphon0x On

Your solution seems unnecessarily complicated; what I would do is just create a class, say Line, which holds the 3 independent lists. Something like this:

class Line {
    private ArrayList<Victim> children;
    private ArrayList<Victim> oldFolks;
    private ArrayList<Victim> adults;

    public void addByPriority(Victim newEntry, int priority) {
        switch(priority) {
        case 1:
             children.add(newEntry);
        break;
        case 2:
             oldFolks.add(newEntry);
        break;
        case 3:
             adults.add(newEntry);
        break;
    }

    public void nextPatient() {
        if(!children.isEmpty()) return children.remove(0);
        if(!oldFolks.isEmpty()) return oldFolks.remove(0);
        if(!adults.isEmpty()) return adults.remove(0);
        return null; // or throw exception, as you like
    }
}

Here I used ArrayList, but there is for sure some other java library implementation of a stack (like this one), which is more suited for the purpose.

You could even make the Line class implement the list interface so that you can still use it as any other standard list (you will need to override the methods though, to take into account the 3 queues)

Hope this helps