一.模拟实现
java
public class MyLinkedList {
static class ListNode {
private int val;
private ListNode prev;//前驱
private ListNode next;//后继
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//双向链表的头节点
public ListNode last;//双向链表的尾巴
//得到链表的长度
public int size(){
ListNode cur = head;
int num = 0;
while (cur != null){
cur = cur.next;
num++;
}
return num;
}
public void display(){
ListNode cur = head;
while(cur != null){
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
//查找是否包含关键字key是否在链表当中
public boolean contains(int key){
ListNode cur = head;
while (cur != null){
if (cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
//头插法
public void addFirst(int data){
ListNode cur = new ListNode(data);
if (head == null){
head = cur;
last = cur;
}else {
cur.next = head;
head.prev = cur;
head = cur;
}
}
//尾插法
public void addLast(int data){
ListNode cur = new ListNode(data);
if (head == null){
head = cur;
last = cur;
}else {
last.next = cur;
cur.prev = last;
last = cur;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {
if (index == 0) {
addFirst(index);
} else if (index == size()) {
addLast(data);
} else {
ListNode node = new ListNode(data);
ListNode sea = searchIndex(index);
node.next = sea;
node.prev = sea.prev;
sea.prev = node;
node.prev.next = node;
}
}
private ListNode searchIndex(int index) {
ListNode cur = head;
if (index < 0 || index >= size()){
System.out.println("位置不合法");
return null;
}
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur;
}
private void checkIndex(int index) {
}
//删除第一次出现关键字为key的节点
public void remove(int key){
ListNode node = head;
while (node != null){
if (head.val == key){
head = head.next;
if (head != null) {
head.prev = null;
}else {
last = null;
}
return;
}else if (node.next == null && last.val == key){
last = last.prev;
last.next = null;
return;
}else if (node.val == key){
node.prev.next = node.next;
node.next.prev = node.prev;
return;
}
node = node.next;
}
System.out.println("没有这个");
}
//删除所有值为key的节点
public void removeAllKey(int key){
ListNode node = head;
while (node != null){
if (head.val == key){
head = head.next;
if (head != null) {
head.prev = null;
}else {
last = null;
}
}else if (node.next == null && last.val == key){
last = last.prev;
last.next = null;
}else if (node.val == key){
node.prev.next = node.next;
node.next.prev = node.prev;
}
node = node.next;
}
}
public void clear(){
head = null;
last = null;
}
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addFirst(1);
myLinkedList.display();
myLinkedList.addLast(3);
myLinkedList.display();
System.out.println(myLinkedList.size());
System.out.println(myLinkedList.contains(2));
System.out.println(myLinkedList.contains(99));
myLinkedList.addIndex(1, 2);
myLinkedList.display();
myLinkedList.remove(2);
myLinkedList.display();
myLinkedList.addLast(4);
myLinkedList.addLast(5);
myLinkedList.addLast(6);
myLinkedList.addLast(6);
myLinkedList.addLast(7);
myLinkedList.display();
myLinkedList.remove(1);
myLinkedList.display();
myLinkedList.removeAllKey(6);
myLinkedList.display();
myLinkedList.clear();
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(1);
myLinkedList.addFirst(2);
myLinkedList.display();
myLinkedList.remove(1);
myLinkedList.display();
myLinkedList.addLast(2);
myLinkedList.display();
myLinkedList.remove(2);
myLinkedList.display();
myLinkedList.addFirst(2);
myLinkedList.display();
myLinkedList.remove(2);
myLinkedList.display();
myLinkedList.addFirst(3);
myLinkedList.addFirst(2);
myLinkedList.addLast(3);
myLinkedList.display();
myLinkedList.remove(3);
myLinkedList.display();
myLinkedList.removeAllKey(1);
myLinkedList.display();
myLinkedList.clear();
myLinkedList.display();
System.out.println(1);
}
}
二.LinkedList的使用
1.LinkedList的构造
java
public class MyLinkedList {
public static void main(String[] args) {
//构造一个空的LinkedList
List<Integer>list1 = new LinkedList<>();
List<String>list2 = new java.util.ArrayList<>();
list2.add("haha");
list2.add("hehe");
//使用ArrayList构造LinkedList
List<String>list3 = new LinkedList<>(list2);
}
}