- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
问题出在 DLinkedList 类中的 findA 方法。该方法显然改变了我的 head.next 指向 tmp.next我创建了一个列表 {0,1,2,3,4,5,6,7,8,9}我用过
findA(9)
并且我的列表缩小到 {9},尽管无论给定值是否在我的列表中(true 或 false),函数都会给出正确的结果
另一方面,我的 find 方法运行良好,两者之间的唯一区别是我在 findA 中使用 Node tmp = head和 Node tmp = head.next in find
下面是完整的代码片段。我意识到有些实现非常不专业。任何对此的评论将不胜感激
public class Node <T extends Comparable<T>> {
T data;
Node prev;
Node next;
Node(){
}
Node(T val){
data = val;
} }
public class DLinkedList<T extends Comparable<T>> {
Node head;
Node tail;
DLinkedList(){
head = new Node();
tail = new Node();
tail.prev = head;
}
void insertInOrder(T value){
Node insert = new Node(value);
if(head.next==null){
head.next = insert;
insert.prev = head;
insert.next = tail;
tail.prev = insert;
}
else{
insert.prev = tail.prev;
tail.prev.next = insert;
tail.prev = insert;
insert.next = tail;
}
}
boolean find (T value){
boolean result = false;
Node tmp = head.next;
if (head!=null){
while(tmp!=null){
if(tmp.data.compareTo(value)!=0){
tmp = tmp.next;
}
else{
result = true;
break;
}
}
}
return result;
}
boolean findA (T value){
boolean result = false;
Node tmp = head;
if (head!=null){
while(tmp.next!=null){
if(tmp.next.data.compareTo(value)!=0){
tmp.next = tmp.next.next;
}
else{
result = true;
break;
}
}
}
return result;
}
void deleteA(T value){
Node tmp = head.next;
while(tmp.data.compareTo(value)!=0){
tmp = tmp.next;
}
if(tmp!=tail){
if(tmp==head.next)
head = tmp.next;
else
tmp.prev.next = tmp.next;
if (tmp==tail)
tail = tmp.prev;
else
tmp.next.prev = tmp.prev;
}
}
void delete(T value){
Node tmp = head.next;
if(find(value)){
while(tmp!=tail){
if(tmp.data.compareTo(value)!=0){
tmp = tmp.next;
}
else{
tmp.prev.next = tmp.next;
tmp.next.prev = tmp.prev;
break;
}
}
}
}
@Override
public String toString(){
Node tmp = head.next;
String result = "";
while(tmp!=tail){
System.out.println(tmp.data);
tmp = tmp.next;
}
return result;
} }
public class ListCheck {
public static void main(String[] args) {
DLinkedList list = new DLinkedList();
DLinkedList listA = new DLinkedList();
for(int i=0; i<10; i++){
list.insertInOrder(i);
listA.insertInOrder(i);
}
System.out.println(listA.findA(9));
System.out.println(list.find(9));
listA.toString();
System.out.println("");
list.toString();
} }
最佳答案
在你的 findA 中,你移动 tmp 的方式是通过执行
tmp.next = temp.next.next
通过这种方式,您将销毁当前指针并将其重新路由到下一个节点(java 的浅拷贝):
tmp--->[node1]--->[node2]
更改为 tmp--->[node2]
因此,在操作结束时,您的链表只剩下最后一个节点。
将其更改为tmp = tmp.next
会有所帮助
关于java - 双向链表 - 头在变化而我没有改变它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42172612/
我正在尝试将多个项目转换为 classy-prelude眼下。虽然大多数行为对我来说似乎相当简单,但 (head . head) 在简单的 2D 列表上给出了神秘的错误。 考虑以下 GHCi sess
我创建了这个类来从请求中获取 Header 值。 public class AuthenticationHeader { private static IHttpContextAccesso
这里有两段代码。 工作: joins :: [String] -> String -> String joins [] _ = "" joins [x] _ = x joins xs d = head
我是编程新手,正在Windows中使用vscode并通过Mingw 64进行编译。尝试了数天后,我要求对此问题做出解答。我用谷歌搜索了很多次。这是我的项目树的外观。请帮助我包括并将 libA 链接到
我正在研究 Ninety-Nine Scala Problems 的 P07 问题: P07 (**) Flatten a nested list structure. Example: scala>
这里有两段代码。 工作: joins :: [String] -> String -> String joins [] _ = "" joins [x] _ = x joins xs d = head
我正在使用现成的欧洲 cookie 政策脚本。问题是,我的 Hubspot CMS 只允许所有内容的全局标题或每个页面的单独标题。我们有 120 个页面,很难手动定义它,但我们需要一个用于德语页面的脚
Solaris 10 SPARC Sun Studio C 编译器 12.3 在 SPARC64 机器上,如果您访问一个在相关 4 或 8 字节边界上未正确对齐的变量,您将获得核心转储。这需要编码人员
我正在尝试从服务器列表中获取 apache 版本。我正在考虑解析 header ,但是我无法发送 HEAD 但它可以与 GET POST 等一起使用。 这是我的代码: import java.io.B
嗨,我是 github 的新手,当我对代码做了一些更改然后 merge 到上游时,如果有任何冲突,它将执行以下操作。 Auto-merging Global.asax.cs CONFLICT (con
例如,获取偏移量 Y 之后的前 X 行的建议方法是什么?我目前正在做的是: offset, limit = 2, 2 df=pd.DataFrame([{'a':1}, {'a': 2}, {'a':
当我在R块 header 中插入长标题等内容时,能够将 header 拆分为多行会很好。 有没有简单的方法可以做到这一点? 例如。: ```{r, echo=FALSE, warning=FALSE,
我刚刚开始使用 Erlang(虽然有一些 lisp 背景)并且有一个关于列表模式匹配的问题。 如果我说 [Head | Tail] = [1, 2, 3]. 然后我得到 Head = 1 Tail =
我的 index.php 文件开头有这段代码: if ( !isset($_GET['cat']) ) die(header("Location: ?cat=top")
对第 3 方应用程序进行一些 SOAP 调用。他们提供此 soap header 作为应用程序期望的示例。如何在 PHP 中创建这样的 SOAP header ?
我对 git 仓库下的文件做了一些更改,我使用 git commit 提交了文件 然后我尝试使用 git push origin master 推送到 master,它返回了 Everything u
我刚刚尝试从 url 下载 webp 图像,但是当我尝试处理存储的图像时,我发现了一些不同的东西。 如果我从浏览器下载图像,它可以使用 x/image/webp 包解码,但如果我使用 http.Get
这是来自 slices 的略微修改的代码 var buffer [256] byte func SubtractOneFromLength(slice []byte) []byte { sli
在 Linux 中是否有一种方法可以请求 Head 或 Tail 但要忽略额外的记录偏移量。 例如,如果文件 example.lst 包含以下内容: row01 row02 row03 row04 r
代码: /* * File: problem5.c * Author: levihackwith * Description: Write a Pop() function that is th
我是一名优秀的程序员,十分优秀!