DBNode.java(创建双向结点)
public class DBNode {
String value;
DBNode pre;
DBNode next;
public DBNode(String value, DBNode pre, DBNode next) {
this.value = value;
this.pre = pre;
this.next = next;
}
// @Override
// public String toString() {
// return "DBNode{" +
// "value='" + value + '\'' +
// ", pre=" + pre +
// ", next=" + next +
// '}';
// }
@Override
public String toString() {//去掉了访问前驱结点,否则会相互循环打印前驱结点
return "DBNode{" +
"value='" + value + '\'' +
", next=" + next +
'}';
}
}
DBNodeDemo.java(创建双向链表)
public class DBNodeDemo {
public static void main(String[] args) {
DBNode dbNode1 = new DBNode("1", null, null);
DBNode dbNode2 = new DBNode("2", dbNode1, null);
DBNode dbNode3 = new DBNode("3", dbNode2, null);
DBNode dbNode4 = new DBNode("4", dbNode3, null);
// //实现1-->2-->3-->4的双向链表
// // 1<--2<--3<--4
dbNode1.next=dbNode2;
dbNode2.next=dbNode3;
dbNode3.next=dbNode4;
//实现双向循环链表
// dbNode1.pre=dbNode4;
// dbNode4.next=dbNode1;
System.out.println(dbNode1);
// @Override
// public String toString() {
// return "DBNode{" +
// "value='" + value + '\'' +
// ", pre=" + pre +
// ", next=" + next +
// '}';
// }
//若前面toString()的方法不变
//此输出结果会爆栈StackOverflowError,
//因执行dbNode1的后驱指向dbNode2,dbNode2的前驱指向dbNode1,不断循环往复。
}
}
MyDBLinked.java(创建头、尾结点和链表大小成员属性,创建增删查改add,remove,set,find成员方法)
public class MyDBLinked {
private DBNode head;//头结点
private DBNode end;//尾结点
private int size;
public MyDBLinked() {
}
public MyDBLinked(DBNode head, DBNode end, int size) {
this.head = head;
this.end = end;
this.size = size;
}
@Override
public String toString() {
return "MyDBLinked{" +
"head=" + head +
", end=" + end +
", size=" + size +
'}';
}
/*添加方法*/
public boolean add(String str) {
//若原来的链表为空
if (head == null) {
head = new DBNode(str, null, null);//新创建一个节点,将其视为头结点
end = head;//因只有一个节点,头结点也是尾节点
size++;
return true;
}
//若原来的链表不为空
DBNode node = new DBNode(str, end, null);//创建一个新节点,并将新节点的前驱指向原来end节点的数据域
end.next = node;//将原来尾结点的后驱指向新创建节点的数据域
end = node;//将新节点赋值为end节点,也可理解为将end引用指向新创建的节点
size++;
return true;
}
/*删除方法*/
public String remove(String str) {
if (size == 0) throw new RuntimeException("dblinked is null!");
//删除的是头结点
if (head.value.equals(str)) {
head = head.next;
if (size == 0) {
end = null;
} else {
//若原链表多余一个节点,需把后移的头结点的前驱指向删除,便于GC
head.pre = null;
}
size--;
return str;
}
//删除的不是头结点
DBNode mid = head;
while (head.next != null && !mid.next.value.equals(str)) {
mid = mid.next;
}
//删除的是尾结点
if (mid.next.next == null) {
mid.next = null;
end = mid;
size--;
return str;
}
//没找到
if (mid.next == null) {
return null;
}
//找到,mid是要删除节点的前一个节点
mid.next = mid.next.next;
size--;
return str;
}
/*修改方法*/
public boolean set(String oldstring, String newstring) {
if (size == 0 || head == null) throw new RuntimeException("dblinked is null!");
//如果修改的头结点是头结点
if (head.value.equals(oldstring)) {
head.value = newstring;
return true;
}
//修改的不是头结点
DBNode mid = head;
while (head.next.value != null && !mid.next.value.equals(oldstring)) {
mid = mid.next;
}
//
if (mid.next == null) {
return false;
}
mid.next.value = newstring;
return true;
}
/*查找方法*/
public boolean find(String str) {
if (size == 0 && head == null) throw new RuntimeException("dblinked is null!");
//查找的元素值是头结点
if (head.value.equals(str)) {
return true;
}
//查找的元素值不是头结点
DBNode mid = head;
//当下一个节点不为空并且下一个节点的值不等于目标元素值,继续找下一个节点
while (mid.next != null && !mid.next.value.equals(str)) {
mid = mid.next;
}
if (mid.next == null) {
return false;
}
return true;
}
}
MyDBLinkedTest.java(实现主类)
public class MyDBLinkedTest {
public static void main(String[] args) {
MyDBLinked myDBLinked = new MyDBLinked();
myDBLinked.add("ff");
myDBLinked.add("hh");
myDBLinked.add("yy");
myDBLinked.add("ll");
System.out.println(myDBLinked);
// myDBLinked.remove("yy");
// System.out.println(myDBLinked);
// myDBLinked.remove("ff");
// System.out.println(myDBLinked);
// myDBLinked.remove("ll");
// System.out.println(myDBLinked);
myDBLinked.set("ff","oo");
System.out.println("改变元素值ff为oo后:"+myDBLinked);
myDBLinked.set("yy","pp");
System.out.println("改变元素值yy为pp后:"+myDBLinked);
myDBLinked.set("ll","qq");
System.out.println("改变元素值ll为qq后:"+myDBLinked);
System.out.println("——————————————————————————");
System.out.println("是否找到字符串oo:"+myDBLinked.find("oo"));
System.out.println("是否找到字符串pp:"+myDBLinked.find("pp"));
System.out.println("是否找到字符串qq:"+myDBLinked.find("qq"));
System.out.println("是否找到字符串xfgj:"+myDBLinked.find("xfgj"));
}
}
结果输出:
MyDBLinked{head=DBNode{value='ff', next=DBNode{value='hh', next=DBNode{value='yy', next=DBNode{value='ll', next=null}}}}, end=DBNode{value='ll', next=null}, size=4}
改变元素值ff为oo后:MyDBLinked{head=DBNode{value='oo', next=DBNode{value='hh', next=DBNode{value='yy', next=DBNode{value='ll', next=null}}}}, end=DBNode{value='ll', next=null}, size=4}
改变元素值yy为pp后:MyDBLinked{head=DBNode{value='oo', next=DBNode{value='hh', next=DBNode{value='pp', next=DBNode{value='ll', next=null}}}}, end=DBNode{value='ll', next=null}, size=4}
改变元素值ll为qq后:MyDBLinked{head=DBNode{value='oo', next=DBNode{value='hh', next=DBNode{value='pp', next=DBNode{value='qq', next=null}}}}, end=DBNode{value='qq', next=null}, size=4}
——————————————————————————
是否找到字符串oo:true
是否找到字符串pp:true
是否找到字符串qq:true
是否找到字符串xfgj:false