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();
}
}
节点不存在的情况
删除头结点
删除尾节点
删除中间节点
上一篇: Android入门教程(五)
下一篇: python练习题-day05
推荐阅读
-
线性表的链式存储结构:定义、单链表存储结构、给链表头结点分配空间、初始化链表数据、输出链表、在某个位置上插入数据、头插法、尾插法、删除某个位置上的数据、删除某个数据、删除整个链表计算链表的长度
-
Java创建二叉搜索树,实现搜索,插入,删除的操作实例
-
java实现堆的操作方法(建堆,插入,删除)
-
链表的创建,插入,删除,输出基本操作
-
史上“最强”单链表实现(插入,删除,查找,求长...你想要的这里都有!)
-
荐 JAVA实现对已有数据库的插入、删除、更改、查询操作详细解释
-
二叉搜索树的插入、删除、查找等操作:Java语言实现
-
Java实现单链表的操作
-
创建一个最简单的链表,插入和删除
-
循环单向链表的基本操作:插入,删除,遍历,排序