Java,字符串的LinkedList。按字母顺序插入
我有一个简单的链表。该节点包含一个字符串(值)和一个int(计数) 在linkedlist中,当我插入时,我需要按字母顺序插入新节点。如果列表中有一个具有相同值的节点,那么我只需增加该节点的计数 我想我的方法真的搞砸了Java,字符串的LinkedList。按字母顺序插入,java,insert,linked-list,Java,Insert,Linked List,我有一个简单的链表。该节点包含一个字符串(值)和一个int(计数) 在linkedlist中,当我插入时,我需要按字母顺序插入新节点。如果列表中有一个具有相同值的节点,那么我只需增加该节点的计数 我想我的方法真的搞砸了 public void addToList(Node node){ //check if list is empty, if so insert at head if(count == 0 ){ head = node;
public void addToList(Node node){
//check if list is empty, if so insert at head
if(count == 0 ){
head = node;
head.setNext(null);
count++;
}
else{
Node temp = head;
for(int i=0; i<count; i++){
//if value is greater, insert after
if(node.getItem().getValue().compareTo(temp.getItem().getValue()) > 0){
node.setNext(temp.getNext());
temp.setNext(node);
}
//if value is equal just increment the counter
else if(node.getItem().getValue().compareTo(temp.getItem().getValue()) == 0){
temp.getItem().setCount(temp.getItem().getCount() + 1);
}
//else insert before
else{
node.setNext(temp);
}
}
}
}
我想您应该使用Google Collections中的一个实现 多重集的工作原理与集合类似,但允许重复(并对其进行计数!)。看看: 一种用于维护 其元素的顺序,根据 他们的自然秩序或 显式比较器
如果看不到完整的代码,就很难进行调试。 我认为问题在于你设置了
Node temp = head;
在循环之前,但在将列表遍历到当前元素时需要重新分配
temp
。在这种情况下,您将继续与头部进行比较您的代码存在一些错误:
- 在
头部插入/before
实际上需要在两种不同的情况下进行:
- 如果列表为空,
头
变为节点
- 如果列表不是空的,但是
节点
小于第一个元素,头
也变为节点
- 在这两种情况下,
node
链接到head
之前指向的任何节点(null
或真实节点),而head
现在指向node
- 如果不是在
头之前插入,则必须在某个节点之后插入。我们只需要找到这个地方。有两种情况:
node.getValue()>temp.getValue()
,以及node.getValue()
node.getValue()>temp.getValue()
和temp.getNext()==null
- 在这两种情况下,
节点插入到temp
和temp.getNext()之间
我建议将插入点搜索封装在其自身的函数中。也就是说,给定列表和值,它需要返回一个节点。如果该节点具有与搜索值相同的值,则只需递增;否则,请在后面插入。作为一种特殊情况,返回null
,指示插入点在头之前
在伪代码中,它将如下所示:
FUNCTION findInsertionPoint(Node head, V value) RETURNS Node
// return null if value needs to be inserted before head
IF head == null OR value < head.getValue()
RETURN null;
// otherwise, either return a node with the given value,
// or return a node after which value should be inserted
Node curr = head;
REPEAT
IF curr.value == value
RETURN curr;
ELSEIF curr.getNext() == null OR curr.getNext().getValue() > value
RETURN curr;
ELSE
curr = curr.getNext();
PROCEDURE insert(V value) {
Node newNode = NEW Node(value);
Node insertPoint = findInsertionPoint(this.head, value);
IF insertPoint == null // insert before head
newNode.setNext(this.head);
this.head = newNode;
ELSE
IF insertPoint.getValue() == value
insertPoint.incrementCounter();
ELSE // insert after insertPoint
newNode.setNext(insertPoint.getNext());
insertPoint.setNext(newNode);
这部分:
IF head == null OR value < head.getValue()
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
IF insertPoint == null
newNode.setNext(this.head); // <<<<<<<<<<<
this.head = newNode;
如果insertPoint==null
newNode.setNext(this.head);//
- 你已经处理好了这个案子
列表最初为空。你
也要照顾好特殊的人
新节点处于be位置时的情况
列表的开头。如果你的名单
是B->C->D
,您正在插入A
- 设置
节点很好。下一步设置为null
(如果尚未设置)。所以
如果节点在
最后,我们将null
作为下一个
最后一个节点
- 您需要更新临时文件才能移动
如果不允许插入,则转到下一个节点
可能的因此,您缺少一个
temp=temp.next代码>
为了做到这一点,我将实现队列接口或扩展现有的PriorityQueue(或可能应用得更好的任何其他排序集合),而不是从头开始开发自己的排序列表。我会将Node类定义为Comparable接口的实现,或者使用Comparator实例实例化我的队列,并重写PriorityQueue add方法,仅当队列中还没有其他对象时才添加新节点,否则会增加计数器。如果使用java>5.0进行类型安全,我将使用generic只允许队列中的节点对象。因为这是家庭作业,所以我不会给您任何源代码。我发现代码有一个大问题:
假设您的列表已经有两个不同的项,并且您正在插入一个新项。在代码中,您正在检查节点
是否大于头部
,如果大于,则立即插入它,忽略列表中的其余项目
你可以这样做。有一些遗漏的细节,你可以自己填写
若列表为空,则设置head=node
,head->next=NULL
,就完成了
否则,如果node->valuevalue
,则设置node->next=head,head=node
否则,如果node->value==head->value
,head->count++
否则,设置tmp=head
。当tmp->next->valuevalue
时,设置tmp=tmp->next
。(检查是否为空!)
如果tmp->next==NULL
,(即您到达了列表的末尾),那么设置tmp->next=node
,就完成了
否则,如果tmp->next->value==node->value
,(即,您到达了具有相同值的节点)tmp->next->count++
否则,如果node->next=tmp->next,tmp->next=node
,则退出
不,我想创建自己的链接list@user69514:这不是最好的主意。。但无论如何,如果您想自己维护它,请使用标准的映射
,其中每个键
都是一个字符串,值
是一个计数器LinkedList
可能是维护您描述的数据结构的最差选择。这是家庭作业,他必须编写自己的列表实现,就这么简单。@user69:我看到您将我的伪代码改编为Java。到目前为止做得很好,但是由于某种原因,当head
不是null
并且插入的值小于head
的值时,您仍然忽略了在head
之前插入的逻辑。请再看看我的伪代码。如果你不明白,问吧。所有东西都在那里是有原因的嘿你是
IF insertPoint == null
newNode.setNext(this.head); // <<<<<<<<<<<
this.head = newNode;