- 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/
我正在查看预先重写的 jQuery 代码。我无法理解以下代码。 $('body > *:not(#print-modal):not(script)').clone(); 最佳答案 此选择器匹配以下任何
所以我开始学习MySQL,我对表有点困惑,所以我想澄清一下。数据库中可以有多个表吗?例如: Database1 -Table1 -Username -Password -Table2 -Name
我在 PostgreSQL 中编写了一个函数,其代码如下: for (i = 0; i str[0][i]); values[i] = datumCopy(dat_value,
oid: 行的对象标识符(对象 ID)。这个字段只有在创建表的时候使用了 WITH OIDS ,或者是设置了default_with_oids 配置参数时出现。 这个字段的类型是 oid (和字段同
我在搜索最大连接设备数时发现了 a post大致说: 当使用 P2P_STAR 时,最大设备数量为 10,因为此 topoly 使用 Wi-Fi 热点。也就是说,如果您没有路由器。 这让我问了两个问题
我不明白为什么会这样: Printf.sprintf "%08s" "s" = Printf.sprintf "%8s" "s" - : bool = true 换句话说,我希望: Printf.sp
我正在遵循 Grails in Action 中的示例。我有一个问题,如何理解 addTo*()功能有效。 我有一个简单的域:具有以下关系的用户、帖子、标签: 用户1对M发帖 用户一对一标签 发布 M
请问为什么行 "b[0]= new Child2();"在运行时而不是在编译时失败。请不要检查语法,我只是在这里做了 class Base {} class Child1 : Base {} clas
所以我想进一步加深我对套接字的理解,但是我想首先从最低级别开始(在C语言中,而不是在汇编中大声笑) 但是,我处理的大多数站点都使用SOCK_STREAM或SOCK_DGRAM。但是我已经阅读了Beej
好吧,我对 javascript 语法了解甚少,而且我对 null 的行为感到非常困惑。关于空值有很多讨论,但我似乎无法找出问题所在!请帮我。这是脚本。 var jsonData = '';
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭5 年前。 Improve thi
问题: SeriesSum 类旨在计算以下系列的总和: 类名:SeriesSum 数据成员/实例变量: x:存储整数 n:存储术语数量 sum:用于存储系列总和的双变量 成员函数: SeriesSum
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
今天我在 logcat 中注意到以下内容: D/OpenGLRenderer:0xa2c70600 (CardView) 上的 endAllStagingAnimators,句柄为 0xa2c9d35
如何创建值有序对的列表,例如list1 [(x, y), (x1, y1) ...].?? 学习如何创建此列表后,我需要知道如何将 x 值提供给列表中的用户输入并搜索 x 的下一个值并显示有序对 (x
我在存储过程中有以下逻辑。 这里完成了什么? 如果color为null,替换为'' IF ISNULL(@color, '') <> '' BEGIN END 最佳答案 它等同于: IF (@colo
我知道.Net中的接口(interface)定义了接口(interface)和继承它的类之间的契约。刚刚完成了一个大量使用数据访问层接口(interface)的项目,这让我开始思考。 . .有什么大不
如何防止基类方法被子类覆盖 最佳答案 您不需要做任何特别的事情:默认情况下方法是不可覆盖的。相反,如果您希望该方法可重写,则必须将 virtual 关键字添加到其声明中。 但是请注意,即使方法不可重写
我已阅读以下有关工厂模式的文章 here 请仅引用Class Registration - avoiding reflection这一部分。 这个版本在没有反射的情况下实现了工厂和具体产品之间的减少耦
我正在学习 Java 类(class),但无法完全理解下一课的内容。 目的:本课的目的是通过创建一个模拟 for-each 循环如何工作的替代方案来解释 for-each 循环的工作方式。 在上一课中
我是一名优秀的程序员,十分优秀!