


How to implement addition, deletion and modification of doubly linked list in java
One-way linked list can only search in one direction, while doubly linked list can search forward or backward
One-way linked list cannot delete itself and needs to rely on auxiliary nodes, while doubly linked list can Self-delete
1. The traversal method is the same as that of a singly linked list, except that it can be searched forward or backward
2. Add (default is added to the end of a doubly linked list)
(1) First find the last node of the doubly linked list
(2) temp.next = newHeroNode
(3) newHeroNode.pre = temp
3. The modification idea is the same as the original one-way linked list
4. Deletion
(1) Because it is a two-way linked list, we can realize self-delete Node
(2) Directly find the node to be deleted, such as temp
(3) temp.pre.next = temp.next
(4) temp.next.pre = temp.pre
Code implementation
package com.hsy.linkedlist; public class DoubleLinkedListDemo { public static void main(String[] args) { System.out.println("双向链表测试:"); //先创建节点 HeroNode2 hero1 = new HeroNode2(1, "刘备", "仁义"); HeroNode2 hero2 = new HeroNode2(2, "关羽", "武圣"); HeroNode2 hero3 = new HeroNode2(3, "张飞", "暴躁"); HeroNode2 hero4 = new HeroNode2(4, "赵云", "单骑救主"); //创建一个双向链表 DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(hero1); doubleLinkedList.add(hero2); doubleLinkedList.add(hero3); doubleLinkedList.add(hero4); //显示链表 doubleLinkedList.showList(); //修改 HeroNode2 newHeroNode = new HeroNode2(4, "好汤圆", "hsy"); doubleLinkedList.update(newHeroNode); System.out.println("修改后:"); doubleLinkedList.showList(); //删除 doubleLinkedList.delete(3); System.out.println("删除后:"); doubleLinkedList.showList(); } } //创建一个双向链表 class DoubleLinkedList { //初始化一个头节点,不存放数据 private final HeroNode2 head = new HeroNode2(0, "", ""); //返回头节点 public HeroNode2 getHead() { return head; } //遍历 public void showList() { //判断链表是否为空 if (head.next == null) { System.out.println("链表为空!"); } //由于头节点不能动,因此我们需要一个辅助变量来遍历 HeroNode2 temp = head.next; while (true) { //判断链表是否到最后 if (temp == null) { break; } System.out.println(temp); //这时需要将temp后移,否则会陷入死循环 temp = temp.next; } } //添加一个新的节点在双向链表中 public void add(HeroNode2 heroNode2) { //思路:(不考虑编号顺序) //1.找到当前链表的最后节点 //2.将最后这个节点的next域指向新的节点 HeroNode2 temp = head; //遍历链表,找到最后的节点 while (true) { if (temp.next == null) { break; } //如果没有找到最后,将temp后移 temp = temp.next; } //必须保证退出while循环时,temp指向链表的最后,并将最后这个节点的next域指向新的节点 //形成一个双向链表 temp.next = heroNode2; heroNode2.pre = temp; } //修改节点 public void update(HeroNode2 newHeroNode2) { //判断链表是否为空 if (head.next == null) { System.out.println("链表为空!"); } HeroNode2 temp = head.next; boolean flag = false; while (true) { if (temp == null) { break; } if (temp.no == newHeroNode2.no) { //找到 flag = true; break; } temp = temp.next; } //根据flag判断是否找到需要修改的值 if (flag) {//编号已经存在 temp.name = newHeroNode2.name; temp.nickname = newHeroNode2.nickname; } else {//没有找到 System.out.println("没有找到编号为:" + newHeroNode2.no + "的节点,不能修改"); } } //删除节点 public void delete(int no) { //判断当前链表是否为空 if (head.next==null){ System.out.println("链表为空,无法删除"); } HeroNode2 temp = head.next; boolean flag = false; while (true) { if (temp == null) { break; } if (temp.no == no) { //找到了待删除节点的前一个结点temp flag = true; break; } temp = temp.next; } if (flag) { temp.pre.next = temp.next; //如果要删除的是最后一个节点,就不能执行下面这句话,否则会出现空指针异常 if (temp.next!=null){ temp.next.pre=temp.pre; } } else { System.out.println("要删除的" + no + "节点不存在"); } } } class HeroNode2 { public int no; public String name; public String nickname; public HeroNode2 next;//指向下一个节点 public HeroNode2 pre;//指向上一个节点 //创建构造器 public HeroNode2(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + '}'; } }
The above is the detailed content of How to implement addition, deletion and modification of doubly linked list in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
