Java实现单向链表的增删改查

class Node
{
    public int val;
    public Node next;

    public Node(int val)
    {
        this.val = val;
    }
}

class ListHead
{
    public int count ;
    public Node next;

    public ListHead()
    {
        this.count = 0;
        this.next = null;
    }
}

class List
{
    public ListHead head;
    private Node current;

    public List()
    {
        this.head = new ListHead();
        this.current = null;
    }

    public void addNew(int val)
    {
        Node newNode = new Node(val);
        if(this.head.next == null)
            this.head.next = newNode;
        else
            this.current.next = newNode;

        this.current = newNode;
    }

    public boolean contains(int val)
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
                return true;
        }

        return false;
    }

    public void replace(int val,int newVal)
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
            {
                current.val = newVal;
                return;
            }
        }
    }

    public void replaceAll(int val, int newVal)
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.val == val)
                current.val = newVal;
        }
    }

    public void remove(int val)
    {
        Node theOneToBeRemoved = null;
        if(this.head.next != null && this.head.next.val == val)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
            return ;
        }

        for(Node current = this.head.next; current != null; current = current.next)
        {
            if(current.next != null && current.next.val == val )
            {
                theOneToBeRemoved = current.next;
                current.next = theOneToBeRemoved.next;
                theOneToBeRemoved.next = null;
                return;
            }
        }
    }

    public void removeAll(int val)
    {
        Node theOneToBeRemoved = null;
        while(this.head.next != null && this.head.next.val == val)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
        }

        for(Node current = this.head.next; current != null; current = current.next)
        {
            while(current.next != null && current.next.val == val)
            {
                theOneToBeRemoved = current.next;
                current.next = theOneToBeRemoved.next;
                theOneToBeRemoved.next = null;
            }
        }
    }

    public void clear()
    {
        Node theOneToBeRemoved = null;
        while(this.head.next != null)
        {
            theOneToBeRemoved = this.head.next;
            this.head.next = theOneToBeRemoved.next;
            theOneToBeRemoved.next = null;
        }
    }

    public void printList()
    {
        for(Node current = this.head.next; current != null; current = current.next)
        {
            System.out.print(current.val + " ");
        }

        System.out.println("\n=================================");
    }

}

public class  Hello
{
    public static void main(String[] args)
    {
        List myList = new List();

        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(1);
        myList.addNew(2);
        myList.addNew(7);
        myList.addNew(7);
        myList.addNew(8);
        myList.addNew(2);

        myList.printList();
        myList.replaceAll(2,9);
        myList.removeAll(7);

        myList.printList();
    }
}
时间: 2024-08-01 10:42:48

Java实现单向链表的增删改查的相关文章

【C】利用单链表数据结构实现通讯录,链表的增删改查

C语言中实现链表,是需要利用到C语言中比较难的结构体与指针才能实现. 结构体中放一个指向后接节点的指针与每一个结点应该存放的信息. 下面做一个命令行的通讯录来说明链表的增删改查这个问题. 一开始让用户输入链表,按1可以输出,按3可以删除. 可以修改: 可以插入. 按0则可以退出: 代码如下: #include<stdio.h> #include<stdlib.h> typedef struct Linklist{ char name[10];//存放名字 char num[10];

Java API实现Hadoop文件系统增删改查

Java API实现Hadoop文件系统增删改查 Hadoop文件系统可以通过shell命令hadoop fs -xx进行操作,同时也提供了Java编程接口 maven配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.ap

数据结构 线性结构(数组[列表] ,链表 单链表的增删改查**, 线性结构的应用 队列 栈[函数的调用**]),非线性结构 树

数据结构 参考:http://lupython.gitee.io/ 线性结构 就是能够用一根线串起来的数据结构 数组 (列表) 问:申请数组的前提条件是啥? a[12]?内存需要满足的条件? 答:内存必须有一块连续的内存空间 int a[7] : 声明一个数组,这个数组的数组名是 a, 数组的大小是 7, 数组元素的类型是整型. int a[7] = array(1,2,3,4,5,6,7) 问:如何申请内存? 答:C,C++语言,申请:mallco (28).释放:free(28) 问:int

Java描述数据结构之链表的增删改查

链表是一种常见的基础数据结构,它是一种线性表,但在内存中它并不是顺序存储的,它是以链式进行存储的,每一个节点里存放的是下一个节点的"指针".在Java中的数据分为引用数据类型和基础数据类型,在Java中不存在指针的概念,但是对于链表而言的指针,指的就是引用数据类型的地址. 链表和数组都是线性的数据结构,对于数组而言其长度是固定的,由于在内存中其是连续的,因此更适合做查找与遍历,而链表在内存中是并不是顺序存储的,但是由于其是通过"指针"构成的,因此在插入.删除时比较数

Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用

前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WH 一.JDBC是什么? Java Data Base Connectivity,java数据库连接,在需要存储一些数据,或者拿到一些数据的时候,就需要往数据库里存取数据,那么java如何连接数据库呢?需要哪些步骤? 1.注册驱动 什么是驱动? 驱动就是JDBC实现类,通俗点讲,就是能够连接到数据库功能的东西就是驱动,由于市面上有很多数据库,Oracle.MySql等等,所以java就有一个连接数据库

java学习笔记之分层增删改查

增删改查 一.准备jar包 数据库驱动包 DBUtils包 C3P0连接池 的jar包 二.配置好C3P0的配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <c3p0-config> 3 <!-- 默认配置 --> 4 <default-config> 5 <property name="driverClass">com.mysql.jdb

java使用dom4j操作XML增删改查-超简洁

一.前言 1.使用工具:eclipse    环境:JDK-11 2.类说明:共四个类:Zen   |  Shan  |   Gai   |   Cha   (一个类只做一件事,简单明了) 3.引用jar包:dom4j-1.6.1.jar 4.反馈方式:邮箱 [email protected] 5.使用的源XML名为:Humen.xml  内容如下 二.源码 1.在源XML基础上添加新的 节点.属性.文本内容 /** * 作者:向家康 * 版权所有 禁止转发 */package 正式版.XML.

Java 创建链表,增删改查

项目结构: Node.java: package linkedList; public class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } LinkedList.java: package linkedList; public class LinkedList { private Node first; //指向第一个节点(默认为null) privat

通过java实现对数据库的增删改查

package cn.hncu; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;import java.util.UUID; import org.junit.Test; import cn.hncu.pubs.Conn