[英]insert method on singly linked list, does this work?
我對正在使用的insert方法感到更擔心,它是一個最多包含10個節點的單鏈接列表。 我有一個SinglyLinkedList
,它具有返回分數的getScore()
方法。 我伴隨了相關方法來查看流程。 基本上,我想完成的事情以及我對insert方法所做的事情都將節點(具有分數)插入到正確的排序位置,從左開始是最高得分,最右邊是最低得分。 如果我有[100]--[80]--[65]
並且得分為67的另一個節點出現,則只要大小為[100]--[80]--[67]--[65]
小於10, if (size == 10)
則表示列表已滿,進入的任何其他節點將僅將其與最后一個節點進行比較,例如,新節點95大於65則僅與65比較。那么這意味着它95必須位於列表中的某個位置,刪除最后一個節點“ 65”,然后遍歷以找到適合95的正確位置。如果另一個節點(例如高於100)(例如200),那么它將只要size < 10
則為[200]--[100]--[95]--[80]--[67]
,否則踢最低分,因此列表仍將保留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;
}
}
您的setNext()方法在哪里? 您要在setNext()中增加大小嗎? ..我認為您的程序將陷入無限循環,因為-
while(getSize() < 10) {
if(pos.getNext() != null && pos.getNext().getScore() > currScore)
pos.setNext(pos);
}
你在哪里做size ++?
給出您正在使用的所有方法的實現...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.