- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据我正在阅读的书,我正在用java编写双向链表的实现。虽然到目前为止我的逻辑已经很好了,但我有两个问题这本书没有涵盖。我的疑问在于从列表中删除特定链接(节点)的方法[其数据(int
)字段与传递给该方法的键匹配]。
我的问题是:
第一个
链接时,为什么下一个
节点的上一个
字段不是设置为 null
,因为 next
节点现在将是链接中的第一个节点,因此不会通过 previous
引用任何内容?我的意思是:current.next.previous = null
最后
链接时,为什么不设置上一个
节点的下一个
链接为null
?这个新节点将是列表中的最后一个,因此显然它不会通过 next
引用任何其他链接。我的意思是:current.previous.next = null
这是我的完整实现,请原谅大量的评论,我只是想跟踪我正在做的事情。
package Chap_5;
/**
* Created by Manish on 10/25/2015.
* this is an implementation of a doubly linked list, it differs from a singly linked list in that you can traverse
* backwards along the list. For this, every Link(node) in the list has a reference to the previous link called previous
* Insertion routine has 3 methods - insertFirst(), insertLast() and insertAfter() which allows you to insert a node
* at the beginning, at the end or after a specific item in the list
* Deletion routine has 3 methods - deleteFirst(), deleteLast() and deleteKey(). the deleteKey() method allows to look
* up a specific node and then delete it.
* this doubly linked list is also a double ended list in that it always keeps a reference to the last node in the list
* (along with the firs)
*/
class Link3 {
//fields - int and 2 references to next and previous
public int iData;
public Link3 next;
public Link3 previous;
//constructor - initialize data and references are set to null auto.
public Link3(int data) {
iData = data;
}
//display the current link's data
public void displayLink() {
System.out.print("{" + iData + "} ");
}
}
class DoublyLinkedList {
//store 2 refernces to first and last link in list;
private Link3 first;
private Link3 last;
//constructor to set references to null
public DoublyLinkedList() {
first = null;
last = null;
}
//------------------------------------------------------------------
//isEmpty() to check if list is empty
public boolean isEmpty() {
return (first == null);
}
//------------------------------------------------------------------
//----------------INSERTION ROUTINES--------------
//------------------------------------------------------------------
//insertFirst() - to insert a node at the beginning of the list
public void insertFirst(int data) {
/**
* inserts a link with given data at the front of the list
*/
//create new link with data
Link3 newLink = new Link3(data);
//special case - if list is empty, set last accordingly
if(isEmpty()) {
last = newLink;
}
else {
//else if list !empty, connect current first link's previous field to newlink
first.previous = newLink;
}
//set next field of newlink to refer to old first
newLink.next = first;
//and set newlink as current first
first = newLink;
}
//------------------------------------------------------------------
//method to insert at last of list
public void insertLast(int data) {
/**
* inserts a link with given data at the end of linked list
*/
//create new link with data
Link3 newLink = new Link3(data);
//special case - if list is empty
if(isEmpty()) {
first = newLink;
}
else {
//if list not empty, make the current last's next refer to newlink
last.next = newLink;
//and set newlink's previous to current last
newLink.previous = last;
}
//finally set last as newlink
last = newLink;
}
//------------------------------------------------------------------
//method to insert at end of a specific link
public boolean insertAfter(int key, int data) {
/**
* inserts a link with given data after the link whose data matches given key
*/
//first search for link with given key, starting at beginning
Link3 current = first;
//until key with data is found
while(current.iData != key) {
current = current.next;
//if end of list is reached without given key, exit
if(current == null) {
return false;
}
}
//when link with key is found, create new link with data to be inserted after it
Link3 newLink = new Link3(data);
//if current link is the last one in the list
if(current == last) {
newLink.next = null;
last = newLink;
}
//else if current link is not last one in the list
else {
//first modify the links towards the right
//make the newlink's next field refer to current link's next field ie: following node
newLink.next = current.next;
//make the following node's previous field refer to newlink
//NOTE: current.next.previous refers to the previous field of the link referred to by next field of the current link
current.next.previous = newLink;
}
//then modify the links on the left
//make newlink's previous field refer to current
newLink.previous = current;
//make current's next refer to newlink
current.next = newLink;
//return status of insertion
return true;
}
//------------------------------------------------------------------
//----------------DELETION ROUTINES--------------
//------------------------------------------------------------------
public Link3 deleteFirst() {
/**
* deletes first node from the linked list
*/
//create link to be returned
Link3 temp = first;
//special case: if only 1 link in list
if(first.next == null) {
//set last as null as there will be no more links in list
last = null;
}
//else if not just 1 link in next
else {
//set following node(after first)'s previous field to null as this will be the new first
first.next.previous = null;
}
first = first.next;
//and return deleted link
return temp;
}
//------------------------------------------------------------------
public Link3 deleteLast() {
/**
* deletes last link from the linked list
*/
//create link to be deleted
Link3 temp = last;
//special case - if only 1 link in list
if(first.next == null) {
//set first to null as no links in list anymore
first = null;
}
else {
//set current second last's next field to null as this is now the new last
last.previous.next = null;
}
//and make the second last link as the new last
last = last.previous;
//and return deleted link
return temp;
}
//------------------------------------------------------------------
public Link3 deleteKey(int key){
/**
* deletes link with given data, if found
*/
//start searching for link from the first
Link3 current = first;
while (current.iData != key) {
//move forward until link found
current = current.next;
if (current == null) {
//if not found, return null
return null;
}
}
//when link found, adjust connections accordingly
//special case 1 - if this is the first link
if(current == first) {
//set new first as the first's next link
first = current.next;
}
else {
//if this is not the first link
// set preceeding link's next to refer to following link by current's next
current.previous.next = current.next;
}
//special case 2 - if this is the last link
if(current == last) {
//set new last as the current last's previous link
last = current.previous;
}
else {
//set current link's next link's previous field to refer to current link's preceeding link
current.next.previous = current.previous;
}
return current;
}
//------------------------------------------------------------------
//----------TRAVERSAL ROUTINES----------
//------------------------------------------------------------------
public void displayForward() {
/**
* display links forward ie: left -> right
*/
System.out.println("List (first -> last)");
//start at first and move forward
Link3 current = first;
while (current != null) {
current.displayLink();
//change current to refer to following node
current = current.next;
}
System.out.println(" ");
}
//------------------------------------------------------------------
public void displayBackward() {
/**
* display links backward ie: right -> left
*/
System.out.println("List (last -> first)");
//start at last and move backward
Link3 current = last;
while (current != null) {
current.displayLink();
//change current to refer to previous node
current = current.previous;
}
System.out.println(" ");
}
}
public class DoublyLinkedApp {
public static void main(String[] args) {
//create new list
DoublyLinkedList theList = new DoublyLinkedList();
//insert 3 at front
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(66);
//insert 3 items at end
theList.insertLast(11);
theList.insertLast(33);
theList.insertLast(55);
//display list forward
theList.displayForward();
//display list backward
theList.displayBackward();
//delete first item
System.out.println("Deleted: " + theList.deleteFirst().iData);
//delete last item
System.out.println("Deleted: " + theList.deleteLast().iData);
//delete a specific link - with key 11
System.out.println("Deleted: " + theList.deleteKey(11).iData);
//display list forward again
theList.displayForward();
//insert 2 items after a specific link
//insert after link with data 22
theList.insertAfter(22, 77);
theList.displayForward();
//insert after link with 33
theList.insertAfter(33, 89);
theList.displayForward();
}
}
最佳答案
When the link to be deleted turns out to be the first link in the list, why isn't the next node's previous field being set to null
通过以下指令将其设置为空:
current.next.previous = current.previous;
事实上,如果 current 是第一个节点,则 current.previous 为 null,并且通过将下一个节点的 previous 设置为 current.previous 来将其设置为 null。
最后一个节点的下一个节点也是如此,通过
将其设置为 nullcurrent.previous.next = current.next;
关于java - 从 Java 中的双向链表实现中删除特定链接的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340142/
背景: 我最近一直在使用 JPA,我为相当大的关系数据库项目生成持久层的轻松程度给我留下了深刻的印象。 我们公司使用大量非 SQL 数据库,特别是面向列的数据库。我对可能对这些数据库使用 JPA 有一
我已经在我的 maven pom 中添加了这些构建配置,因为我希望将 Apache Solr 依赖项与 Jar 捆绑在一起。否则我得到了 SolarServerException: ClassNotF
interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle {
我希望可用于 Java 的对象/关系映射 (ORM) 工具之一能够满足这些要求: 使用 JPA 或 native SQL 查询获取大量行并将其作为实体对象返回。 允许在行(实体)中进行迭代,并在对当前
好像没有,因为我有实现From for 的代码, 我可以转换 A到 B与 .into() , 但同样的事情不适用于 Vec .into()一个Vec . 要么我搞砸了阻止实现派生的事情,要么这不应该发
在 C# 中,如果 A 实现 IX 并且 B 继承自 A ,是否必然遵循 B 实现 IX?如果是,是因为 LSP 吗?之间有什么区别吗: 1. Interface IX; Class A : IX;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在阅读标准haskell库的(^)的实现代码: (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 a -> b ->a expo x0
我将把国际象棋游戏表示为 C++ 结构。我认为,最好的选择是树结构(因为在每个深度我们都有几个可能的移动)。 这是一个好的方法吗? struct TreeElement{ SomeMoveType
我正在为用户名数据库实现字符串匹配算法。我的方法采用现有的用户名数据库和用户想要的新用户名,然后检查用户名是否已被占用。如果采用该方法,则该方法应该返回带有数据库中未采用的数字的用户名。 例子: “贾
我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点
我目前正在 ika 中开发我的 Python 游戏,它使用 python 2.5 我决定为 AI 使用 A* 寻路。然而,我发现它对我的需要来说太慢了(3-4 个敌人可能会落后于游戏,但我想供应 4-
我正在寻找 Kademlia 的开源实现C/C++ 中的分布式哈希表。它必须是轻量级和跨平台的(win/linux/mac)。 它必须能够将信息发布到 DHT 并检索它。 最佳答案 OpenDHT是
我在一本书中读到这一行:-“当我们要求 C++ 实现运行程序时,它会通过调用此函数来实现。” 而且我想知道“C++ 实现”是什么意思或具体是什么。帮忙!? 最佳答案 “C++ 实现”是指编译器加上链接
我正在尝试使用分支定界的 C++ 实现这个背包问题。此网站上有一个 Java 版本:Implementing branch and bound for knapsack 我试图让我的 C++ 版本打印
在很多情况下,我需要在 C# 中访问合适的哈希算法,从重写 GetHashCode 到对数据执行快速比较/查找。 我发现 FNV 哈希是一种非常简单/好/快速的哈希算法。但是,我从未见过 C# 实现的
目录 LRU缓存替换策略 核心思想 不适用场景 算法基本实现 算法优化
1. 绪论 在前面文章中提到 空间直角坐标系相互转换 ,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换。这个就是我们经常在测绘数据处理中,WGS-84坐标系、54北京坐标系
在软件开发过程中,有时候我们需要定时地检查数据库中的数据,并在发现新增数据时触发一个动作。为了实现这个需求,我们在 .Net 7 下进行一次简单的演示. PeriodicTimer .
二分查找 二分查找算法,说白了就是在有序的数组里面给予一个存在数组里面的值key,然后将其先和数组中间的比较,如果key大于中间值,进行下一次mid后面的比较,直到找到相等的,就可以得到它的位置。
我是一名优秀的程序员,十分优秀!