public interface IDoubleLinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
boolean addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
boolean contains(int key);
//删除第一次出现关键字为key的节点
int remove(int key);
//删除所有值为key的节点
void removeAllKey(int key);
//得到单链表的长度
int getLength();
void display();
void clear();
}
定义一个DoubleLinkListImpl类实现IDoubleLinked接口。
public class DoubleLinkListImpl implements IDoubleLinked{
class Node{
private int data;//数据
private Node prev;//前驱
private Node next;//后继
public Node(int data){
this.data=data;
this.next=null;
this.prev=null;
}
}
private Node head;
private Node list;
public DoubleLinkListImpl(){
this.head=null;//初始化一下
this.list=null;//标志尾巴
}
@Override
public void addFirst(int data) {
//第一次插入
Node node =new Node(data);
if (head==null){
this.head=node;
this.list=node;
}else {
node.next=this.head;
node.next.prev=node;
this.head=node;
}
}
@Override
public void addLast(int data) {
Node node=new Node(data);
if (this.head==null){
this.head=node;
this.list=node;
}else {
this.list.next=node;
node.prev=this.list;
this.list=node;
}
}
private Node searchIndex(int index){
checkIndex(index);
Node cur =this.head;
int count=0;
while (countgetLength()){
throw new IndexOutOfBoundsException("下标不合法");
}
}
@Override
public boolean addIndex(int index, int data) {
if (index==0){
addFirst(data);
return true;
}else if (index==getLength()){
addLast(data);
return true;
}else {
Node node = new Node(data);
Node cur = searchIndex(index);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
return true;
}
}
@Override
public boolean contains(int key) {
Node cur =this.head;
while (cur!=null){
if (cur.data==key){
return true;
}
cur=cur.next;
}
return false;
}
@Override
public int remove(int key) {
Node cur=this.head;
while (cur!=null){
if (cur.data==key){
int oldData=cur.data;
//删除的节点是头节点
if (cur==this.head){
this.head=this.head.next;
this.head.prev=null;
}else {
//删除普通节点
cur.prev.next=cur.next;
if (cur.next!=null){
cur.next.prev=cur.prev;
}else {
//删除的节点是尾节点,last需要指回来。
this.list=cur.prev;
}
}
return oldData;
}
cur=cur.next;
}
return -1;
}
@Override
public void removeAllKey(int key) {
Node cur=this.head;
while (cur!=null){
if (cur.data==key){
//删除的节点是头节点
if (cur==this.head){
this.head=this.head.next;
this.head.prev=null;
}else {
//删除普通节点
cur.prev.next=cur.next;
if (cur.next!=null){
cur.next.prev=cur.prev;
}else {
//删除的节点是尾节点,last需要指回来。
this.list=cur.prev;
}
}
}
cur=cur.next;
}
}
@Override
public int getLength() {
int count=0;
Node cur=this.head;
while (cur!=null){
count++;
cur=cur.next;
}
return count;
}
@Override
public void display() {
Node cur=this.head;
while (cur!=null){
System.out.print(cur.data+" ");
cur=cur.next;
}
System.out.println();
}
@Override
public void clear() {
while (this.head!=null){
Node cur = this.head.next;
head.next=null;
head.prev=null;
head=cur;
}
this.list=null;
}
}
测试类
public class TestDemo1 {
public static void main(String[] args) throws InterruptedException {
DoubleLinkListImpl doubleLinkList=new DoubleLinkListImpl();
doubleLinkList.addFirst(38);
doubleLinkList.addFirst(68);
doubleLinkList.addFirst(98);
doubleLinkList.display();//98 68 18
doubleLinkList.addIndex(0,18);
doubleLinkList.display();// 38 98 68 18
doubleLinkList.addIndex(2,88);
doubleLinkList.display();// 38 98 88 68 18
doubleLinkList.addIndex(4,58);
doubleLinkList.display();// 38 98 88 68 18 58
doubleLinkList.remove(38);
doubleLinkList.display();// 98 88 68 58 18
doubleLinkList.remove(68);
doubleLinkList.display();// 98 88 58 18
doubleLinkList.remove(58);
doubleLinkList.display();// 98 88 18
System.out.println(doubleLinkList.remove(188));
doubleLinkList.removeAllKey(18);
doubleLinkList.display();
//doubleLinkList.clear();