Java 添加到有序链表的前面
我创建了一个有序的链表,由包含指针、数据和用于排序的键的节点组成。到目前为止,在我名为“add”的方法中,我已经成功地将其添加到一个节点之后。例如,先添加A,然后添加B,然后添加C。但是当我尝试在一个节点前面不断添加时,第一个节点似乎没有任何指向它的东西,或者顺序不正确。 通过消除过程,看起来add方法中的第二个else就是我错误地指向节点的地方。我怎样才能得到add方法来添加D,然后是C,然后是B,然后是BCDJava 添加到有序链表的前面,java,linked-list,nodes,Java,Linked List,Nodes,我创建了一个有序的链表,由包含指针、数据和用于排序的键的节点组成。到目前为止,在我名为“add”的方法中,我已经成功地将其添加到一个节点之后。例如,先添加A,然后添加B,然后添加C。但是当我尝试在一个节点前面不断添加时,第一个节点似乎没有任何指向它的东西,或者顺序不正确。 通过消除过程,看起来add方法中的第二个else就是我错误地指向节点的地方。我怎样才能得到add方法来添加D,然后是C,然后是B,然后是BCD public class OrderedLinkedList<E&g
public class OrderedLinkedList<E>{
private int size;
private int index;
private KeyedNode currentNode;
private KeyedNode previousNode;
private KeyedNode head;
/**
* Constructor that creates the head node that points to null and initializes size to 0.
*/
public OrderedLinkedList(){
head = null;
this.size = 0;
}
/**
* Method that goes through the OrderedLinkedList and adds one to counter then returns counter as the size.
*
* @return Returns the int variable counter.
*/
public int size(){
return this.size;
}
/**
*
* @param key
* @param value
* @return
*/
public E add(String key, E value){
if(currentNode == null){//takes care of empty list
currentNode = new KeyedNode(key, value);
head = currentNode;
}
else{
if(key.compareToIgnoreCase(currentNode.getKey()) < 0){//add to left
KeyedNode newNode = new KeyedNode(key, value);
if(previousNode == null){
previousNode = currentNode;
newNode.setNext(currentNode);
currentNode = newNode;
head = currentNode;
}
else{
newNode.setNext(currentNode);
previousNode = currentNode;
currentNode = newNode;
}
}
else if(key.compareToIgnoreCase(currentNode.getKey()) > 0){//add to right
KeyedNode newNode = new KeyedNode(key, value);
previousNode = currentNode;
currentNode = newNode;
currentNode.setNext(previousNode.getNext());
previousNode.setNext(currentNode);
}
}
size++;
return null;
}
/**
* Method that looks through the keys of the OrderedLinkedList nodes and returns the data of the node.
* @param key The String key parameter used to search the OrderedLinkedList.
* @return Returns the data of the node with the matching key.
*/
public E find(String key){
KeyedNode current = head;
while(current != null){
if(current.getKey().compareToIgnoreCase(key) == 0){
return current.getData();
}
else if(key.compareToIgnoreCase(current.getKey()) < 0){
return null;
}
else{
current = current.getNext();
}
}
return null;
}
/**
* Method that creates an index for the OrderedLinkedList, then uses the index to find the position of a node to return.
*
* @param position Position is an int variable used to match with index to find a node.
* @return Returns the data of the node in the desired position.
*/
public E get(int position){
KeyedNode current = head;
index = 0;
if(position < 0 || position > size){
return null;
}
while(current != null){
if(index == position){
return current.getData();
}
else{
current = current.getNext();
index++;
}
}
return null;
}
/**
* Private inner class KeyedNode of OrderedLinkedList begins.
*/
private class KeyedNode{
private String key;
private E data;
private KeyedNode next;
public KeyedNode(String keyWord, E dataItem){
data = dataItem;
key = keyWord;
next = null;
}
//getters
public E getData(){
return data;
}
public KeyedNode getNext(){
return next;
}
public String getKey(){
return key;
}
//setters
@SuppressWarnings("unused")
public void setData(E nodeData){
data = nodeData;
}
public void setNext(KeyedNode nodeNext){
next = nodeNext;
}
@Override
public String toString(){
return "The nodes key is: " + key;
}
}
/**
* private class KeyedNode ends
*/
公共类OrderedLinkedList{
私有整数大小;
私有整数索引;
私钥节点currentNode;
私钥节点previousNode;
私钥节点头;
/**
*构造函数,该构造函数创建指向null的头节点并将大小初始化为0。
*/
公共OrderedLinkedList(){
head=null;
此值为0.size=0;
}
/**
*方法,该方法遍历OrderedLinkedList并将一个添加到计数器,然后将计数器作为大小返回。
*
*@return返回int变量计数器。
*/
公共整数大小(){
返回此.size;
}
/**
*
*@param-key
*@param值
*@返回
*/
公共E添加(字符串键,E值){
如果(currentNode==null){//处理空列表
currentNode=新的KeyedNode(键,值);
head=当前节点;
}
否则{
如果(key.compareToIgnoreCase(currentNode.getKey())<0){//向左添加
KeyedNode newNode=新KeyedNode(键,值);
if(previousNode==null){
previousNode=currentNode;
newNode.setNext(currentNode);
currentNode=newNode;
head=当前节点;
}
否则{
newNode.setNext(currentNode);
previousNode=currentNode;
currentNode=newNode;
}
}
else如果(key.compareToIgnoreCase(currentNode.getKey())>0){//add to right
KeyedNode newNode=新KeyedNode(键,值);
previousNode=currentNode;
currentNode=newNode;
currentNode.setNext(previousNode.getNext());
previousNode.setNext(当前节点);
}
}
大小++;
返回null;
}
/**
*方法,该方法查看OrderedLinkedList节点的键并返回该节点的数据。
*@param key用于搜索OrderedLinkedList的字符串键参数。
*@return返回具有匹配键的节点的数据。
*/
公共E查找(字符串键){
KeyedNode电流=磁头;
while(当前!=null){
if(current.getKey().compareToIgnoreCase(key)==0){
返回current.getData();
}
else if(key.compareToIgnoreCase(current.getKey())<0){
返回null;
}
否则{
current=current.getNext();
}
}
返回null;
}
/**
*方法,该方法为OrderedLinkedList创建索引,然后使用该索引查找要返回的节点的位置。
*
*@param position是一个int变量,用于与索引匹配以查找节点。
*@return返回节点在所需位置的数据。
*/
公共E-get(内部位置){
KeyedNode电流=磁头;
指数=0;
如果(位置<0 | |位置>大小){
返回null;
}
while(当前!=null){
如果(索引==位置){
返回current.getData();
}
否则{
current=current.getNext();
索引++;
}
}
返回null;
}
/**
*OrderedLinkedList的私有内部类KeyedNode开始。
*/
私有类KeyedNode{
私钥;
私人电子数据;
私钥节点next;
公钥节点(字符串关键字,E数据项){
数据=数据项;
关键字=关键字;
next=null;
}
//吸气剂
公共E getData(){
返回数据;
}
公钥节点getNext(){
下一步返回;
}
公共字符串getKey(){
返回键;
}
//二传手
@抑制警告(“未使用”)
公共void setData(E nodeData){
数据=节点数据;
}
public void setNext(KeyedNode nodeNext){
next=nodeNext;
}
@凌驾
公共字符串toString(){
返回“节点键为:”+键;
}
}
/**
*私有类KeyedNode结束
*/
}为什么在需要它们的方法中,
previousNode
和currentNode
是类的成员而不是局部变量?此外,您不处理键相等的情况。如果列表为NULL,如果比较为< 0,否则比较> 0 < /代码>如果比较=0,你需要一个<代码>而<代码>循环。考虑一个不同的名字;“有序”并不意味着你认为它是什么。所有类型的列表已按定义排序。