无头双向非循环链表实现
- Ilist.java接口:
- MyLinkedList.java:
无头双向非循环链表大致与无头单向非循环差不多,只不过每个节点多了个prev引用,可以从后一个节点找到前一个节点。并且除了头节点head,双链表还多了个尾节点tail
要模拟实现无头双向非循环链表,同样需要创建三个文件:IList.java接口,MyLinkedList.java文件,test.java测试文件,下面仅展示IList.java接口和MyLinkedList.java文件
Ilist.java接口:
// 2、无头双向链表实现
public class LinkedList {
//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);
//删除第一次出现关键字为key的节点
public void remove(int key);
//删除所有值为key的节点
public void removeAllKey(int key);
//得到单链表的长度
public int size();
public void display();
public void clear();
}
MyLinkedList.java:
public class MyLinkedList implements IList{
static class ListNode {
public int val;
public ListNode next;
public ListNode prev;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode tail;
@Override
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(head == null) {
head = tail = node;
return;
}
node.next = head;
head.prev = node;
head = node;
}
@Override
public void addLast(int data) {
ListNode node = new ListNode(data);
if(head == null) {
head = tail = node;
return;
}
node.prev = tail;
tail.next = node;
tail = node;
}
@Override
public void addIndex(int index, int data) {
int len = size();
if(index < 0 || index > len) {
return;
}
if(index == 0) {
addFirst(data);
return;
}
if(index == len) {
addLast(data);
return;
}
ListNode node = new ListNode(data);
ListNode cur = findIndex(index);
node.prev = cur;
node.next = cur.next;
cur.next.prev = node;
cur.next = node;
}
private ListNode findIndex(int index) {
ListNode cur = head;
for(int i = 0; i < index-1; i++) {
cur = cur.next;
}
return cur;
}
@Override
public boolean contains(int key) {
ListNode cur = head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
@Override
public void remove(int key) {
if(head == null) {
return;
}
ListNode del = findKeyOfIndex(key);
if(del == null) {
return;
}
if(del.prev == null) {
head = head.next;
if(head != null) {
head.prev = null;
}
return;
}
if(del.next == null) {
tail = tail.prev;
tail.next = null;
return;
}
del.prev.next = del.next;
del.next.prev = del.prev;
}
private ListNode findKeyOfIndex(int key) {
ListNode cur = head;
ListNode last = tail;
while(cur != last) {
if(cur.val == key) {
return cur;
}
if(last.val == key) {
return last;
}
cur = cur.next;
last = last.prev;
}
if(cur.val == key) {
return cur;
}
return null;
}
@Override
public void removeAllKey(int key) {
if(head == null) {
return;
}
ListNode start = head;
ListNode cur = head.next;
while(cur != null) {
if(cur.val == key) {
start.next = cur.next;
cur = cur.next;
if(cur != null) {
cur.prev = start;
}
}else {
start = cur;
cur = cur.next;
}
}
if(head.val == key) {
head = head.next;
}
}
@Override
public int size() {
int len = 0;
ListNode cur = head;
while(cur != null) {
len++;
cur = cur.next;
}
return len;
}
@Override
public void clear() {
ListNode cur = head;
while(cur != null) {
ListNode curN = cur.next;
cur.prev = null;
cur.next = null;
cur = curN;
}
head = null;
tail = null;
}
@Override
public void display() {
ListNode cur = head;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
}