内含创建、插入、删除。
因为感觉这类操作代码都大同小异,于是就不再缀写了。
//基于双向链表的基本操作
public class dbList {
public int data;
public dbList prior;
public dbList next;
public dbList() {}
public dbList(int data) {
this.data = data;
}
//创建双向链表
public dbList head;//默认值为空
public dbList tail;
//插入指定结点
public void addNode(int data) {
dbList node = new dbList(data);
if(this.head==null) {
this.head = node;
this.tail = node;
}else {
this.tail.next = node;
node.prior = this.tail;
this.tail = this.tail.next;
}
}
//删除指定位置的下一个结点
public void deleteNode(int index) {
dbList temp = this.head;
int step = 0;
while(temp!=null) {
step++;
if(step!=index) {
temp = temp.next;
}else {
temp.next.next.prior = temp;
temp.next = temp.next.next;
}
}
}
//打印双向链表
public void printDB() {
dbList temp = this.head;
while(temp!=null) {
System.out.print(temp.data+" ");
temp = temp.next;
}
}
public static void main(String[] args) {
dbList db = new dbList();
db.addNode(0);
db.addNode(1);
db.addNode(2);
db.addNode(3);
db.printDB();
System.out.println("\t");
db.deleteNode(1);
db.printDB();
}
}