编写一个方法remove(),接受一条链表和一个字符串key作为参数,删除链表中所有item域为key的结点
核心代码:
//接受一条链表和一个字符串key作为参数,删除链表中所有item域为key的结点
public void remove(Object obj){
if(N <= 0){
return;
}
NODE node = first;
for(int i = 1; i <= N; i++){
//数值相匹配
while (node.obj == obj){
int t = k;
Node_Text = first;
//错误
for(int j = 1; j < k; j++){
Node_Text = Node_Text.next;
}
Node_Text.next = Node_Text.next.next;
N--;
node = Node_Text.next;
}
node = node.next;
k++;
}
}
总体代码:
//编写一个方法remove(),接受一条链表和一个字符串key作为参数,删除链表中所有item域为key的结点
public class Node {
//链表指针
private NODE first = null;
public NODE getFirst() {
return first;
}
private NODE lase = null;
public NODE getLase() {
return lase;
}
//计数器
private int N = 0;
public int getN() {
return N;
}
//结点
private class NODE{
Object obj;
NODE next;
}
//判断链表是否为空
public Boolean IsEmpty(){
return first == null;
}
//添加节点
public void EnNode(Object obj){
NODE oldNode = lase;
lase = new NODE();
lase.obj = obj;
lase.next = null;
if(IsEmpty()){
first = lase;
oldNode = lase;
}
oldNode.next = lase;
N++;
}
//接受一个链表结点并插入到指定结点的后面
public void insertAfter(NODE node1, NODE node2){
if(node1 == null || node2 == null){
System.out.println("插入失败");
return;
}
node2.next = node1.next.next;
node1.next = node2;
}
//删除节点
public Object delete(int k){
if(k < 0 || k > N){
System.out.println("k的范围不符合规定");
return "";
}
NODE Node = first;
Object obj = null;
for (int i = 1; i < k - 1; i++){
Node = Node.next;
}
obj = Node.next.obj;
Node.next = Node.next.next;
N--;
return "删除的元素是:" + obj;
}
//接受一个链表结点并接受另一个结点作为后续结点,并删除原后续结点
public Object removeAfter(NODE Top_Node){
if(Top_Node == null || Top_Node.next == null){
return "空";
}
Object Deter_Obj = Top_Node.next.obj;
Top_Node.next = Top_Node.next.next;
N--;
return Deter_Obj;
}
//获取指定结点
public NODE Get_Node(int Key){
if(Key < 0 || Key > N){
System.out.println("指定位置不存在");
return null;
}
NODE node = first;
for(int i = 0; i < Key - 1; i++){
node = node.next;
}
return node;
}
int k = 0;
NODE Node_Text = null;
//接受一条链表和一个字符串key作为参数,删除链表中所有item域为key的结点
public void remove(Object obj){
if(N <= 0){
return;
}
NODE node = first;
for(int i = 1; i <= N; i++){
//数值相匹配
while (node.obj == obj){
int t = k;
Node_Text = first;
//错误
for(int j = 1; j < k; j++){
Node_Text = Node_Text.next;
}
Node_Text.next = Node_Text.next.next;
N--;
node = Node_Text.next;
}
node = node.next;
k++;
}
}
//查寻
public boolean find(Object KEY){
NODE node = first;
if(N <= 0){
return false;
}
for(int i = 0; i < N; i++){
if(node.obj.equals(KEY)){
return true;
}
node = node.next;
}
return false;
}
//遍历
public void print(){
if(N <= 0){
System.out.println("链表已为空,无法删除");
return;
}
NODE node = first;
for(int i = 0; i < N; i++){
System.out.println("元素:" + node.obj);
node = node.next;
}
System.out.println();
}
}
测试代码:
public class Text {
public static void main(String[] args) {
Node node = new Node();
node.EnNode(2);
node.EnNode(1);
node.EnNode(1);
node.EnNode(3);
node.EnNode(1);
node.EnNode(1);
node.EnNode(4);
node.EnNode(5);
node.EnNode(1);
node.EnNode(6);
node.print();
node.remove(1);
node.EnNode(7);
node.print();
}
}
测试结果:
"C:\Program Files\Java\jdk1.8.0_333\bin\java.exe" "-javaagent:D:\IDEA\IntelliJ IDEA Community Edition 2020.3\lib\idea_rt.jar=57742:D:\IDEA\IntelliJ IDEA Community Edition 2020.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_333\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_333\jre\lib\rt.jar;E:\······后端······\1.2 ····java系列····\01_代码库\!Java数据结构\练习题\链表习题\删除指定数据的结点\out\production\untitled" Text
元素:2
元素:1
元素:1
元素:3
元素:1
元素:1
元素:4
元素:5
元素:1
元素:6
元素:2
元素:3
元素:4
元素:5
元素:6
元素:7
进程已结束,退出代码为 0
感谢观看~