单向链表的元素查找和删除

整个过程以根节点为基础,先确定根节点的情况,再一次类推

 1 package test02;
 2
 3 /*
 4  * 单向链表的正宗实现
 5  * */
 6
 7 class Link{
 8     class Node{
 9         private String data;
10         private Node next;
11         public Node(String data){
12             this.data = data;
13         }
14         public void addNode(Node newNode){
15             if(this.next == null){
16                 this.next = newNode;
17             }else{
18                 this.next.addNode(newNode);
19             }
20         }
21         public void printNode(){
22             System.out.println(this.data);
23             if(this.next != null){
24                 this.next.printNode();
25             }
26         }
27         public boolean searchNode(String data){        //内部定义搜索方法
28             if((this.data).equals(data)){            //判断当前节点内容是否与查找的一致
29                 return true;
30             }else{
31                 if(this.next != null){
32                     return this.next.searchNode(data);
33                 }else{
34                     return false;
35                 }
36             }
37         }
38         public void deleteNode(String data){        //删除节点,仍然以根节点为基础
39             if(this.next == null){
40
41             }else{
42                 if((this.next.data).equals(data)){
43                     this.next = this.next.next;
44                 }else{
45                     this.next.deleteNode(data);
46                 }
47             }
48         }
49     }
50     Node root;
51     boolean b;
52     public void add(String data){
53         Node newNode = new Node(data);        //第一步就是生成节点,接下来就可以参考链表的简单实现方法
54         if(this.root == null){                //抓住根节点是首要任务,以后的其他操作都可以建立在根节点上
55             this.root = newNode;
56         }else{
57             this.root.addNode(newNode);
58         }
59     }
60     public void printnode(){
61         this.root.printNode();
62     }
63     public boolean contains(String data){              //判断元素是否存在
64         return this.root.searchNode(data);
65
66     }
67     public void delete(String data){
68         if(this.root == null){
69
70         }else{
71             if((this.root.data).equals(data)){
72                 this.root = this.root.next;
73                 this.delete(data);                    //如果根节点的下一个节点也是查找内容,则递归
74             }else{
75                 this.root.deleteNode(data);
76             }
77         }
78     }
79 }
80
81 public class LianBiao01 {
82
83     public static void main(String[] args) {
84         Link l = new Link();
85         l.add("ROOT");
86         l.add("A");
87         l.add("B");
88         l.add("C");
89         l.printnode();
90         System.out.println(l.contains("B"));
91         System.out.println(".....");
92         l.delete("B");
93
94         l.printnode();
95
96     }
97 }
时间: 2024-07-30 08:00:40

单向链表的元素查找和删除的相关文章

链表(四)——带表头的单向链表

1.带表头的单向链表 (1)不带表头的单向链表在实现插入和删除时必须区分头结点和其他节点的处理. (2)使用带表头的单向链表的好处:不用考虑头结点的单独处理. 表头节点:数据域没有值,指针域指向单向链表中数据域含值的第一个结点. 2.代表头的单向链表的基本操作 #include <stdio.h> #include <malloc.h> #define NULL 0 typedef struct node { int data; struct node *next; }ElemSN

链表(二)——单向链表的基本操作(创建、删除、打印、结点个数统计)

1.指针的联动 通过两个指针分别指向前驱和后继结点,并在单向链表上进行移动,当指针指向待处理的结点时,该结点的前驱也有指针指向. 2.设有一个无序单向链表,且数据域的值均不相同,使指针pmin指向最小值结点,并使指针prem指向最小值结点的前驱结点: 代码片段: for(p = head; p; q = p, p = p->next) { if(pmin->data > p->data) { pmin = p; prem = q; } } 3.单向链表的删除算法 注:使用mallo

单向链表的建立,添加与删除

/*-------------------------包含头文件------------------------------------*/ #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> int count=0; /*-------------------------结构体定义部分------------------------------*/ typed

给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点

#include <iostream> #include <string.h> #include <stdlib.h> #include <stack> using namespace std; struct Node { int data; struct Node* next; }; struct Node* create_list(int len) { if (len <= 0) return NULL; struct Node* head; st

C语言实现简单的单向链表(创建、插入、删除)及等效STL实现代码

实现个算法,懒得手写链表,于是用C++的forward_list,没有next()方法感觉很不好使,比如一个对单向链表的最简单功能要求: input: 1 2 5 3 4 output: 1->2->5->3->4 相当于仅仅实现了插入.遍历2个功能(当然遍历功能稍微修改就是销毁链表了) 用纯C写了份测试代码 /* 基本数据结构的定义以及函数的声明 */ typedef int ElemType; typedef struct Node { ElemType elem; struc

该文档举例说明了multimap的查找和删除元素的使用

该文档举例说明了multimap的查找和删除元素的使用. 其中,在使用迭代器遍历元素的时候,如果使用了删除迭代器的操作,那么需要小心迭代器失效的情况. /* 功能说明: multimap的查找和删除元素的使用举例 代码说明: 使用multimap的equal_range()方法来查找符合指定key的元素.使用erase来删除指定key的元素. 实现方式: 限制条件或者存在的问题: 无 */ #include <iostream> #include <string> #include

编写程序,查找并删除forward_list&lt;int&gt;中的奇数元素

#include<iostream> #include<forward_list> using namespace std; int main() { forward_list<int> flst={0,1,2,3,4,5,6,7,8,9}; auto prev=flst.before_begin(); auto curr=flst.begin(); while(curr!=flst.end()) { if(*curr%2)//当找到奇数元素时,我们将prev传递给er

单链表的使用(插入,查找,删除,链表的倒置,删除相同结点)

typedef struct node//该结构体代表一个结点{ int data; //结点的数据域 struct node *next; //结点的指针域}lnode,*linklist; //定义一个结构体变量和指向结构体的指针//用头插法创建一个链表linklist create_begin(){ linklist head=(linklist)malloc(sizeof(lnode));//定义头结点并开辟空间 head->next=NULL; //为避免指针乱指,将头结点下一个指针赋

在O(1)时间内删除单向链表结点

原理: 1.若待删结点在单链表中间,则获取结点下一个结点的值,并复制给待删结点,然后删除待删结点的下一个结点. 2.若待删结点在单链表尾部,则顺序遍历单链表,删除. 3.若链表只有一个结点,正是待删结点,则删除之,并修改相关指针. 核心代码: //在O(1)时间删除结点.注:必须保证pDel为链表中的结点 void DeleteNode(List *list,pNode pDel) { if(*list == NULL || pDel == NULL) { return; } //不是最后一个节