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 last;
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(head == null) {
head = node;
last = node;
}else {
node.next = head;
head.prev = node;
head = node;
}
}
public void addLast(int data) {
ListNode node=new ListNode(data);
if(last == null){
last = node;
head = node;
} else{
node.prev=last;
last.next=node;
last=node;
}
}
public void addIndex(int index, int data) {
if(index < 0 || index > size()) {
throw new IndexException("双向链表插入,index不合法"+index);
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
ListNode cur = findIndex(index);
ListNode node = new ListNode(data);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
private ListNode findIndex(int index) {
ListNode cur = head;
while (index != 0) {
cur = cur.next;
index--;
}
return cur;
}
public boolean contains(int key) {
ListNode cur = head;
while (cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
public void remove(int key) {
ListNode cur = head;
while (cur != null) {
if(cur.val == key) {
if(cur == head) {
head = head.next;
if(head != null) {
head.prev = null;
}else {
//只有一个节点 且是需要删除的节点
last = null;
}
}else {
cur.prev.next = cur.next;
//删除中间节点
if(cur.next != null) {
cur.next.prev = cur.prev;
//cur.prev.next = cur.next;
}else {
//删除尾巴节点
//cur.prev.next = cur.next;
last = last.prev;
}
}
return;
}
cur = cur.next;
}
}
public void removeAllKey(int key) {
ListNode cur = head;
while (cur != null) {
if(cur.val == key) {
if(cur == head) {
head = head.next;
if(head != null) {
head.prev = null;
}else {
//只有一个节点 且是需要删除的节点
last = null;
}
}else {
cur.prev.next = cur.next;
//删除中间节点
if(cur.next != null) {
cur.next.prev = cur.prev;
//cur.prev.next = cur.next;
}else {
//删除尾巴节点
//cur.prev.next = cur.next;
last = last.prev;
}
}
}
cur = cur.next;
}
}
public int size() {
int count = 0;
ListNode cur = head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
public void clear() {
ListNode cur = head;
while (cur != null) {
ListNode curNext = cur.next;
cur.next = null;
cur.prev = null;
cur = curNext;
}
this.head = null;
this.last = null;
}
public void display() {
ListNode cur = head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
打印方式
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
System.out.println("====");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("===========");
for(int x : list) {
System.out.print(x +" ");
}
System.out.println();
System.out.println("======");
ListIterator<Integer> it = list.listIterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("======");
ListIterator<Integer> it2 = list.listIterator(list.size());
while (it2.hasPrevious()) {
System.out.print(it2.previous()+" ");
}
System.out.println();
}
}
ArrayList和LinkedList的区别
查找,修改用ArrayList
插入和删除用LinkedList
ArrayList空间上连续,LinkedList空间上不一定连续。