双向链表与单向链表的区别是,单向链表的一个结点包含(当前节点的值val、指向下个结点的地址next);双向链表的一个结点包含(当前节点的值val、指向下个节点的地址Next、指向上一个结点的地址prev)
class ListNode(){
// 也可以使用public进行修饰
// 但是双链表使用private,为了熟悉一下使用get和set操作
private int val;
private ListNode next;
private ListNode prev;
// 生成构造方法
public List(int val){this.val = val;}
// 创建节点中三个部分各自的get和set函数
public void setVal(int val){
this.val = val;
}
public void getVal(){return next;}
public ListNode setNext(ListNode next){
this.next = next;
}
public void getNext(){return prev;}
public ListNode setPrev(NodeList prev){
this.prev = prev;
}
public void getPrev(){return prev;}
}
public class twoWayLinkList{
// 与单向链表不同的是,单向链表只有一个指针head
// 双向链表有两个指针,一个在表头head,和表尾last;
private ListNode head;
private ListNode last;
// 打印链表
public void display(){
ListNode cur = this.head;
while(cur != null){
// 使用了private修饰后,想要访问该结点的val不能之间“点”出来,需要使用get方法
System.out.println(cur.getVal() + " ");
cur = cur.getNext();
}
System.out.println();
}
// 头插法
public void addFirst(int data){
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
this.last = node;
}else{
node.setNext(this.head);
node.setPrev(node);
this.head = node;
}
}
// 尾插法
public void addLast(int data){
ListNode noda = new ListNode(data);
if(this.head == null){
this.head == node;
this.last = node;
}else{
this.last.setNext(node);
node.setPrev(this.last);
this.last = node;
}
}
// 求链表长度
public int size(){
ListNode cur = this.head;
int count = 0;
while(cur != null){
cur = cur.getNext();
count++;
}
return count;
}
// 任意位置插入,第一个数据节点为0下标
public void addIndex(int index, int data){
if(index == 0){
addFirst(data);
return;
}
if(index == size){
addList(data);
return;
}
ListNode cur = this.head;
while(cur != 0){
cur = cur.getNex();
index--;
}
ListNode node = new ListNode(data);
node.setNext(cur);
node.setPrev(cur.getPrev());
node.getPrev().setNote(node);
cur.setPrev(node);
}
// 查找关键字key是否在链表中
public boolean contains(int key){
ListNode cur = this.head;
while(cur != null){
if(cur.getVal == key){
return true;
}
cur = cur.gerNext();
}
return false;
}
// 找到第一次出现关键字key的结点
public ListNode findKeyNode(int key){
ListNode cur = this.head;
while(while != null){
if(cur.getVal == key){
return cur;
}
cur = getNext();
}
return null;
}
// 删除第一次出现关键字key的节点
public void remove(int key){
ListNode cur = this.findKeyNode(key);
if(cur == null){
return;
}
if(cur == this.head){
this.head = this.head.getNext();
this.head.setPrev(null);
}
if(cur == this.last){
cur.setPrev().setNext(null);
this.last = this.last.getPrev();
return;
}
cur.getPrev().setNext(cur.getNext());
cur.getNext().setPrev(cur.getPrev());
}
// 删除所有值为key的结点
public void removeAllKey(int key){
ListNode cur = this.head;
while(cur != null){
if(cur.getVal() == key){
if(cur == this.head){
this.head = this.head.getNext();
this.head.setPrev(null);
}else{
cur.getPrev().setNext(cur.getNext());
if(cur.getNext() != null){//是不是尾结点
cur.getNext().setPrev(cur.getPrev());
}else{
this.last = this.last.getPrev();
}
}
}
cur = cur.getNext();
}
}
// 清空链表
public void clear(){
this.head = null;
this.last = null;
}
}