public class test {
public static void main(String[] args) {
fangfa a = new fangfa();
a.Initialize(6);//初始化链表
// a.display();//打印链表
// System.out.println(a.size());//打印链表大小
// System.out.println(a.contains(2));//判断链表中有没有该元素
// a.AddFirst(0);//头插法
// a.AddLast(0);//尾插法
// a.remove(0);//删除链表中全部的 某个元素节点
// a.searchlndex(0, 10);//在链表任意地方 插入元素
a.clear();//非暴力清空链表
a.display();//打印链表
}
}
public class LinkedList {
public int data;
LinkedList last;
LinkedList next;
LinkedList(int a) {
data = a;
}
}
class fangfa {
//成员变量
LinkedList head;
LinkedList tail;
//初始化链表
public void Initialize(int a) {
for (int i = 0; i < a; i++) {
LinkedList middle = new LinkedList(i);
middle.data = i;
if (i > 0) {
tail.next = middle;
tail.next.last = tail;
tail = tail.next;
}
if (i == 0) {
head = middle;
tail = middle;
}
}
}
//打印双链表
public void display() {
LinkedList tmp = head;
while (tmp != null) {
System.out.print(tmp.data + ", ");
tmp = tmp.next;
}
System.out.println();
}
//返回链表大小
public int size() {
int counter = 0;
LinkedList tmp = head;
while (tmp != null) {
counter++;
tmp = tmp.next;
}
return counter;
}
//查找是否包含关键字 key 在链表中
public boolean contains(int a) {
LinkedList tmpleft = head;
LinkedList tmpright = tail;
while ((tmpleft.data != a && tmpright.data != a) && (tmpleft != tmpright) && (tmpleft.next != tmpright)) {
tmpleft = tmpleft.next;
tmpright = tmpright.last;
}
if (tmpleft.data == a || tmpright.data == a) {
return true;
}
return false;
}
//头插法
public void AddFirst(int a) {
if (head == null) {
LinkedList tmp = new LinkedList(a);
head = tmp;
tail = tmp;
return;
}
LinkedList tmp = new LinkedList(a);
tmp.next = head;
head.last = tmp;
head = tmp;
}
//尾插法
public void AddLast(int a) {
if (head == null) {
LinkedList tmp = new LinkedList(a);
head = tmp;
tail = tmp;
return;
}
LinkedList tmp = new LinkedList(a);
tmp.last = tail;
tail.next = tmp;
tail = tmp;
}
//删除全部的 key 节点
public void remove(int a) {
if (head.next == null) {
head = null;
tail = null;
return;
}
while (contains(a)) {
LinkedList tmpleft = head;
LinkedList tmpright = tail;
while ((tmpleft.data != a && tmpright.data != a) && (tmpleft != tmpright) && (tmpleft.next != tmpright)) {
tmpleft = tmpleft.next;
tmpright = tmpright.last;
}
if (tmpleft.data == a || tmpright.data == a) {
if (tmpleft.data == a) {//如果是左下标找到 key 那么进来
if (tmpleft == head || tmpleft == tail) { //如果要删除的节点位置在头,或在尾巴 就进来。
if (tmpleft == head) {//如果在头,就头删
head.next.last = null;
head = head.next;
} else {//如果不在头,就尾删
tail.last.next = null;
tail = tail.last;
}
} else {//如果不在头也不再尾 就中间正常删除
tmpleft.last.next = tmpleft.next;
tmpleft.next.last = tmpleft.last;
}
} else {//不是左下标找到 key 就进来
tmpleft = tmpright;
if (tmpleft.data == a) {//如果是左下标找到 key 那么进来
if (tmpleft == head || tmpleft == tail) { //如果要删除的节点位置在头,或在尾巴 就进来。
if (tmpleft == head) {//如果在头,就头删
head.next.last = null;
head = head.next;
} else {//如果不在头,就尾删
tail.last.next = null;
tail = tail.last;
}
} else {//如果不在头也不再尾 就中间正常删除
tmpleft.last.next = tmpleft.next;
tmpleft.next.last = tmpleft.last;
}
} else {
return;
}
}
} else {
System.out.println("没有找到key,无删除");
}
}
}
//任意位置插入一个元素
public void searchlndex(int index, int a) {
if (head == null) {
System.out.println("链表为空,你怎么任意位置插入?");
return;
}
LinkedList middle = new LinkedList(a);
LinkedList tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
if (tmp == null) {
System.out.println("当前位置不合法");
return;
}
}
if (tmp == head || tmp == tail) {
if (tmp == head) {
middle.next = tmp;
tmp.last = middle;
head = middle;
} else {
middle.last = tmp;
tmp.next = middle;
tail = middle;
}
} else {
middle.next = tmp;
middle.last = tmp.last;
tmp.last.next = middle;
tmp.last = middle;
}
}
//非暴力清空 链表
public void clear() {
head = head.next;
while (head != tail) {
head.last.next = null;
head.last.last = null;
head = head.next;
}
head.last.next = null;
head.last.last = null;
head = null;
tail = null;
}
}