我更关心我正在处理的插入方法,一个单独链接列表,最多只需要10个节点。我有一个SinglyLinkedList
,其getScore()
方法会返回得分。我附带相关方法来查看流程。基本上我想要完成的事情和我在插入方法上所做的事情将节点(有一个分数)插入到左边的正确分类位置是最高分,最右边是最低分。如果我有[100]--[80]--[65]
并且另一个得分为67的节点出现,只要大小小于10,[100]--[80]--[67]--[65]
就会变为if (size == 10)
,这意味着列表已满并且任何其他节点出现in只会将它与最后一个节点进行比较,例如95的新节点只与65进行比较,如果它大于65则表示95必须在列表中的某个位置,删除最后一个节点'65'并迭代通过找到合适的地方95.如果另一个节点例如高于100,例如200 ..那么只要[200]--[100]--[95]--[80]--[67]
它就会size < 10
,否则会得到最低分,所以列表会仍然保持10个节点。你明白了。
public void insert(Node n) {
//insert if node's score is higher than the last node
int currScore = n.getScore();
Node pos = head;
if(pos.getScore() < currScore) {
addFirst(n);
}
if(pos != null) {
if(getSize() == 10) {
if(tail.getScore() < currScore) {
removeLast();
} else {
n = null;
}
} else {
while(getSize() < 10) {
if((pos.getNext() != null) && (pos.getNext().getScore() > currScore))
pos.setNext(pos);
}
insertAfter(n,pos);
}
}
}
public void addFirst(Node v) {
if(head == null) {
head = v;
tail = v;
size++;
}
public void insertAfter(Node p, Node v) { //insert v after p
Node t = p.getNext();
p.setNext(v);
v.setNext(t);
size++;
}
public void removeLast() {
if(head == null) {
return;
}
Node pos = head;
if(pos.getNext() == null) {
pos = null;
}
Node curr = head.getNext();
while(curr.getNext() != null) {
pos = curr;
curr.setNext(curr);
if(curr.getNext() == null) {
curr = null;
}
}
}
public class Node {
private String name;
private int score;
private Node next;
public Node() {
name = "";
score = 0;
next = null;
}
public Node(String name, int score, Node next) {
this.name = name;
this.score = score;
this.next = next;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public Node getNext() {
return next;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
public void setNext(Node next) {
this.next = next;
}
}
public class SinglyLinkedList {
public Node head;
public Node tail;
public int size;
static final int MAXSIZE = 10;
public SinglyLinkedList() {
head = null;
tail = null;
size = 0;
}
public void addFirst(Node v) {
if(head == null) {
head = v;
tail = v;
size++;
}
}
public void addLast(Node v) {
v.setNext(null);
tail.setNext(v);
tail = v;
size++;
}
public void insertAfter(Node p, Node v) { //insert v after p
Node t = p.getNext();
p.setNext(v);
v.setNext(t);
size++;
}
public void removeFirst() {
if(head == null) {
return;
}
Node t = head;
head.setNext(head);
t = null;
size--;
}
public void removeLast() {
if(head == null) {
return;
}
Node pos = head;
if(pos.getNext() == null) {
pos = null;
}
Node curr = head.getNext();
while(curr.getNext() != null) {
pos = curr;
curr.setNext(curr);
if(curr.getNext() == null) {
curr = null;
}
}
}
public void insert(Node n) { //insert if node's score is higher than the last node
int currScore = n.getScore();
Node pos = head;
if(pos.getScore() < currScore) {
addFirst(n);
}
if(pos != null) {
if(getSize() == 10) {
if(tail.getScore() < currScore) {
removeLast();
} else {
n = null;
}
} else {
while(getSize() <= 10) {
if(pos.getNext() != null && pos.getNext().getScore() > currScore)
pos.setNext(pos);
}
insertAfter(n,pos);
}
}
}
public int getSize() {
return size;
}
}
答案 0 :(得分:0)
你的setNext()方法在哪里?你在setNext()中增加大小吗? ..我认为你的程序会无限循环,因为 -
while(getSize() < 10) {
if(pos.getNext() != null && pos.getNext().getScore() > currScore)
pos.setNext(pos);
}
你在哪里做大小++ ??
实施您正在使用的所有方法......