简体   繁体   English

LinkedList根据优先级编号添加新节点

[英]LinkedList add new node based on priority number

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. 我的java数据结构分配要求是创建食物管理,以将食物分配给自然灾害的受害者,并且还需要处理3种类型的受害者:儿童,oldFolks和成人。

What I wanted to achieve is say I have a LinkedList to arrange the priorities. 我想实现的目标是说我有一个LinkedList来安排优先级。 So now I have a Person object that downcast to Victim and Patient . 因此,现在我有了一个Person对象,该对象对VictimPatient沮丧。 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) setPriority (这里,我根据受害者的DOB将受害者分为优先级1、2、3,分别是childoldfolksadults

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 . 我的想法是在ADT链表中,我将其分为三个部分,第一个是child ,第二个是oldfolks ,第三个是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. 上面是我的想法的图片,当添加一个新的受害者并且优先级为2时,我需要找到最后一个老人,然后将新的受害者放在后面,然后增加lastOldFolk位置。

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. 因此,现在在我的主程序中,我添加了每个优先级3,然后添加了另一个具有优先级1的newEntry ,它将存储在优先级1的第4个位置,但是如果我添加另一个新的优先级1则不起作用。结构,希望有人能启发我。

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. P / S:不允许使用arraylist或任何Java API来完成任务,我必须创建自己的ADT来解决问题。 Thanks. 谢谢。

Your solution seems unnecessarily complicated; 您的解决方案似乎不必要地复杂; what I would do is just create a class, say Line, which holds the 3 independent lists. 我要做的就是创建一个类,例如Line,它包含3个独立的列表。 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. 在这里,我使用了ArrayList,但是可以肯定有一些其他的Java库实现栈(例如),更适合于此目的。

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) 您甚至可以使Line类实现列表接口,以便仍可以将其用作其他任何标准列表(不过,您需要重写方法,以考虑3个队列)

Hope this helps 希望这可以帮助

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. 您可以执行一个简单的while循环,直到到达一个优先级低于新节点或为空的节点为止,即应添加新节点。 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); 
     }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM