public interface IList {
//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public void 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();
}
2.MySingleLinkedList.java
import java.util.List;
public class MySingleLinkedList implements IList{
static class ListNode { //创建内部类
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public void addFirst(int data) {
ListNode node = new ListNode(data);
node.next = head;
head = node;
}
public void addLast(int data) {
ListNode node = new ListNode(data);
if (head == null) {
head = node;
return;
}
ListNode cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
public void addIndex(int index,int data) {
int len = size();
if(index < 0 || index > len) {
System.out.println("下标不在正确范围内");
return;
}
if (index == 0) {
addFirst(data);
return;
}
if (index == len) {
addLast(data);
return;
}
ListNode cur = head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
public ListNode findNodeOfKey(int key) {
ListNode cur = head;
while (cur.next != null) {
if (cur.next.val == key) {
return cur; //返回值为所查找值的前一个节点
}
cur = cur.next;
}
return null;
}
public void remove(int key) {
if (head == null) {
return;
}
if (head.val == key) {
head = head.next;
return;
}
ListNode cur = findNodeOfKey(key);
if (cur == null) {
return;
}
ListNode del = cur.next;
cur.next = del.next;
}
public void removeAllKey(int key) {
if (head == null) {
return;
}
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (cur.val == key) {
pre.next = cur.next;
cur = cur.next;
}else {
pre = cur;
cur = cur.next;
}
}
//如果从头节点开始,连续两个数都为key的话,放在开头会删不掉
if (head.val == key) {
remove(key);
}
}
public int size() {
int len = 0;
ListNode cur = head;
while (cur != null) {
len++;
cur = cur.next;
}
return len;
}
public boolean contains(int key) {
ListNode cur = head;
while (cur != null) {
if(key == cur.val) {
return true;
}
cur = cur.next;
}
return false;
}
public void display() {
ListNode cur = head;
while (cur != null) { //当cur指向最后一个节点,cur = null
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
3.Test.java
public class Test {
public static void main(String[] args) {
// 创建链表
IList list = new MySingleLinkedList();
//尾插法
list.addLast(23);
list.addLast(34);
list.addLast(45);
list.addLast(56);
//头插法
list.addFirst(12);
//打印链表
list.display();
System.out.println("----------");
//输出元素个数
int size = list.size();
System.out.println("size = " + size);
System.out.println("----------");
//任意位置插入
list.addIndex(0,0);
list.addIndex(2,666);
list.addIndex(7,78);
list.display();
System.out.println("----------");
//删除第一次出现关键字为key的节点
list.remove(666);
list.display();
System.out.println("----------");
//删除所有值为key的节点
list.addIndex(3,999);
list.addFirst(999);
list.display();
list.removeAllKey(999);
list.display();
}
}