Java 跟踪sortedLinkedList中上一个节点的最佳方法?
我的CS课教授让我整理一个LinkedList。我尝试使用的对linkedlist进行排序的方法是,每当向链接列表中添加新的int时,都会这样做。问题的实质是,他建议我使用的对linkedlist排序的方法要求我以某种方式跟踪链接列表中的前一个元素是什么,尽管它是单链接列表而不是双链接列表。我问他是否想让我创建一个双链接列表,他说这不是他所说的。最大的障碍是在我的add函数中的第二个else if代码块上,这里的代码在哪里:Java 跟踪sortedLinkedList中上一个节点的最佳方法?,java,linked-list,Java,Linked List,我的CS课教授让我整理一个LinkedList。我尝试使用的对linkedlist进行排序的方法是,每当向链接列表中添加新的int时,都会这样做。问题的实质是,他建议我使用的对linkedlist排序的方法要求我以某种方式跟踪链接列表中的前一个元素是什么,尽管它是单链接列表而不是双链接列表。我问他是否想让我创建一个双链接列表,他说这不是他所说的。最大的障碍是在我的add函数中的第二个else if代码块上,这里的代码在哪里: if ((int) input < (int) current
if ((int) input < (int) current.value){
LinkedListNode newnode = new LinkedListNode(input, current);
如果((int)输入<(int)当前值){
LinkedListNode newnode=新LinkedListNode(输入,当前);
我不知道如何追踪上一个。最好的方法是什么
public class SortedLinkedList<T extends Comparable<T>> {
private LinkedListNode head;
SortedLinkedList() {
head = null;
}
// Start from head
// Check its value
// 2 nodes at once
// Check previous node
// Check next node
// Check after head before end
// Check last element
public synchronized void add(T input) {
LinkedListNode current;
if (head == null) {
LinkedListNode newNode = new LinkedListNode(input, null);
head = newNode;
head.setIndex(0);
} else if ((int) input < (int) head.value) {
current = head;
LinkedListNode newNode = new LinkedListNode(input, null);
head = newNode;
newNode.setNext(current);
} else if ((int) input > (int) head.value) {
current = head;
while (current.getNext() != null) {
if ((int) input < (int) current.value) {
LinkedListNode newnode = new LinkedListNode(input, current);
}
current = current.getNext();
}
} else {
current = head;
int indexCounter = head.index;
while (current.getNext() != null) {
current = current.getNext();
indexCounter++;
int currentgetNEXTHOLDER;
int currentValueHolder;
// Loops through the functuon and switches any values less than the previous
if ((int) current.getNext().value < (int) current.value) {
currentgetNEXTHOLDER = (int) current.getNext().value;
currentValueHolder = (int) current.value;
current.getNext().value = currentValueHolder;
current.value = currentgetNEXTHOLDER;
}
}
current.setIndex(indexCounter);
LinkedListNode mynewNode = new LinkedListNode(input, null);
current.setNext(mynewNode);
}
}
public T getValue(int index) {
T keeptheValue = null;
LinkedListNode current = getHead();
while (current.getNext() != null) {
if (current.index == index) {
keeptheValue = (T) current.value;
}
current = current.getNext();
}
return keeptheValue;
}
public Boolean search(T value) {
LinkedListNode current = getHead();
boolean isitThere = false;
while (current.getNext() != null) {
if (current.value == value) {
isitThere = true;
}
}
return isitThere;
}
public LinkedListNode getHead() {
return head;
}
public String printAllValues() {
LinkedListNode current = head;
String intTOStringchain = "";
while (current.getNext() != null) {
intTOStringchain = intTOStringchain + "," + Integer.toString((int) current.value);
}
return intTOStringchain;
}
class LinkedListNode<T extends Comparable<T>> {
public T value;
private LinkedListNode next;
public int index;
public LinkedListNode previous;
public LinkedListNode(T value, LinkedListNode next) {
this.value = value;
this.next = next;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
public LinkedListNode getPrevious() {
return previous;
}
public void setPrevious(LinkedListNode previous) {
this.previous = previous;
}
public boolean greaterThan(T otherValue) {
int definingValue = otherValue.compareTo(value);
if (definingValue > 0) {
return true;
} else {
return false;
}
}
public void setIndex(int index) {
this.index = index;
}
}
}
公共类分类链接列表{
私有链接列表节点头;
SortedLinkedList(){
head=null;
}
//从头开始
//检查它的值
//一次2个节点
//检查上一个节点
//检查下一个节点
//头后检查,头前检查
//检查最后一个元素
公共同步作废添加(T输入){
LinkedListNode当前;
if(head==null){
LinkedListNode newNode=新LinkedListNode(输入,空);
头=新节点;
人头指数(0);
}如果((int)输入<(int)头值){
电流=水头;
LinkedListNode newNode=新LinkedListNode(输入,空);
头=新节点;
newNode.setNext(当前);
}else if((int)输入>(int)头值){
电流=水头;
while(current.getNext()!=null){
如果((int)输入<(int)当前值){
LinkedListNode newnode=新LinkedListNode(输入,当前);
}
current=current.getNext();
}
}否则{
电流=水头;
int indexCounter=head.index;
while(current.getNext()!=null){
current=current.getNext();
indexCounter++;
int currentgetNEXTHOLDER;
int当前值持有人;
//循环函数并切换任何小于上一个值的值
if((int)current.getNext().value<(int)current.value){
currentgetNEXTHOLDER=(int)current.getNext().value;
currentValueHolder=(int)current.value;
current.getNext().value=currentValueHolder;
current.value=currentgetNEXTHOLDER;
}
}
当前设置索引(indexCounter);
LinkedListNode mynewNode=新LinkedListNode(输入,null);
current.setNext(mynewNode);
}
}
公共T getValue(int索引){
T keeptheValue=null;
LinkedListNode当前=getHead();
while(current.getNext()!=null){
if(current.index==索引){
keeptheValue=(T)current.value;
}
current=current.getNext();
}
返回保留值;
}
公共布尔搜索(T值){
LinkedListNode当前=getHead();
布尔值isitThere=false;
while(current.getNext()!=null){
if(current.value==值){
isitThere=true;
}
}
返回此处;
}
公共LinkedListNode getHead(){
回流头;
}
公共字符串printAllValues(){
LinkedListNode当前=头;
字符串intTOStringchain=“”;
while(current.getNext()!=null){
intTOStringchain=intTOStringchain+“,”+整数.toString((int)当前.value);
}
返回IntToString链;
}
类LinkedListNode{
公共价值观;
私有LinkedListNode下一步;
公共整数指数;
公共LinkedListNode先前;
公共LinkedListNode(T值,LinkedListNode下一个){
这个值=值;
this.next=next;
}
公共LinkedListNode getNext(){
下一步返回;
}
public void setNext(LinkedListNode next){
this.next=next;
}
公共LinkedListNode getPrevious(){
返回上一个;
}
public void setPrevious(LinkedListNode previous){
this.previous=先前;
}
公共布尔值大于(T其他值){
int definingValue=otherValue.compareTo(值);
如果(定义值>0){
返回true;
}否则{
返回false;
}
}
公共void集合索引(int索引){
这个指数=指数;
}
}
}
以上是类中的所有代码,如有任何帮助,将不胜感激。添加方法的伪代码:
prev=null
curr=头
而curr!=null和curr.value如果您的列表是从java.util.list
int idx = Collections.binarySearch( list, valueToAdd );
if( idx < 0 )
idx = -idx - 1;
list.add( idx, valueToAdd );
int idx=Collections.binarySearch(list,valueToAdd);
if(idx<0)
idx=-idx-1;
列表.添加(idx,valueToAdd);
如果使用上述方法添加所有元素,列表中的所有元素都将被排序只要有一个变量指向列表的前一个元素,并将其更新为“当前”问题的实质是,他推荐我使用的对链接列表进行排序的方法要求我以某种方式跟踪链接列表中的前一个元素是什么,尽管它是单链接列表而不是双链接列表。
-您创建了什么链接列表节点前一个
例如?为什么要将类LinkedListNode
中的实例变量声明为public
?我真的不明白这是如何对链接列表进行排序的,您能给我介绍一下逻辑吗?对不起,我有点慢。@a.JDprev
引用curr
之前的节点。如果列表为空,它们都为空。如果curr>
位于第一个节点上,prev
为空。继续在节点之间循环,直到curr.value>value
或列表末尾(curr
为空),此时我们在curr
之前插入新节点,即prev
之后。