一、模拟实现无头单向非循环链表
public class SingleLinkList {
static class LinkNode {
public int value;
public LinkNode next;
public LinkNode(int value) {
this.value = value;
}
}
public LinkNode head;
public void creatSingleLinkList() {
LinkNode node1 = new LinkNode(1);
LinkNode node2 = new LinkNode(2);
LinkNode node3 = new LinkNode(3);
LinkNode node4 = new LinkNode(4);
LinkNode node5 = new LinkNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
head = node1;
}
public void display() {
LinkNode cur = this.head;
while(cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println();
}
public int size() {
int count = 0;
LinkNode cur = this.head;
while(cur != null) {
cur = cur.next;
count++;
}
return count;
}
public void addFirst(int data) {
LinkNode cur = new LinkNode(data);
cur.next = this.head;
this.head = cur;
}
public void addLast(int data) {
LinkNode cur1 = new LinkNode(data);
LinkNode cur = this.head;
if(cur == null) {
this.head = cur1;
return;
}
while(cur.next != null) {
cur = cur.next;
}
cur.next = cur1;
}
public boolean contains(int key) {
LinkNode cur = this.head;
while(cur != null) {
if(cur.value == key) {
return true;
}
cur = cur.next;
}
return false;
}
private LinkNode addIndexSubOne(int index) {
LinkNode cur = this.head;
while((index - 1) != 0) {
cur = cur.next;
index--;
}
return cur;
}
public boolean addIndex(int index,int data) {
LinkNode cur1 = new LinkNode(data);
if((index < 0) || (index > size())) {
System.out.println(index + "坐标不合法");
return false;
}
if(index == 0) {
addFirst(data);
return true;
}
LinkNode cur = addIndexSubOne(index);
cur1.next = cur.next;
cur.next = cur1;
return true;
}
private LinkNode removeSubOne(int key) {
LinkNode cur = this.head;
while(cur.next != null) {
if(cur.next.value == key) {
return cur;
}
cur = cur.next;
}
return null;
}
public void remove(int key) {
if(head == null) {
return;
}
if(this.head.value == key) {
this.head = this.head.next;
return;
}
LinkNode cur = removeSubOne(key);
if(cur == null) {
System.out.println("要删除的关键字不存在");
return;
}
LinkNode del = cur.next;
cur.next = del.next;
}
public void removeAllKey(int key) {
if(this.head == null) {
return;
}
LinkNode cur = head.next;
LinkNode pre = head;
while(cur != null) {
if(cur.value == key) {
pre.next = cur.next;
cur = cur.next;
}else {
pre = cur;
cur = cur.next;
}
}
if(this.head.value == key) {
this.head = this.head.next;
}
}
public void clear() {
this.head = null;
}
}
二、模拟实现无头双向非循环链表
public class LinkedList {
static class LinkNode {
public int val;
public LinkNode prev;
public LinkNode next;
public LinkNode(int data) {
this.val = data;
}
}
public LinkNode head;
public LinkNode last;
public int size() {
int count = 0;
LinkNode cur = head;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
public void display() {
LinkNode cur = head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public boolean contains(int key) {
LinkNode cur = head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
public void addFirst(int data) {
LinkNode cur = new LinkNode(data);
if(head == null) {
head = cur;
last = cur;
return;
}
cur.next = head;
head.prev = cur;
head = cur;
}
public void addLast(int data) {
LinkNode cur = new LinkNode(data);
if(last == null) {
head = cur;
last = cur;
return;
}
last.next = cur;
cur.prev = last;
last = cur;
}
public boolean addIndex(int index,int data) {
if((index < 0) || (index > size())) {
throw new IndexOutOfException(index + "下标不合法");
}
if(index == 0) {
addFirst(data);
return true;
}
if(index == size()) {
addLast(data);
return true;
}
LinkNode node = new LinkNode(data);
LinkNode cur = head;
while(index != 0) {
cur = cur.next;
index--;
}
node.next = cur;
node.prev = cur.prev;
cur.prev.next = node;
cur.prev = node;
return true;
}
public void remove(int key) {
if(head == null) {
System.out.println("链表为空");
return;
}
LinkNode cur = head;
while(cur != null) {
if(cur.val == key) {
if(cur.prev == null) {
if(cur.next != null) {
head = cur.next;
head.prev = null;
}else {
head = cur.next;
last = null;
}
}else {
if(cur.next == null) {
cur.prev.next = cur.next;
last = cur.prev;
}else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
return;
}else {
cur = cur.next;
}
}
System.out.println("没有要删除的数据");
}
public void removeAllKey(int key) {
if(head == null) {
System.out.println("链表为空");
return;
}
LinkNode cur = head;
while(cur != null) {
if(cur.val == key) {
if(cur.prev == null) {
if(cur.next != null) {
head = cur.next;
head.prev = null;
}else {
head = cur.next;
last = null;
}
}else {
if(cur.next == null) {
cur.prev.next = cur.next;
last = cur.prev;
}else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
cur = cur.next;
}else{
cur = cur.next;
}
}
}
public void clear() {
LinkNode cur = head;
while(cur != null) {
LinkNode curNext = cur.next;
cur.next = null;
cur.prev = null;
cur = curNext;
}
head = null;
last = null;
}
}