我创建了一个由节点组成的有序链接列表,其中包含指针,数据和用于对它们进行排序的键。到目前为止,在我的名为“add”的方法中,我已经让它在节点之后不断成功添加。例如,添加A然后添加B然后添加C.但是当我尝试在节点前连续添加时,第一个节点似乎没有指向它的任何内容或订单不正确。 通过消除过程,它看起来像add方法中的第二个是我错误地指向我的节点的地方。我怎样才能使用add方法添加D然后再添加C然后按B B进行排序?
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
*/
}