gpt4 book ai didi

java - 单链表上的插入方法,这有效吗?

转载 作者:行者123 更新时间:2023-12-01 04:20:27 27 4
gpt4 key购买 nike

我更关心我正在研究的插入方法,一个最多只占用10个节点的单链表。我有一个SinglyLinkedListgetScore()返回分数的方法。我附上相关方法看看流程。基本上我想要完成的任务以及我在插入方法上所做的操作将节点(有一个分数)插入到正确的排序位置,从左边是最高分数,最右边是最低分数。如果我有[100]--[80]--[65]又出现了一个分数为 67 的节点,它变成 [100]--[80]--[67]--[65]只要大小小于 10,if (size == 10)那么这意味着列表已满,任何其他进入的节点只会将其与最后一个节点进行比较,例如 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;
}
}

最佳答案

你的setNext()方法在哪里??您是否增加了 setNext() 中的大小? ..我认为你的程序将陷入无限循环,因为 -

 while(getSize() < 10) {
if(pos.getNext() != null && pos.getNext().getScore() > currScore)
pos.setNext(pos);
}

你在哪里做size++?

给出您正在使用的所有方法的实现...

关于java - 单链表上的插入方法,这有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18973376/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com