双向链表
package com.baicai.DoubleLinkedList;
public class DoubleLinkedList {
public static void main(String[] args){
//定义变量
Student head = new Student(0,"", null, null);
Student student1 = new Student(1,"蔡俊杰", null, null);
Student student2 = new Student(2,"严汉宁", null, null);
Student student3 = new Student(3,"姚程杰", null, null);
Student student4 = new Student(4,"周渤森", null, null);
Student student5 = new Student(5,"金嘉庆", null, null);
Student student6 = new Student(6,"陈旭瑞", null, null);
Student student7 = new Student(9,"郑晓聪", null, null);
Student student8 = new Student(8,"符祥杰", null, null);
Student student9 = new Student(6,"小黄狗", null, null);
//建立双向链表
head.next = student1;
student1.next = student2;
student2.next = student3;
student3.next = student4;
student4.next = student5;
student5.next = student6;
student6.pre = student5;
student5.pre = student4;
student4.pre = student3;
student3.pre = student2;
student2.pre = student1;
student1.pre = head;
//showDoubleLinkedList(head);
addStudentNode(head, student7);
//showDoubleLinkedList(head);
insertStudentNode(head, student8);
//showDoubleLinkedList(head);
updateStudentNode(head,student9);
//showDoubleLinkedList(head);
deleteStudentNode(head, 9);
showDoubleLinkedList(head);
}
class Student{
public int no;
public String name;
public Student next;
public Student pre;
public Student(int no,String name,Student pre,Student next){
this.no = no;
this.name = name;
this.next = next;
this.pre = pre;
}
@Override
public String toString() {
return no + " " + name;
}
}
删除节点的操作
/*
*
* 删除节点
* */
public static void deleteStudentNode(Student head,int no){
//链表为空
if(head.next == null){
System.out.println("链表为空,没有办法删除");
return;
}
//定义循环变量
Student tempStudent = head.next;
//遍历元素
while(tempStudent.next != null){
if(tempStudent.no == no){
tempStudent.pre.next = tempStudent.next;
tempStudent.next.pre = tempStudent.pre;
tempStudent.pre = null;
tempStudent.next = null;
break;
}
tempStudent = tempStudent.next;
}
//最后一个是要删除的节点
if(tempStudent.next == null && tempStudent.no == no){
tempStudent.pre.next = null;
tempStudent.pre = null;
}
}
插入节点的操作
/*插入节点按照编号*/
public static void insertStudentNode(Student head,Student insertStudentNode){
if(head.next == null) {
addStudentNode(head, insertStudentNode);
}
//创建临时节点
Student tempStudent = head.next;
//辅助变量
boolean flag = true;
//遍历节点
while(flag){
//插入节点
if(tempStudent.no > insertStudentNode.no){
insertStudentNode.pre = tempStudent.pre;
insertStudentNode.next = tempStudent.pre.next;
tempStudent.pre.next = insertStudentNode;
tempStudent.pre = insertStudentNode;
flag = false;
}
if(tempStudent.next == null || flag == false){
break;
}
tempStudent = tempStudent.next;
}
//如果前面没有合适的插入的节点,就直接放在最后面
if(flag == true){
addStudentNode(head, insertStudentNode);
}
}
}
正向遍历节点的操作
/*正向遍历列表*/
public static void showDoubleLinkedList(Student head){
if(head == null || head.next == null){
System.out.println("链表为空或者链表头节点为空");
return;
}
//临时节点
Student tempStudentNode = head.next;
//循环遍历
while(tempStudentNode != null){
//打印节点
System.out.println(tempStudentNode);
//指向下一个元素
tempStudentNode = tempStudentNode.next;
}
}
修改节点的操作
*修改节点 */
public static void updateStudentNode(Student head,Student studentNode){
if(head.next == null){
System.out.println("链表为空,无法完成修改元素");
return;
}
Student tempStudent = head.next;
//遍历链表查看有没有相同的元素
while(tempStudent.next != null) {
if (studentNode.no == tempStudent.no) {
tempStudent.name = studentNode.name;
}
tempStudent = tempStudent.next;
}
}
在链表的最后添加节点
/* 在链表的最后添加节点 */
public static void addStudentNode(Student head,Student studentNode){
if(head.next == null){
head.next = studentNode;
studentNode.pre = head;
return;
}
//建立临时节点
Student tempStudent = head.next;
//遍历节点
while(tempStudent.next != null){
//遍历下一个节点
tempStudent = tempStudent.next;
}
//插入节点
tempStudent.next = studentNode;
studentNode.pre = tempStudent;
}