LinkedList 根据优先级数添加新节点
LinkedList add new node based on priority number
my java 数据结构分配要求是创建一个食物管理来将食物分配给自然灾害的受害者,它还需要处理 3 种类型的受害者,即儿童、老人和成人。
我想要实现的是说我有一个 LinkedList 来安排优先级。所以现在我有一个向下转换为 Victim
和 Patient
的 Person
对象。我需要处理受害者。
受害者对象
setPriority
(这里我根据受害者的出生日期将受害者分为优先级 1、2、3,即 child
、oldfolks
和 adults
)
所以现在我将有一个受害者对象和它的优先级。
我的想法是在链表ADT中,我把它分成3部分,第一个是child
,第二个是oldfolks
,第三个是adults
。
上面是我的想法的图片,当添加一个新的受害者并且优先级是2时,我需要得到最后一个旧的人然后把新的受害者放在后面然后增加lastOldFolk
位置。
以下是我目前所做的:-
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;
}
所以现在在我的主程序中,我添加了每个优先级的 3 个,然后我添加了另一个优先级为 1 的 newEntry
,它将存储在优先级 1 的第 4 个位置,但如果我添加另一个则不起作用new priority 1. 刚接触数据结构,希望高手赐教
P/S: 我不允许使用数组列表或任何 Java API 来完成任务,我必须创建自己的 ADT 来解决问题。谢谢。
您的解决方案似乎不必要地复杂;我要做的就是创建一个 class,比如 Line,它包含 3 个独立的列表。像这样:
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
}
}
这里我使用了 ArrayList,但肯定有一些其他 java 库实现的堆栈(如 this one),它更适合这个目的。
您甚至可以让行 class 实现 list interface 这样您仍然可以将它用作任何其他标准列表(不过您需要重写这些方法,以考虑3 个队列)
希望对您有所帮助
由于您扫描列表而不是直接访问展示位置,
无需跟踪每个类型的最后一个 <>。
你可以做一个简单的 while 循环,直到你到达一个优先级较低的节点
比新节点或 null,即您应该添加新节点的时间。
如果列表为空,则只放入新节点。
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);
}
}
my java 数据结构分配要求是创建一个食物管理来将食物分配给自然灾害的受害者,它还需要处理 3 种类型的受害者,即儿童、老人和成人。
我想要实现的是说我有一个 LinkedList 来安排优先级。所以现在我有一个向下转换为 Victim
和 Patient
的 Person
对象。我需要处理受害者。
受害者对象
setPriority
(这里我根据受害者的出生日期将受害者分为优先级 1、2、3,即child
、oldfolks
和adults
)
所以现在我将有一个受害者对象和它的优先级。
我的想法是在链表ADT中,我把它分成3部分,第一个是child
,第二个是oldfolks
,第三个是adults
。
上面是我的想法的图片,当添加一个新的受害者并且优先级是2时,我需要得到最后一个旧的人然后把新的受害者放在后面然后增加lastOldFolk
位置。
以下是我目前所做的:-
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;
}
所以现在在我的主程序中,我添加了每个优先级的 3 个,然后我添加了另一个优先级为 1 的 newEntry
,它将存储在优先级 1 的第 4 个位置,但如果我添加另一个则不起作用new priority 1. 刚接触数据结构,希望高手赐教
P/S: 我不允许使用数组列表或任何 Java API 来完成任务,我必须创建自己的 ADT 来解决问题。谢谢。
您的解决方案似乎不必要地复杂;我要做的就是创建一个 class,比如 Line,它包含 3 个独立的列表。像这样:
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
}
}
这里我使用了 ArrayList,但肯定有一些其他 java 库实现的堆栈(如 this one),它更适合这个目的。
您甚至可以让行 class 实现 list interface 这样您仍然可以将它用作任何其他标准列表(不过您需要重写这些方法,以考虑3 个队列)
希望对您有所帮助
由于您扫描列表而不是直接访问展示位置, 无需跟踪每个类型的最后一个 <>。
你可以做一个简单的 while 循环,直到你到达一个优先级较低的节点 比新节点或 null,即您应该添加新节点的时间。 如果列表为空,则只放入新节点。
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);
}
}