链表
来源:互联网 发布:淘宝运费险退到哪里了 编辑:程序博客网 时间:2024/05/16 10:47
package class1;
class Link {
class Node {
private String data ;
private Node next ;
public Node (String data) {
this.data = data ;
}
public void add(Node newNode) {
if(this.next == null) {
this.next = newNode ;
} else {
this.next.add(newNode);
}
}
public void print() {
System.out.print(this.data + "\t");
if(this.next != null){
this.next.print();
}
}
public boolean search(String data) {
if(data.equals(this.data)) {
return true ;
} else {
if(this.next != null) {
return this.next.search(data) ;
}else {
return false ;
}
}
}
public void delete (Node previous,String data) {
if(data.equals(this.data)) {
previous.next = this .next ;
} else {
if(this.next != null) {
this.next.delete(this,data);
}
}
}
}
private Node root ;
public void addNode(String data) {
Node newNode = new Node(data) ;
if(this.root == null){
this.root = newNode ;
} else {
this.root.add(newNode);
}
}
public void printNode() {
if(this.root != null){
this.root.print() ;
}
}
public boolean contains(String name) {
return this.root.search(name) ;
}
public void deleteNode(String data) {
if(this.contains(data)) {
if(this.root.data.equals(data)) {
this.root = this.root.next ;
} else {
this.root.next.delete(root, data);
}
}
}
}
public class LinkDemo02 {
public static void main(String args[]) {
Link l = new Link() ;
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
System.out.println("===========删除前=======");
l.printNode();
l.deleteNode("C") ;
l.deleteNode("D") ;
System.out.println() ;
System.out.println("======删除后=======");
l.printNode() ;
System.out.println() ;
System.out.println("查询节点:" + l.contains("A"));
}
}
class Link {
class Node {
private String data ;
private Node next ;
public Node (String data) {
this.data = data ;
}
public void add(Node newNode) {
if(this.next == null) {
this.next = newNode ;
} else {
this.next.add(newNode);
}
}
public void print() {
System.out.print(this.data + "\t");
if(this.next != null){
this.next.print();
}
}
public boolean search(String data) {
if(data.equals(this.data)) {
return true ;
} else {
if(this.next != null) {
return this.next.search(data) ;
}else {
return false ;
}
}
}
public void delete (Node previous,String data) {
if(data.equals(this.data)) {
previous.next = this .next ;
} else {
if(this.next != null) {
this.next.delete(this,data);
}
}
}
}
private Node root ;
public void addNode(String data) {
Node newNode = new Node(data) ;
if(this.root == null){
this.root = newNode ;
} else {
this.root.add(newNode);
}
}
public void printNode() {
if(this.root != null){
this.root.print() ;
}
}
public boolean contains(String name) {
return this.root.search(name) ;
}
public void deleteNode(String data) {
if(this.contains(data)) {
if(this.root.data.equals(data)) {
this.root = this.root.next ;
} else {
this.root.next.delete(root, data);
}
}
}
}
public class LinkDemo02 {
public static void main(String args[]) {
Link l = new Link() ;
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
System.out.println("===========删除前=======");
l.printNode();
l.deleteNode("C") ;
l.deleteNode("D") ;
System.out.println() ;
System.out.println("======删除后=======");
l.printNode() ;
System.out.println() ;
System.out.println("查询节点:" + l.contains("A"));
}
}
阅读全文
0 0
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- 链表
- SQL MID() 函数
- 设置时区的几种方式
- 千万不要天真的以为努力就可以成功,埋头走路,也要抬头看天。
- Java定时任务工具Quartz——学习笔记(一)
- 爬虫(一)
- 链表
- 395
- idea 使用记录
- 将json数据集合表格显示及表格中增加按钮
- etcd键值存储系统的介绍和使用
- hrbust 1962 Rectangular Pizza (结构体排序)
- (sqlite3.OperationalError) no such table- users [SQL- 'SELECT users.id AS users_id, users.email AS u
- linux 解压命令 tar
- kubernetes中的中间件集群故障测试