欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java单链表的插入和删除操作

程序员文章站 2022-04-03 18:05:48
...

StudentNode

package com.zhangyu.linklisttest2.linkedlist;

import java.util.Objects;

/**
 * 节点类
 */
public class StudentNode {
    int id;//学号
    String name;//名字
    int data;//成绩
    StudentNode next;//下一个节点

    public StudentNode(int id, String name, int data) {
        this.id = id;
        this.name = name;
        this.data = data;
        next = null;
    }

    @Override
    public String toString() {
        return "StudentNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", data=" + data +
                ", next=" + next +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentNode node = (StudentNode) o;
        return id == node.id &&
                data == node.data &&
                Objects.equals(name, node.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, data, next);
    }
}

StudentLinkedList

package com.zhangyu.linklisttest2.linkedlist;

import android.util.Log;

/**
 * 构建链表,头结点和尾节点指针
 */
public class StudentLinkedList {
    private static final String TAG = "StudentLinkedList";

    public StudentNode first;
    public StudentNode last;

    //判空
    public boolean isEmpty() {
        return first == null;
    }

    //打印表中内容
    public void print() {
        StudentNode current = first;
        while (current != null) {
            String s = current.toString();
            Log.e(TAG, "print: " + s);
            current = current.next;
        }
    }

    //插入数据
    public void insert(int id, String name, int data) {
        StudentNode node = new StudentNode(id, name, data);
        if (this.isEmpty()) {
            first = node;
            last = node;
        } else {
            last.next = node;
            last = node;
        }
    }

    //删除数据
    public void delete(StudentNode node) {
        if (isEmpty()) {
            Log.e(TAG, "delete: list is empty");
            return;
        }
        //1.删除头结点
        if (first.equals(node)) {
            Log.e(TAG, "delete: 删除头结点");
            first = first.next;
        }
        //2.删除尾节点
        else if (last.equals(node)) {
            Log.e(TAG, "delete: 删除尾节点");
            StudentNode tmpNode = first;
            while (tmpNode.next != last) {
                tmpNode = tmpNode.next;
            }
            tmpNode.next = last.next;//last.next = null
            last = tmpNode;
        }
        //3.删除中间节点
        else {
            StudentNode curNode = first.next;
            StudentNode preNode = first;
            while (curNode != null && !curNode.equals(node)) {
                curNode = curNode.next;
                preNode = preNode.next;
            }
            if (curNode != null && curNode.equals(node)) {
                preNode.next = curNode.next;
                Log.e(TAG, "delete: 删除中间节点");
            } else {
                Log.e(TAG, "delete: 该节点不存在");
            }
        }
        Log.e(TAG, "delete: 结束");
    }

}

Main

package com.zhangyu.linklisttest2;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.zhangyu.linklisttest2.linkedlist.StudentLinkedList;
import com.zhangyu.linklisttest2.linkedlist.StudentNode;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StudentLinkedList linkedList = new StudentLinkedList();
        linkedList.insert(1,"张三",93);
        linkedList.insert(2,"李四",94);
        linkedList.insert(3,"王五",95);
        linkedList.insert(4,"赵六",96);
        linkedList.print();
        //
        Log.e(TAG, "delete" );
//        StudentNode node = new StudentNode(1, "张三", 93);
//        StudentNode node = new StudentNode(2, "李四", 94);
        StudentNode node = new StudentNode(4,"赵六",962);
        linkedList.delete(node);
        linkedList.print();
    }
}

节点不存在的情况

Java单链表的插入和删除操作

删除头结点

Java单链表的插入和删除操作

删除尾节点

Java单链表的插入和删除操作

删除中间节点

Java单链表的插入和删除操作