gpt4 book ai didi

java - 将比较对象添加到排序链接列表

转载 作者:行者123 更新时间:2023-12-02 08:53:54 26 4
gpt4 key购买 nike

我在这里编写了一个 add(Comparable obj) 方法:但是,当我对其运行测试时,例如下面列出的方法,打印的列表仅包含最近添加的项目。我找不到我的逻辑断点。

当我尝试在 While 循环之前增加 theSize++ 时,收到“线程“main”中的异常 java.lang.ClassCastException:类 java.lang.Character 无法转换为类 java.lang.String (java.lang .Character 和 java.lang.String 位于加载程序“bootstrap”的 java.base 模块中)”。

编辑:完整代码:


/**
* Class OrderedLinkedList.
* <p>
* This class functions as a linked list, but ensures items are stored in ascending order.
*/
public class OrderedLinkedList {

/**************************************************************************
* Constants
*************************************************************************/

/**
* return value for unsuccessful searches
*/
private static final OrderedListNode NOT_FOUND = null;


/**************************************************************************
* Attributes
*************************************************************************/

/**
* current number of items in list
*/
private int theSize;

/**
* reference to list header node
*/
private OrderedListNode head;

/**
* reference to list tail node
*/
private OrderedListNode tail;

/**
* current number of modifications to list
*/
private int modCount;


/**************************************************************************
* Constructors
*************************************************************************/


/**
* Create an instance of OrderedLinkedList.
*/
public OrderedLinkedList() {
// empty this OrderedLinkedList
clear();
}


/**************************************************************************
* Methods
*************************************************************************/


/*
* Add the specified item to this OrderedLinkedList.
*
* @param obj the item to be added
*/
public boolean add(Comparable obj) {
// TODO: Implement this method (8 points)
OrderedListNode node = new OrderedListNode(obj, null, null);

//if list is empty, add to beginning of list
if (isEmpty()) {
head.next = node;
node.next = tail;
tail.previous = node;

}

//if theItem is less than the first element, add it to the beginning of the list
if (obj.compareTo(head.next.theItem) < 0) {
node.next = head.next;
head.next = node;

}

OrderedListNode pointer = head.next;

while (pointer.theItem.compareTo(obj) < 0 && pointer.next != null) {
pointer = pointer.next;
node.previous = pointer.previous;
node.next = pointer;
pointer.previous.next = node;
pointer.previous = node;

theSize++;
modCount++;
}
return true;

}


/*
* Remove the first occurrence of the specified item from this OrderedLinkedList.
*
* @param obj the item to be removed
*/
public boolean remove(Comparable obj) {
// TODO: implement this method (7 points)
OrderedListNode node = head.next;

if (isEmpty()) {
return false;
}

while (node != null) {
if (obj.compareTo(node) == 0) {
node.next.previous = node.previous;
node = node.next;
return true;
}

node = node.next;
}
++modCount;

return false;
}


/**
* Empty this OrderedLinkedList.
*/
public void clear() {
// reset header node
head = new OrderedListNode("HEAD", null, null);

// reset tail node
tail = new OrderedListNode("TAIL", head, null);

// header references tail in an empty LinkedList
head.next = tail;

// reset size to 0
theSize = 0;

// emptying list counts as a modification
modCount++;
}


/**
* Return true if this OrderedLinkedList contains 0 items.
*/
public boolean isEmpty() {
return theSize == 0;
}


/**
* Return the number of items in this OrderedLinkedList.
*/
public int size() {
return theSize;
}


/*
* Return a String representation of this OrderedLinkedList.
*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String s = "";

OrderedListNode currentNode = head.next;

while (currentNode != tail) {
s += currentNode.theItem.toString();

if (currentNode.next != tail) {
s += ", ";
}

currentNode = currentNode.next;
}

return s;
}


/**************************************************************************
* Inner Classes
*************************************************************************/


/**
* Nested class OrderedListNode.
* <p>
* Encapsulates the fundamental building block of an OrderedLinkedList
* contains a data item, and references to both the next and previous nodes
* in the list
*/

public static class OrderedListNode {
Comparable theItem;
OrderedListNode next;
OrderedListNode previous;

OrderedListNode(Comparable item, OrderedListNode prev, OrderedListNode next) {
this.theItem = item;
this.previous = prev;
this.next = next;
}

}


// TODO: Implement the nested class OrderedListNode (5 points). This nested class
// should be similar to the nested class ListNode of the class LinkedList, but
// should store a data item of type Comparable rather than Object.

public static void main(String[] args) { System.out.println("test");

OrderedLinkedList listOne = new OrderedLinkedList();

listOne.add('1');
listOne.add('2');
listOne.add('3');

System.out.println(listOne + " ");

}
}

最佳答案

这是 add 方法的编辑版本:

public boolean add(Comparable obj) 
{
// TODO: Implement this method (8 points)
OrderedListNode node = new OrderedListNode(obj, null, null);

//if list is empty, add to beginning of list
if (isEmpty())
{
head.next = node;
node.next = tail;
tail.previous = node;
theSize++;
}
else
{
OrderedListNode pointer = head.next;
while(pointer.theItem.compareTo(obj) < 0 && pointer != tail)
{
pointer = pointer.next;
}
if(pointer == tail)
{
pointer = tail.previous;
pointer.next = node;
node.previous = pointer;
node.next = tail;
tail.previous = node;
modCount++;
}
else
{
node.previous = pointer.previous;
node.next = pointer;
pointer.previous.next = node;
pointer.previous = node;
modCount++;
}
theSize++;
}
return true;

}

这应该有效!我认为通过列表进行洗牌而不是仅仅找到正确的位置会导致一些指针问题......这应该可行!

关于java - 将比较对象添加到排序链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60604563/

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